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

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

主站蜘蛛池模板: 兴义市| 壤塘县| 交城县| 东源县| 青田县| 大城县| 全南县| 柞水县| 囊谦县| 蒙城县| 定南县| 盐源县| 静乐县| 南江县| 和顺县| 江阴市| 江西省| 鸡西市| 五原县| 永仁县| 鲜城| 昆山市| 铁力市| 巴林左旗| 德庆县| 云霄县| 教育| 外汇| 凯里市| 肥西县| 阳朔县| 襄汾县| 中方县| 四平市| 金阳县| 广平县| 嘉禾县| 潜江市| 瑞安市| 宝丰县| 青田县|