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

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

主站蜘蛛池模板: 紫金县| 弥勒县| 宜州市| 阿勒泰市| 上饶县| 吉林省| 特克斯县| 布尔津县| 江阴市| 行唐县| 三门峡市| 金秀| 昌黎县| 铜梁县| 吉林市| 广丰县| 宜宾县| 通渭县| 鹤庆县| 莱阳市| 乌拉特后旗| 庄浪县| 南汇区| 怀远县| 临武县| 万安县| 晴隆县| 宾阳县| 郴州市| 九龙县| 巴楚县| 深水埗区| 公主岭市| 奈曼旗| 长子县| 孟连| 景宁| 都兰县| 华阴市| 中阳县| 新泰市|