FranAguiar
FranAguiar

Reputation: 647

How to test an Electron app with selenium webdriver

I have read the documentation and I have followed the tutorial step by step and I only have managed to run the app.

The connection with chromedriver I cannot make it work, when I launch the test and try click a simple button I get this:

Error: ChromeDriver did not start within 5000ms at Error (native)
at node_modules/spectron/lib/chrome-driver.js:58:25 at Request._callback (node_modules/spectron/lib/chrome-driver.js:116:45) at Request.self.callback (node_modules/spectron/node_modules/request/request.js:200:22) at Request. (node_modules/spectron/node_modules/request/request.js:1067:10) at IncomingMessage. (node_modules/spectron/node_modules/request/request.js:988:12) at endReadableNT (_stream_readable.js:913:12) at _combinedTickCallback (internal/process/next_tick.js:74:11) at process._tickCallback (internal/process/next_tick.js:98:9)

My code:

"use strict";
require("co-mocha");
var Application = require('spectron').Application;
var assert = require('assert');

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

const driver = new webdriver.Builder()
  .usingServer('http://127.0.0.1:9515')
  .withCapabilities({
    chromeOptions: {
      binary: "./appPath/app"
    }
  })
  .forBrowser('electron')
  .build();

describe('Application launch', function () {
  this.timeout(100000);
  var app;
  beforeEach(function () {
    app = new Application({
      path: "./appPath/app"
    });
    return app.start();
  });

  afterEach(function () {
    if (app && app.isRunning()) {
      return app.stop();
    }
  });

  it('click a button', function* () {
    yield driver.sleep(5000);
    yield driver.findElement(webdriver.By.css(".classSelector")).click();
  });
});

Thanks and sorry for my English.

Upvotes: 2

Views: 18720

Answers (3)

Necmttn
Necmttn

Reputation: 1271

I recommend you to use Spectron. which is a less painful way of testing your electron app. in my opinion perfect combination is using it with Ava test framework, which allows the concurrently test.

async & await is also another big win. which allows you to have so clean test cases.

and also if you have a test which needs to happen serial, you can use test.serial

test.serial('login as new user', async t => {
  let app = t.context.app
  app = await loginNewUser(app)
  await util.screenshotCreateOrCompare(app, t, 'new-user-mission-view-empty')
})
    
test.serial('Can Navigate to Preference Page', async t => {
  let app = t.context.app
  await app.client.click('[data-test="preference-button"]')
  await util.screenshotCreateOrCompare(app, t, 'new-user-preference-page-empty')
})

and just for reference; my helper test cases.

test.before(async t => {
  app = util.createApp()
  app = await util.waitForLoad(app, t)
})

test.beforeEach(async t => {
  t.context.app = app
})

test.afterEach(async t => {
  console.log('test complete')
})
// CleanUp
test.after.always(async t => {
  // This runs after each test and other test hooks, even if they 
 failed
  await app.client.localStorage('DELETE', 'user')
  console.log('delete all files')
  const clean = await exec('rm -rf /tmp/DesktopTest')
  await clean.stdout.on('data', data => {
    console.log(util.format('clean', data))
  })
  await app.client.close()
  await app.stop()
})

util function,

    // Returns a promise that resolves to a Spectron Application once the app has loaded.
    // Takes a Ava test. Makes some basic assertions to verify that the app loaded correctly.
    function createApp (t) {
      return new Application({
        path: path.join(__dirname, '..', 'node_modules', '.bin',
          'electron' + (process.platform === 'win32' ? '.cmd' : '')),

        // args: ['-r', path.join(__dirname, 'mocks.js'), path.join(__dirname, '..')],
        env: {NODE_ENV: 'test'},
        waitTimeout: 10e3
      })
    }

Upvotes: 1

armoucar
armoucar

Reputation: 408

I could get ChromeDriver working by adding a proxy exception in my terminal.

export {no_proxy,NO_PROXY}="127.0.0.1"

Upvotes: 0

Bill Yang
Bill Yang

Reputation: 1413

First off, Spectron (which is a wrapper for WebdriverIO) and WebdriverJS (which is part of Selenium-Webdriver) are two different frameworks, you only need to use one of them for your tests.

If you are using WebdriverJS, then you need to run ./node_modules/.bin/chromedriver in this step: http://electron.atom.io/docs/tutorial/using-selenium-and-webdriver/#start-chromedriver

Upvotes: 1

Related Questions