Browser Plugin Testing With Selenium

雨燕双飞 提交于 2019-11-27 18:24:30

The answer is Yes, Selenium 2 supports (remote) installation of browser extensions.

The Chrome and Firefox WebDriver support the installation of extensions, remotely. Here's sample code for Chrome and Firefox:

Chrome

File file = new File("extension.crx"); // zip files are also accepted
ChromeOptions options = new ChromeOptions();
options.addExtensions(file);

// Option 1: Locally.
WebDriver driver = new ChromeDriver(options);

// Option 2: Remotely
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);

Firefox

File file = new File("extension.xpi");
FirefoxProfile firefoxProfile = new FirefoxProfile();
firefoxProfile.addExtension(file);

// Option 1: Locally
WebDriver driver = new FirefoxDriver(firefoxProfile);

// Option 2: Remotely
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setCapability(FirefoxDriver.PROFILE, firefoxProfile);
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);

I have also implemented automated installation of Opera and Safari extensions, and they have been merged upstream:

Opera

This API is similar to the FirefoxDriver.

File file = new File("extension.oex"); // Must end with ".oex"
OperaProfile operaProfile = new OperaProfile();
operaProfile.addExtension(file);

// Option 1: Locally
WebDriver driver = new OperaDriver(operaProfile);

// Option 2: Remotely
DesiredCapabilities capabilities = DesiredCapabilities.opera();
capabilities.setCapability("opera.profile", operaProfile);
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);

Safari

This API is similar to the ChromeDriver.

File file = new File("extension.safariextz");
SafariOptions options = new SafariOptions();
options.addExtensions(file);

// Option 1: Locally.
WebDriver driver = new SafariDriver(options);

// Option 2: Remotely
DesiredCapabilities capabilities = DesiredCapabilities.safari();
capabilities.setCapability(SafariOptions.CAPABILITY, options);
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);

Internet Explorer

Good luck.

Short answer: no

Installing a browser extension is outside of the scope of handling in Selenium.

In Chrome, it displays a modal window that is not "clickable" with Selenium when you want to add a plugin or app. Chrome does not require restarting.

Firefox has the same kind of behaviour to prompt for extension permissions.

You can try something that resides outside of the browser to do what you want. Sikuli might do the trick.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!