我在网上找到的几乎每一个例子都没有很好地解释如何实现摩卡并与nightwatchJS一起使用。
无论我做什么,我都无法避免该错误消息,即使我遵循official nightwatch how-to use mocha的所有步骤只有我能够做的事情是至少让谷歌Chrome浏览器打开,就是这样。
这是我想要运行的代码
var nightwatch = require('nightwatch');
describe('Google', function() {
var client = nightwatch.initClient({
// Pass here options from nightwatch.json
// selenium logs; change it to true if you want to see them in console
silent : false,
desiredCapabilities: {
browserName: "chrome",
javascriptEnabled: true,
acceptSslCerts: true
}
});
var browser = client.api();
// Mocha timeout
this.timeout(15000);
it('Demo test Google', function (done) {
browser
.url('http://www.google.com')
.waitForElementVisible('body', 1000)
.setValue('input[type=text]', 'nightwatch')
.waitForElementVisible('button[name=btnG]', 1000)
.click('button[name=btnG]')
.pause(1000)
.assert.containsText('#main', 'Night Watch')
.end();
client.start(done);
});
});
这是浏览器弹出后总是发生在我身上的错误消息:
INFO Request: POST /wd/hub/session
- data: {"desiredCapabilities":{"browserName":"firefox","javascriptEnabled":true,"acceptSslCerts":true,"platform":"ANY"}}
- headers: {"Content-Type":"application/json; charset=utf-8","Content-Length":113}
1) Demo test Google
0 passing (15s)
1 failing
1) Google Demo test Google:
Error: timeout of 15000ms exceeded. Ensure the done() callback is being called in this test.
最初的问题表明,没有关于如何使用摩卡设置夜班的好文档。我本周末发现了这种情况,因为我想用摩卡设置我的夜班测试。通过执行以下操作,我能够设置我的夜班测试而不会看到完成的回调错误:
我使用了nightwatch.conf.js,但我相信你也可以在nightwatch.json中执行以下操作:
module.exports = {
src_folders : ["test"],
test_runner: {
type: 'mocha',
options: {
ui: 'bdd',
reporter: 'spec'
}
},
...
}
简单吧?这允许摩卡运行你的夜班测试。更简单,更友好的语法IMO。
以下是我的package.json的相关部分:
{
"name": "nightwatch-mocha",
"directories": {
"test": "test"
},
"scripts": {
"nightwatch": "nightwatch -c ./nightwatch.conf.js"
},
"devDependencies": {
"chromedriver": "^73.0.0",
"mocha": "^6.1.4",
"nightwatch": "^1.0.19"
}
}
我已经安装了chromedriver,所以我可以通过chrome运行测试。
还安装了摩卡和守夜人。
我在scripts对象中创建了一个名为nightwatch的脚本。
当我位于项目的根目录中时,当我从命令行运行npm run nightwatch
时,这将使用mocha运行nightwatch。
此外,使用nightwatch.json或nightwatch.conf.js可以将该配置保留在测试之外 - 您不必在编写新测试套件时初始化该配置,只需执行一次即可完成。
尝试在链的末尾调用done()
回调。我没有完全掌握client.start()
的速度,但我很确定你想要在链条到达终点时完成测试。
var nightwatch = require('nightwatch');
describe('Google', function() {
var client = nightwatch.initClient({
// Pass here options from nightwatch.json
// selenium logs; change it to true if you want to see them in console
silent : false,
desiredCapabilities: {
browserName: "chrome",
javascriptEnabled: true,
acceptSslCerts: true
}
});
var browser = client.api();
// Mocha timeout
this.timeout(15000);
it('Demo test Google', function (done) {
browser
.url('http://www.google.com')
.waitForElementVisible('body', 1000)
.setValue('input[type=text]', 'nightwatch')
.waitForElementVisible('button[name=btnG]', 1000)
.click('button[name=btnG]')
.pause(1000)
.assert.containsText('#main', 'Night Watch')
.end(done);
client.start();
});
});