Abhishek Agarwal
Abhishek Agarwal

Reputation: 722

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.

Upvotes: 2

Views: 2750

Answers (3)

Haythem
Haythem

Reputation: 123

Here's a demo for testing Electron application with Selenium in Python

  1. Install and start ChromeDriver: we need to download ChromeDriver version which matches what our application uses.
  2. Python code: enter image description here

For more details check this

Upvotes: -1

Daniel Aron Goldenberg
Daniel Aron Goldenberg

Reputation: 164

@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/')

Upvotes: 0

Dirk
Dirk

Reputation: 41

    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.

Upvotes: 2

Related Questions