3

I want to test one extension on different browser versions using BrowserStack. This is a function that returns driver with specified capabilities. I have a .crx file for Chrome and an .xpi file for Firefox on my local machine. I want to use Remote Webdriver with a corresponding extension installed, using Python.

def my_webdriver(browser, browser_version, os, os_version):
    caps = {}
    caps["browser"] = browser
    caps["browser_version"] = browser_version
    caps["os"] = os
    caps["os_version"] = os_version
    caps["browserstack.debug"] = "true"
    driver = webdriver.Remote(
    ¦   command_executor = 'blahblahblah',
    ¦   desired_capabilities = caps)
    driver.maximize_window()
    return driver
serv-inc
  • 35,772
  • 9
  • 166
  • 188
Dhiraj
  • 481
  • 6
  • 13

2 Answers2

2

For Firefox, you need to create a profile and add your extension to it using add_extension. Then you pass the profile to the WebDriver constructor:

from selenium.webdriver.remote.webdriver import WebDriver as RemoteWebDriver
selenium.webdriver.firefox.firefox_profile import FirefoxProfile
...
fp = FirefoxProfile()
fp.add_extension('/path/to/your/extension.xpi')
driver = RemoteWebDriver(..., browser_profile=fp)

Alternatively, you can create a Firefox profile in advance, and manually add your extenstion to it. Later you pass its path as parameter to FirefoxProfile()

fp = FirefoxProfile('/path/to/your/profile')

For Chrome, use ChromeOptions:

from selenium.webdriver.chrome.options import Options as ChromeOptions
chrome_options = ChromeOptions()
chrome_options.add_extension('/path/to/your/extension.crx')
driver = RemoteWebDriver(..., desired_capabilities = caps + chrome_options.to_capabilities())
Community
  • 1
  • 1
E.Z.
  • 6,393
  • 11
  • 42
  • 69
  • 1
    When I try it with Chrome, I get this exception: `TypeError: unsupported operand type(s) for +: 'dict' and 'dict'` when running `desired_capabilities=caps + chrome_options.to_capabilities()`. – Uri Feb 23 '15 at 11:14
0

E.Z.'s answer for chrome works if you use caps.update:

from selenium.webdriver.chrome.options import Options as ChromeOptions
chrome_options = ChromeOptions()
chrome_options.add_extension('/path/to/your/extension.crx')
caps.update(chrome_options.to_capabilities())
driver = RemoteWebDriver(..., desired_capabilities=caps)
serv-inc
  • 35,772
  • 9
  • 166
  • 188