Expect API
概述
Nightwatch 提供了一个流畅的 BDD 风格接口,用于对元素执行断言,该接口定义在主 Nightwatch 实例的 expect
命名空间上。它基于 Chai Expect 断言库,并提供更高的灵活性,还在经典 assert
接口之上增加了新的功能。
它使用可链接的语言来构建断言,该断言由使用 CSS/XPath 选择器指定的元素指定。一个简单的示例如下所示
javascript
describe('expect example', function() {
it('sample test', function (browser) {
// start with identifying the element
// and then assert the element is present
browser.expect.element('#main').to.be.present;
// or assert the element is visible
browser.expect.element('#main').to.be.visible;
});
}
语言链
以下内容作为可链接的 getter 提供,以提高断言的可读性。它们不提供测试功能,顺序也不重要。
- to
- be
- been
- is
- that
- which
- and
- has
- have
- with
- at
- does
- of
.equal(value
)/.contain(value
)/.match(regex
)
这些方法将在当前元素上的指定目标上执行断言。目标可以是属性值、元素的内部文本和 CSS 属性。
javascript
this.demoTest = function (browser) {
browser.expect.element('#main').text.to.equal('The Night Watch');
browser.expect.element('#main').text.to.contain('The Night Watch');
browser.expect.element('#main').to.have.css('display').which.equals('block');
};
.startWith(value
)/.endWith(value
)
与 equal
/ contain
/ match
相同。
this.demoTest = function (browser) {
browser.expect.element('#main').text.to.endWith('Watch');
browser.expect.element('#main').text.to.startWith('The');
};
.not
否定链中任何后续断言。
this.demoTest = function (browser) {
browser.expect.element('#main').text.to.not.equal('The Night Watch');
browser.expect.element('#main').text.to.not.contain('The Night Watch');
browser.expect.element('#main').to.have.css('display').which.does.not.equal('block');
};
.before(ms
)/.after(ms
)
这些方法执行相同的事情,本质上是为给定时间量(以毫秒为单位)重试断言。before
或 after
可以链接到任何断言,从而添加重试功能。
可以通过在 nightwatch.json
或外部全局文件中定义全局属性来更改轮询间隔(以毫秒为单位)。类似地,默认超时可以作为全局 waitForConditionTimeout
属性(以毫秒为单位)指定。
this.demoTest = function (browser) {
browser.expect.element('#main').text.to.contain('The Night Watch').before(1000);
browser.expect.element('#main').text.to.not.contain('The Night Watch').after(500);
};
<%- include('./_cookie.md') %>
expect.element()
断言
- to.be.active
- to.have.attribute()
- to.have.css()
- to.be.enabled
- to.be.present
- to.be.property
- to.be.selected
- to.have.text()
- to.be.a()
- to.have.value()
- to.be.visible
expect.elements()
断言
<%- include('./_title.md') %> <%- include('./_url.md') %>