来自守夜人的摩卡断言.包含文本失败



这是很新的。 运行">从夜巡使用摩卡">示例 http://nightwatchjs.org/guide#using-mocha,逐字测试 - 它失败了。 想不通为什么。

浏览器启动,"夜巡"被输入谷歌搜索。

$ npm test
> jbserver@0.0.101 test /var/www/html/3jbserver
> node ./node_modules/nightwatch/bin/nightwatch -c ./test/nightwatch.json ./test/google.test.js
1) Google demo test for Mocha with Nightwatch uses BDD to run the Google simple test
0 passing (8s)
1 failing
1) Google demo test for Mocha with Nightwatch uses BDD to run the Google simple test:
Testing if element <#main> contains text: "Night Watch". - Expected "Night Watch" but got: ""
at Context.<anonymous> (test/google.test.js:30:17)
at Context.<anonymous> (test/google.test.js:20:7)
npm ERR! Test failed.  See above for more details.

在夜巡.json...

...
"test_runner" : {
"type" : "mocha",
"options" : {
"ui" : "bdd",
"reporter" : "list"
}
}
...

google.test.js正是给定的例子

...
it('uses BDD to run the Google simple test', function(client) {
client
.url('http://google.com')
.expect.element('body').to.be.present.before(1000);
client.setValue('input[type=text]', ['nightwatch', client.Keys.ENTER])
.pause(1000)
.assert.containsText('#main', 'Night Watch');
});
...

包.json

"scripts": {
"test": "node ./node_modules/nightwatch/bin/nightwatch -c ./test/nightwatch.json ./test/google.test.js"
...

运行与(npm)硒独立运行。

当你使用像 #main 这样通用的东西时,测试往往会中断(这是谷歌的整个页面,有点多)。Nightwatch中的文档可能会更好一些。

你可以这样做,看看守夜人是如何工作的。我使用了更具体的选择器。

.assert.containsText('#rhs_title span', 'Night Watch');

这将选择 #rhs_title 元素内的span,并检查它是否包含"夜巡

"。尽量避免.pause,而是使用.waitForElementVisible(time, 'element').暂停将强制暂停 1000 毫秒,而.waitForElementVisible将等待最多1000 毫秒,但如果找到该元素,测试将继续。这节省了宝贵的时间。

它一起看起来像:

.waitForElementVisible(1000, '#rhs_title span')
.assert.containsText('#rhs_title span', 'Night Watch');

将此用作测试设置

"test_settings" : {
"default" : {
"launch_url" : "http://localhost",
"selenium_port"  : 4444,
"selenium_host"  : "localhost",
"test_runner" : {
"type" : "mocha",
"options" : {
"ui" : "bdd",
"reporter" : "list"
}
},
"desiredCapabilities": {
"browserName": "chrome",
"javascriptEnabled": true,
"acceptSslCerts": true
}
}
}

现在用于断言

it('uses BDD to run the application test', function(client) {
client
.url('url')
.expect.element('body').to.be.present.before(1000);
client.setValue('input[type=text]', ['ranjan', client.Keys.ENTER])
.pause(1000)
.assert.containsText('#main', 'ranjan');
});

这段代码为我唤醒了,请将网址更改为谷歌

最新更新