Adding the extension to Firefox
In this section, we will see how we can extend our Firefox browser with some additional capabilities using Profiles. Whenever WebDriver launches a new Firefox browser, it creates a new profile on the disk, and this profile doesn't contain any of the installed Firefox extensions in it. We will add an extension using the Profiles every time WebDriver creates an instance of the Firefox browser.
Now, let's change the profile using the addExtension() method provided by FirefoxProfile. This method is used to add extensions to the Firefox browser.
The following is the API syntax for the method:
public void addExtension(java.io.File extensionToInstall) throws java.io.IOException
The input parameter is the XPI file that has to be installed on the Firefox browser. If WebDriver doesn't find the file in the specified location, it will raise IOException. The following is the code to override the default profile and extend the Firefox browser to have an extension named Xpath Finder:
public class FirefoxCustomProfile {
public static void main(String... args) {
System.setProperty("webdriver.gecko.driver",
"./src/test/resources/drivers/geckodriver 2");
FirefoxProfile profile = new FirefoxProfile();
profile.addExtension(
new File("./src/test/resources/extensions/xpath_finder.xpi"));
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.setProfile(profile);
FirefoxDriver driver = new FirefoxDriver(firefoxOptions);
try {
driver.get("http://www.google.com");
} finally {
//driver.quit();
}
}
}
Now, if you see the Firefox browser that is launched by the FirefoxDriver, you will find the Xpath Finder extension installed on it. In the console log, you will see a message indicating the extension has been added to the browser:
1532196699704 addons.xpi-utils DEBUG New add-on xPathFinder@0.9.3 installed in app-profile