0
# Extension Management
1
2
Browser extension installation, management, and removal with support for temporary and permanent extensions. Firefox WebDriver provides comprehensive extension management capabilities through multiple interfaces.
3
4
## Capabilities
5
6
### HasExtensions Interface
7
8
Main interface for managing browser extensions during WebDriver sessions.
9
10
```java { .api }
11
/**
12
* Interface for managing browser extensions.
13
* Implemented by FirefoxDriver to provide extension management capabilities.
14
*/
15
@Beta
16
public interface HasExtensions {
17
18
/**
19
* Installs a browser extension from a file path.
20
* @param path Path to extension file (.xpi) or directory
21
* @return Extension ID string for later reference
22
*/
23
String installExtension(Path path);
24
25
/**
26
* Installs a browser extension with temporary flag option.
27
* @param path Path to extension file (.xpi) or directory
28
* @param temporary true for temporary installation, false for permanent
29
* @return Extension ID string for later reference
30
*/
31
String installExtension(Path path, Boolean temporary);
32
33
/**
34
* Uninstalls a previously installed extension.
35
* @param extensionId Extension ID returned from installExtension
36
*/
37
void uninstallExtension(String extensionId);
38
}
39
```
40
41
**Usage Examples:**
42
43
```java
44
import org.openqa.selenium.firefox.FirefoxDriver;
45
import org.openqa.selenium.firefox.HasExtensions;
46
import java.nio.file.Paths;
47
48
FirefoxDriver driver = new FirefoxDriver();
49
HasExtensions extensionManager = driver; // FirefoxDriver implements HasExtensions
50
51
// Install permanent extension
52
String extensionId = extensionManager.installExtension(
53
Paths.get("/path/to/extension.xpi")
54
);
55
56
// Install temporary extension (removed when browser closes)
57
String tempExtensionId = extensionManager.installExtension(
58
Paths.get("/path/to/temp-extension.xpi"),
59
true // temporary
60
);
61
62
// Uninstall extension
63
extensionManager.uninstallExtension(extensionId);
64
```
65
66
### Extension Interface
67
68
Base interface for extension implementations supporting different sources.
69
70
```java { .api }
71
/**
72
* Interface for Firefox extensions from various sources.
73
* Provides unified API for extension installation.
74
*/
75
public interface Extension {
76
77
/**
78
* Writes the extension to the specified parent directory.
79
* @param parentDirectory Directory where extension should be written
80
*/
81
void writeTo(File parentDirectory);
82
}
83
```
84
85
### FileExtension Class
86
87
Extension implementation for loading extensions from the file system.
88
89
```java { .api }
90
/**
91
* Extension loaded from file system.
92
* Supports both .xpi files and unpacked extension directories.
93
*/
94
public class FileExtension implements Extension {
95
96
/**
97
* Creates a FileExtension from a file or directory.
98
* @param toInstall Extension file (.xpi) or directory to install
99
*/
100
public FileExtension(File toInstall);
101
102
/**
103
* Writes the extension to the specified parent directory.
104
* @param parentDirectory Directory where extension should be written
105
*/
106
@Override
107
public void writeTo(File parentDirectory);
108
}
109
```
110
111
**Usage Examples:**
112
113
```java
114
import org.openqa.selenium.firefox.FileExtension;
115
import java.io.File;
116
117
// Create extension from .xpi file
118
File xpiFile = new File("/path/to/extension.xpi");
119
FileExtension extension = new FileExtension(xpiFile);
120
121
// Create extension from directory
122
File extensionDir = new File("/path/to/unpacked-extension");
123
FileExtension extension = new FileExtension(extensionDir);
124
125
// Write extension to profile directory
126
File profileDir = new File("/tmp/firefox-profile");
127
extension.writeTo(profileDir);
128
```
129
130
### ClasspathExtension Class
131
132
Extension implementation for loading extensions from the Java classpath.
133
134
```java { .api }
135
/**
136
* Extension loaded from classpath resources.
137
* Useful for bundling extensions with test applications.
138
*/
139
public class ClasspathExtension implements Extension {
140
141
/**
142
* Creates a ClasspathExtension from classpath resources.
143
* @param loadResourcesUsing Class to use for resource loading context
144
* @param loadFrom Classpath location of extension resources
145
*/
146
public ClasspathExtension(Class<?> loadResourcesUsing, String loadFrom);
147
148
/**
149
* Writes the extension to the specified parent directory.
150
* @param parentDirectory Directory where extension should be written
151
*/
152
@Override
153
public void writeTo(File parentDirectory);
154
}
155
```
156
157
**Usage Examples:**
158
159
```java
160
import org.openqa.selenium.firefox.ClasspathExtension;
161
162
// Load extension from classpath
163
ClasspathExtension extension = new ClasspathExtension(
164
MyTestClass.class, // Class for resource context
165
"/extensions/my-test-extension" // Classpath location
166
);
167
168
// Write to profile directory
169
File profileDir = new File("/tmp/firefox-profile");
170
extension.writeTo(profileDir);
171
```
172
173
### Profile-Based Extension Management
174
175
Extensions can also be managed through FirefoxProfile for persistent installation.
176
177
```java { .api }
178
// From FirefoxProfile class
179
/**
180
* Adds an extension from the classpath to the profile.
181
* @param loadResourcesUsing Class to use for resource loading
182
* @param loadFrom Classpath location of extension
183
*/
184
public void addExtension(Class<?> loadResourcesUsing, String loadFrom);
185
186
/**
187
* Adds an extension from a file to the profile.
188
* @param extensionToInstall Extension file (.xpi) or directory
189
*/
190
public void addExtension(File extensionToInstall);
191
192
/**
193
* Adds an extension with a specific key to the profile.
194
* @param key Extension identifier key
195
* @param extension Extension implementation
196
*/
197
public void addExtension(String key, Extension extension);
198
```
199
200
**Complete Extension Management Example:**
201
202
```java
203
import org.openqa.selenium.firefox.*;
204
import java.io.File;
205
import java.nio.file.Paths;
206
207
// Method 1: Runtime extension management
208
FirefoxDriver driver = new FirefoxDriver();
209
210
// Install extension at runtime
211
String extensionId = driver.installExtension(
212
Paths.get("/path/to/extension.xpi")
213
);
214
System.out.println("Installed extension with ID: " + extensionId);
215
216
// Install temporary extension
217
String tempId = driver.installExtension(
218
Paths.get("/path/to/dev-extension"),
219
true // temporary
220
);
221
222
// Use the browser with extensions
223
driver.get("https://example.com");
224
// ... test operations that use extension functionality
225
226
// Remove extension when done
227
driver.uninstallExtension(extensionId);
228
229
// Method 2: Profile-based extension management
230
FirefoxProfile profile = new FirefoxProfile();
231
232
// Add extension from file
233
File extensionFile = new File("/path/to/extension.xpi");
234
profile.addExtension(extensionFile);
235
236
// Add extension from classpath
237
profile.addExtension(MyTestClass.class, "/test-extensions/helper-extension");
238
239
// Add custom extension implementation
240
Extension customExtension = new FileExtension(new File("/path/to/custom.xpi"));
241
profile.addExtension("custom-key", customExtension);
242
243
// Use profile with driver
244
FirefoxOptions options = new FirefoxOptions().setProfile(profile);
245
WebDriver driver2 = new FirefoxDriver(options);
246
// Extensions are automatically available
247
248
// Method 3: Mixed approach
249
FirefoxProfile profile = new FirefoxProfile();
250
// Add persistent extensions to profile
251
profile.addExtension(new File("/path/to/persistent-extension.xpi"));
252
253
FirefoxOptions options = new FirefoxOptions().setProfile(profile);
254
FirefoxDriver driver3 = new FirefoxDriver(options);
255
256
// Add temporary extension at runtime
257
String runtimeId = driver3.installExtension(
258
Paths.get("/path/to/temporary-extension.xpi"),
259
true
260
);
261
262
// Now browser has both persistent and temporary extensions
263
```
264
265
**Extension Development Tips:**
266
267
```java
268
// For development workflow with unpacked extensions
269
File devExtensionDir = new File("/workspace/my-extension/src");
270
String devExtensionId = driver.installExtension(
271
devExtensionDir.toPath(),
272
true // temporary for development
273
);
274
275
// Test extension functionality
276
driver.get("https://test-site.com");
277
// ... test extension behavior
278
279
// Reload extension after changes
280
driver.uninstallExtension(devExtensionId);
281
String newId = driver.installExtension(devExtensionDir.toPath(), true);
282
283
// Package extension for distribution
284
File packagedExtension = new File("/workspace/my-extension/dist/extension.xpi");
285
String prodId = driver.installExtension(packagedExtension.toPath(), false);
286
```