简体   繁体   中英

Using Python + Selenium for Electron-based app testing

There is a lot of documentation on using Spectron for testing applications built using Electron.

As I have a lot of utilities written in Python, I would like to know if there is any way to use Python-Selenium for testing apps built in Electron.

From a few online resources, I found a few people were able to do it (though not the latest version, which I am using currently). I was able to launch the app using below code, but the call webdriver.Chrome() is a blocking call, and I never get the driver instance:

from selenium import webdriver
options = webdriver.ChromeOptions()
options.binary_location = "/home/username/electron_test/node_modules/electron/dist/electron"

options.add_argument("--app=/home/username/electron_test/")
driver = webdriver.Chrome(chrome_options=options)

Thanks.

    from selenium import webdriver

    # Start the web driver
    web_driver_path = os.path.join(
        os.environ["ProgramFiles(x86)"],
        "chromedriver-v3.1.9-win32-x64",
        "chromedriver.exe")
    service = webdriver.chrome.service.Service(web_driver_path)
    service.start()

    # start the app
    self.web_driver = webdriver.remote.webdriver.WebDriver(
        command_executor=service.service_url,
        desired_capabilities={
            'browserName': 'chrome',
            'goog:chromeOptions': {
                'args': [],
                'binary': PATH_TO_BINARY_APP,
                'extensions': [],
                'windowTypes': ['webview']},
            'platform': 'ANY',
            'version': ''},
        browser_profile=None,
        proxy=None,
        keep_alive=False)

First you need to create a service instance for the webdriver. After that, open the electron app with the service url, so they can connect to each other.

Be sure to use the right web driver version matching your electron version.

FYI: When you use something like webviews in your app, you'll love the "windowTypes" line. Took me some hours to figure out.

@dirk answer is right! the goog:chromeOptions is the trick. Thanks!

Here what I ended up with:

from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

cap = DesiredCapabilities.CHROME.copy()
cap['goog:chromeOptions'] = {'binary': PATH_TO_BINARY_APP}
driver = webdriver.Remote(
    command_executor='http://127.0.0.1:4444/wd/hub',
    desired_capabilities=cap)

driver.get('http://google.com/')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM