官术网_书友最值得收藏!

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

主站蜘蛛池模板: 奎屯市| 台北市| 安图县| 万全县| 延吉市| 罗田县| 嘉黎县| 高清| 西青区| 苏州市| 曲阜市| 潮州市| 汶川县| 陇川县| 大城县| 沙雅县| 航空| 和硕县| 丁青县| 通河县| 库尔勒市| 施甸县| 余庆县| 通许县| 九台市| 彩票| 旬阳县| 林州市| 孝义市| 夹江县| 城口县| 钟祥市| 宁海县| 荔波县| 铜陵市| 连南| 莱芜市| 锡林郭勒盟| 定州市| 南昌县| 中方县|