Browser Plugin Testing With Selenium

Sakamoto Kazuma picture Sakamoto Kazuma · Feb 21, 2013 · Viewed 11.7k times · Source

I am writing a webapp that has a browser plugin component for both firefox and chrome. My current testing system uses a series of Selenium tests created through Selenium IDE.

Is it possible to also have selenium install, activate, and delete browser plugins for firefox and chrome (possibly other browsers as well)?

I think the biggest concern is that installing/enabling the browser plugin requires a browser restart, and I'm not sure if that would through selenium off.

The acquisition of the plugin is easily handled by visiting an internal site-link to a php-script that detects your browser.

Answer

Rob W picture Rob W · Jun 14, 2013

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.