1、配置自定义断言的文件路径
修改文件:nightwatch.json
增加:
"custom_assertions_path" : ["tests/custom-assertions"],
2、创建自定义断言的文件夹
tests/custom-assertions
3、自定义断言文件
elementCount.js
文件内容如下:
exports.assertion = function (selector, count) {
this.message = 'Testing if element <' + selector + '> has count: ' + count
this.expected = count
this.pass = function (val) {
return val === this.expected
}
this.value = function (res) {
return res.value
}
this.command = function (cb) {
var self = this
return this.api.execute(function (selector) {
return document.querySelectorAll(selector).length
}, [selector], function (res) {
cb.call(self, res)
})
}
}
4、使用断言:
直接在测试文件中使用即可, 不用引用
browser.assert.elementCount('span',1)
经验证, 可以使用。