power_settings_new

How may I test a browser extension with Selenium WebDriver?

by (391 points)
+1 vote
204 views
How may I test a browser extension with Selenium WebDriver?
by (171 points)
which browser extension do you want to test?
by (171 points)
Also specify the name of the browser. Chrome or Firefox?
by (391 points)
It is an extension that is being built for internal use. It will support Chrome, Firefox and Opera.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.

1 Answer

by (171 points)
edited by
0 votes
Chrome:
 
1. Install Your Chrome Extension 
2. Download the Chrome Extension Source Viewer from the Google Web Store. - Using this you can see the source of CRX file.
3. Now you can identify the page you want to test.
4. Use below code to initiate the chrome browser with extension
 
ChromeOptions options = new ChromeOptions ();
File[] files = new File[1];
files[0] = new File("/path/to/extension.crx");
options.addExtensions(files);
DesiredCapabilities capabilities = new DesiredCapabilities ();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
ChromeDriver driver = new ChromeDriver(capabilities);
 
5. Now you can navigate to extension page
driver.get('chrome-extension://UNIQUEID/SPECIFICPAGE.html')
 
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);
 
 
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);
 
 
Hope that helps!
add
...