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

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

主站蜘蛛池模板: 镇巴县| 太湖县| 武城县| 临清市| 夏河县| 黄梅县| 穆棱市| 深泽县| 南木林县| 伽师县| 西平县| 安庆市| 天水市| 黄山市| 安阳县| 喜德县| 阳原县| 阳信县| 赤壁市| 聊城市| 芦山县| 叶城县| 原平市| 陈巴尔虎旗| 宁明县| 西乡县| 留坝县| 科技| 阜平县| 九台市| 四川省| 镇雄县| 双流县| 商河县| 平度市| 大兴区| 镇巴县| 柘荣县| 松桃| 旺苍县| 富阳市|