Overview
ClasspathExtension implements the Extension interface to load and install Firefox browser extensions from the Java classpath into a Firefox profile.
Description
The ClasspathExtension class enables loading Firefox extensions (XPI files) that are bundled as resources within the Java classpath. It takes a reference class (used to locate resources via Class.getResourceAsStream()) and a resource path string. When writeTo() is called, it verifies the resource is a zipped file (XPI format), creates a staging directory within the target extensions directory, extracts the classpath resource to that staging area, and then delegates to FileExtension to perform the final installation. This class only supports zipped (XPI) extensions and will throw a WebDriverException if the resource is not in ZIP format.
Usage
Use ClasspathExtension when you need to install a Firefox extension that is packaged as a resource within your project's classpath (e.g., bundled in a JAR file). This is typically invoked indirectly through FirefoxProfile.addExtension(Class, String) when the specified path does not correspond to an existing file on disk.
Code Reference
Source Location
Signature
public class ClasspathExtension implements Extension {
public ClasspathExtension(Class<?> loadResourcesUsing, String loadFrom)
@Override
public void writeTo(File extensionsDir) throws IOException
}
Import
import org.openqa.selenium.firefox.ClasspathExtension;
I/O Contract
Constructor Parameters
| Parameter |
Type |
Description
|
loadResourcesUsing |
Class<?> |
The class whose classloader is used to locate the extension resource via getResourceAsStream().
|
loadFrom |
String |
The classpath resource path to the extension file (must be a zipped/XPI file).
|
Methods
| Method |
Input |
Output |
Description
|
writeTo |
File extensionsDir |
void |
Extracts the classpath extension resource to a staging directory and installs it into the given extensions directory via FileExtension.
|
Exceptions
| Exception |
Condition
|
WebDriverException |
The resource at loadFrom is not a zipped file.
|
IOException |
An I/O error occurs while creating directories or writing the extension file.
|
IllegalArgumentException |
The resource specified by loadFrom cannot be found on the classpath of the given class.
|
Usage Examples
// Add a classpath extension to a Firefox profile
FirefoxProfile profile = new FirefoxProfile();
profile.addExtension(MyTestClass.class, "/extensions/my-extension.xpi");
FirefoxOptions options = new FirefoxOptions();
options.setProfile(profile);
WebDriver driver = new FirefoxDriver(options);
// Direct usage of ClasspathExtension
ClasspathExtension extension = new ClasspathExtension(
MyTestClass.class, "/extensions/my-extension.xpi");
extension.writeTo(new File("/path/to/profile/extensions"));
Related Pages
- FirefoxProfile - Manages extensions and calls
ClasspathExtension when loading from classpath
- HasExtensions - Interface for runtime extension install/uninstall operations
- FirefoxOptions - Accepts a profile containing extensions
- FirefoxDriver - Launches Firefox with the configured profile and extensions
Page Connections
Double-click a node to navigate. Hold to expand connections.