Home > Net >  How do I get selenium to open my electron app?
How do I get selenium to open my electron app?

Time:05-14

I'm attempting to setup Selenium to test my electron app. So far I have the code from the Electron documentation, however, this only opens the HTML file, not my actual app. Here is my code:

import * as path from "path";

const webdriver = require('selenium-webdriver');

const driver = new webdriver.Builder()
    // The "9515" is the port opened by ChromeDriver.
    .usingServer('http://localhost:9515')
    .withCapabilities({
        'chromeOptions': {
            // Here is the path to your Electron binary.
            binary: '../../node_modules/electron/dist/electron.exe'
        }
    })
    .forBrowser('chrome')
    .build();

const current_path = path.join(__dirname, "../../dist/public/index.html");

driver.get('file://'   current_path);

How can I get it to open my app?

I've been trying to figure this out for several days, and all the other articles and stackoverflow posts either point to Spectron (which is now deprecated) or don't explain how to open my own app and not a different website.

EDIT: I figured out that adding "args": ['--app=PATH_TO_PROJECT_DIR')] in the chromeOptions object , and changing the name to "goog:chromeOptions", makes it open to the correct file path, however, now I get the following error:

WebDriverError: unknown error: no chrome binary at ../../node_modules/electron/dist/electron.exe

Changing back to "chromeOptions" stops it from opening the app directory, but does open a blank chrome window.

CodePudding user response:

The solution was to:

  1. Replace '../../node_modules/electron/dist/electron.exe' with path.join(__dirname, '../../node_modules/electron/dist/electron.exe')
  2. To change chromeOptions to goog:chromeOptions
  3. To add "args": [path.join(__dirname, '../../')] to the goog:chromeOptions object.

Working Code:

import * as path from "path";

const webdriver = require('selenium-webdriver');

const driver = new webdriver.Builder()
    // The "9515" is the port opened by ChromeDriver.
    .usingServer('http://localhost:9515')
    .withCapabilities({
        'goog:chromeOptions': {
            // Here is the path to your Electron binary.
            "binary": path.join(__dirname, '../../node_modules/electron/dist/electron.exe'),
            "args": ['--app='   path.join(__dirname, "../../")]
        }
    })
    .forBrowser('chrome')
    .build();
  • Related