Browser Plugin Testing With Selenium

11,958

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.

Share:
11,958

Related videos on Youtube

Sakamoto Kazuma
Author by

Sakamoto Kazuma

I am an Automation Framework Engineer. I work mainly with Java-Selenium stack, but have worked with cucumberjs, protractor, and a few other tools here and there. In my spare time, I work on a number of PHP applications I've been trying to put together for a few years now. Also interested in getting into mobile development soon.

Updated on September 14, 2022

Comments

  • Sakamoto Kazuma
    Sakamoto Kazuma over 1 year

    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.

  • Uri
    Uri about 9 years
    Hi Rob, I'll appreciate it if you also answer how to do it in Python with Safari, please look at my question - stackoverflow.com/questions/29606672/…
  • Uri
    Uri about 9 years
    Rob, did you see this commit? github.com/SeleniumHQ/selenium/commit/… "All extension related methods in SafariOptions have been deprecated, are no-ops, and will be removed in the next+1 release.", what does it mean?
  • Rob W
    Rob W about 9 years
    @Uri No, I didn't. Thanks for pointing it out. It seems that you have to manually build this, as I have shown in the previous revision of my answer (and it will only work for Safari 6-).
  • Uri
    Uri about 9 years
    Thank you, Rob. Actually we test our extensions with Safari 7 and 8.
  • mosaad
    mosaad almost 7 years
    Any updates if there is an automated way to do it in safari ?