如何使用 selenium webdriver 测试 Electron 应用程序
How to test an Electron app with selenium webdriver
我已阅读文档并按照教程一步步操作,但我只成功 运行 应用程序。
与 chromedriver 的连接我无法使其工作,当我启动测试并尝试单击一个简单的按钮时,我得到了这个:
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)
我的代码:
"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();
});
});
谢谢,对不起我的英语。
首先,Spectron(WebdriverIO 的包装器)和 WebdriverJS(Selenium-Webdriver 的一部分)是两个不同的框架,您只需要使用其中一个来进行测试。
如果你使用的是WebdriverJS,那么你需要运行./node_modules/.bin/chromedriver
这一步:http://electron.atom.io/docs/tutorial/using-selenium-and-webdriver/#start-chromedriver
我可以通过在终端中添加代理异常来让 ChromeDriver 正常工作。
export {no_proxy,NO_PROXY}="127.0.0.1"
我推荐你使用Spectron. which is a less painful way of testing your electron app. in my opinion perfect combination is using it with 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
})
}
我已阅读文档并按照教程一步步操作,但我只成功 运行 应用程序。
与 chromedriver 的连接我无法使其工作,当我启动测试并尝试单击一个简单的按钮时,我得到了这个:
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)
我的代码:
"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();
});
});
谢谢,对不起我的英语。
首先,Spectron(WebdriverIO 的包装器)和 WebdriverJS(Selenium-Webdriver 的一部分)是两个不同的框架,您只需要使用其中一个来进行测试。
如果你使用的是WebdriverJS,那么你需要运行./node_modules/.bin/chromedriver
这一步:http://electron.atom.io/docs/tutorial/using-selenium-and-webdriver/#start-chromedriver
我可以通过在终端中添加代理异常来让 ChromeDriver 正常工作。
export {no_proxy,NO_PROXY}="127.0.0.1"
我推荐你使用Spectron. which is a less painful way of testing your electron app. in my opinion perfect combination is using it with 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
})
}