2

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.

Ratmir Asanov
  • 6,237
  • 5
  • 26
  • 40
Abhishek Agarwal
  • 694
  • 1
  • 9
  • 25

3 Answers3

2
    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
  • 41
  • 4
  • Hi Dirk, "I am getting the error: selenium.common.exceptions.WebDriverException: Message: unknown error: DevToolsActivePort file doesn't exist" My electron version is:7.3.0 and chromedriver version is: 78.0.3904.105. After all the googling around I found solutions to add options as --headless, --no-sandbox etc, nothing worked. Can you help? – Mayur Jadhav Jul 17 '20 at 02:07
0

@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/')
  • Hi Daniel, "I am getting the error: selenium.common.exceptions.WebDriverException: Message: unknown error: DevToolsActivePort file doesn't exist" My electron version is:7.3.0 and chromedriver version is: 78.0.3904.105. After all the googling around I found solutions to add options as --headless, --no-sandbox etc, nothing worked. Can you help? – Mayur Jadhav Jul 17 '20 at 02:11
-1

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

Haythem
  • 43
  • 6
  • Downvoted because a Visual Code screen capture containing almost the same content of a previous text answer is not useful. – Apteryx Jul 06 '22 at 14:34