Browser Plugin Testing With Selenium
Asked Answered
I

2

17

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.

Insolate answered 21/2, 2013 at 15:21 Comment(0)
W
40

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.

Watermark answered 14/6, 2013 at 21:56 Comment(5)
Hi Rob, I'll appreciate it if you also answer how to do it in Python with Safari, please look at my question - #29607172Kutaisi
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?Kutaisi
@Kutaisi 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-).Watermark
Thank you, Rob. Actually we test our extensions with Safari 7 and 8.Kutaisi
Any updates if there is an automated way to do it in safari ?Qr
B
-4

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.

Biafra answered 28/2, 2013 at 14:45 Comment(2)
You can do extension testing using Selenium. Extensions are a combination of HTML, CSS and JS. So you can handle those with SeleniumTetrapterous
You can add an extension locally by using var options = new chrome.Options(); options.addExtensions("<local file name"); var browser = new webdriver.Builder(). withCapabilities(options.toCapabilities()).build(); // you can then use this as any other html pageFascia

© 2022 - 2024 — McMap. All rights reserved.