如何使用selenium webdriver测试电子应用程序

时间:2016-06-22 13:09:16

标签: selenium electron e2e-testing

我已经阅读了文档,并且我已经按照教程一步一步地进行了操作,并且我只能设法运行该应用程序。

与chromedriver的连接我不能使它工作,当我启动测试并尝试点击一个简单的按钮我得到这个:

  

错误:ChromeDriver在错误(本机)的5000毫秒内没有启动   在node_modules / spectron / lib / chrome-driver.js:58:25 at   Request._callback(node_modules / spectron / lib / chrome-driver.js:116:45)   在Request.self.callback   (node_modules / spectron / node_modules / request / request.js:200:22)at   请求。   (node_modules / spectron / node_modules / request / request.js:1067:10)at   IncomingMessage。   (node_modules / spectron / node_modules / request / request.js:988:12)at   _combinedTickCallback的endReadableNT(_ stream_readable.js:913:12)   (internal / process / next_tick.js:74:11)在process._tickCallback   (内部/过程/ next_tick.js:98:9)

我的代码:

"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();
  });
});

谢谢,对不起我的英语。

4 个答案:

答案 0 :(得分:0)

首先,Spectron(它是WebdriverIO的包装)和WebdriverJS(它是Selenium-Webdriver的一部分)是两个不同的框架,你只需要使用其中一个进行测试。

如果您使用的是WebdriverJS,则需要在此步骤中运行./node_modules/.bin/chromedriverhttp://electron.atom.io/docs/tutorial/using-selenium-and-webdriver/#start-chromedriver

答案 1 :(得分:0)

我可以通过在终端中添加代理例外来使ChromeDriver正常工作。

export {no_proxy,NO_PROXY}="127.0.0.1"

答案 2 :(得分:0)

首先,我得到了同样的错误“ChromeDriver没有在5000毫秒内启动”。

我用

设置了no_proxy
set no_proxy,NO_PROXY="127.0.0.1,localhost"

现在我又收到了另一个错误:

1) context local project "before all" hook:
     Error: Timeout of 40000ms exceeded. For async tests and hooks, ensure "done()" is called; if returning a Promise, ensure it resolves.

奇怪的是它在我的机器上不起作用,但在其他机器上这些测试运行正常! 我可以看到ChromeDriver的过程是在任务管理器中启动的。

答案 3 :(得分:0)

我建议您使用Spectron。这是一种测试电子应用程序的痛苦方法。在我看来,完美的组合是将它与Ava测试框架一起使用,它允许同时测试。

async & await也是另一个重大胜利。这使您可以拥有如此干净的测试用例。

如果您的测试需要连续发生,您可以使用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')
})

仅供参考;我的帮助测试用例。

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函数,

    // 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
      })
    }