Nightwatch provides a fluent BDD-style interface for performing assertions on elements, defined on the expect namespace on the main Nightwatch instance(Nightwatch提供了一个流畅的BDD风格的界面,用于对元素进行断言,在主要的Nightwatch实例上的expect命名空间中定义)
this.demoTest = 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;
};
Language Chains
to
be
been
is
that
which
and
has
have
with
at
does
of
.equal(value)/.contain(value)/.match(regex)
These methods will perform assertions on the specified target on the current element. The targets can be an attribute value, the element's inner text and a css property(这些方法将在当前元素的指定目标上执行断言。目标可以是属性值,元素的内部文本和css属性)
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');
};
.startsWith(value)/.endsWith(value)
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)
These methods perform the same thing which is essentially retrying the assertion for the given amount of time (in milliseconds). before or after can be chained to any assertion and thus adding retry capability(这些方法执行相同的操作,基本上是在给定的时间量(以毫秒为单位)重试断言。之前或之后可以链接到任何断言,从而添加重试功能)
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);
};
.a(type [, message])
this.demoTest = function (browser) {
browser.expect.element('#q').to.be.an('input');
browser.expect.element('#q').to.be.an('input', 'Testing if #q is an input');
browser.expect.element('#w').to.be.a('span');
}
.attribute(name [, message])
this.demoTest = function (browser) {
browser.expect.element('body').to.have.attribute('data-attr');
browser.expect.element('body').to.not.have.attribute('data-attr');
browser.expect.element('body').to.not.have.attribute('data-attr', 'Testing if body does not have data-attr');
browser.expect.element('body').to.have.attribute('data-attr').before(100);
browser.expect.element('body').to.have.attribute('data-attr')
.equals('some attribute');
browser.expect.element('body').to.have.attribute('data-attr')
.not.equals('other attribute');
browser.expect.element('body').to.have.attribute('data-attr')
.which.contains('something');
browser.expect.element('body').to.have.attribute('data-attr')
.which.matches(/^something\ else/);
};
.css(property [, message])
this.demoTest = function (browser) {
browser.expect.element('#main').to.have.css('display');
browser.expect.element('#main').to.have.css('display', 'Testing for display');
browser.expect.element('#main').to.not.have.css('display');
browser.expect.element('#main').to.have.css('display').before(100);
browser.expect.element('#main').to.have.css('display').which.equals('block');
browser.expect.element('#main').to.have.css('display').which.contains('some value');
browser.expect.element('#main').to.have.css('display').which.matches(/some\ value/);
};
.enabled
Property that checks if an element is currently enabled.(检查当前是否启用了元素的属)
this.demoTest = function (browser) {
browser.expect.element('#weblogin').to.be.enabled;
browser.expect.element('#main').to.not.be.enabled;
browser.expect.element('#main').to.be.enabled.before(100);
};
.present
Property that checks if an element is present in the DOM.(检查DOM中是否存在元素的属性)
this.demoTest = function (browser) {
browser.expect.element('#main').to.be.present;
browser.expect.element('#main').to.not.be.present;
browser.expect.element('#main').to.be.present.before(100);
};
.selected
Property that checks if an OPTION element, or an INPUT element of type checkbox or radio button is currently selected.(检查当前是否选中OPTION元素或类型为复选框或单选按钮的INPUT元素的属性。)
this.demoTest = function (browser) {
browser.expect.element('#main').to.be.selected;
browser.expect.element('#main').to.not.be.selected;
browser.expect.element('#main').to.be.selected.before(100);
};
.text
this.demoTest = function (browser) {
browser.expect.element('#main').text.to.equal('The Night Watch');
browser.expect.element('#main').text.to.not.equal('The Night Watch');
browser.expect.element('#main').text.to.equal('The Night Watch').before(100);
browser.expect.element('#main').text.to.contain('The Night Watch');
browser.expect.element('#main').text.to.match(/The\ Night\ Watch/);
};
.value
Property that retrieves the value (i.e. the value attributed) of an element. Can be chained to check if contains/equals/matches the specified text or regex(检索元素的值(即属性值)的属性。可以链接以检查是否包含/ equals /匹配指定的文本或正则表达式)
this.demoTest = function (browser) {
browser.expect.element('#q').to.have.value.that.equals('search');
browser.expect.element('#q').to.have.value.not.equals('search');
browser.expect.element('#q').to.have.value.which.contains('search');
browser.expect.element('#q').to.have.value.which.matches(/search/);
};
.visible
Property that asserts the visibility of a specified element.(断言指定元素可见性的属性。)
this.demoTest = function (browser) {
browser.expect.element('#main').to.be.visible;
browser.expect.element('#main').to.not.be.visible;
browser.expect.element('#main').to.be.visible.before(100);
};
Assert
.assert
when an assertion fails, the test ends, skipping all other assertions.(当断言失败时,测试结束,跳过所有其他断言)
.verify
when an assertion fails, the test logs the failure and continues with other assertions.(当断言失败时,测试记录失败并继续其他断言)
Node.js Assert Module Nightwatch.js extends Node.js assert module, so you can also use any of the available methods there in your tests.(Nightwatch.js扩展了Node.js断言模块,因此您还可以在测试中使用任何可用的方法。)
Automatically retrying failed assertions setting the property retryAssertionTimeout (in milliseconds) in the globals file.
Example: retryAssertionTimeout = 2000
Checks if the specified css property of a given element has the expected value.(检查给定元素的指定css属性是否具有期望值)
this.demoTest = function (browser) {
browser.assert.cssProperty("#main", "display", "block");
};
.elementPresent(cssSelector [, msg])
Checks if the given element exists in the DOM(检查DOM中是否存在给定元素)
this.demoTest = function (browser) {
browser.assert.elementPresent("#main");
};
.elementNotPresent(cssSelector [, msg])
Checks if the given element does not exist in the DOM.(检查DOM中是否存在给定元素)
this.demoTest = function (browser) {
browser.assert.elementNotPresent(".should_not_exist");
};
.hidden(cssSelector [, msg])
Checks if the given element is not visible on the page.(检查给定元素是否在页面上不可见)
this.demoTest = function (browser) {
browser.assert.hidden(".should_not_be_visible");
};
.title(expected [, msg])
Checks if the page title equals the given value.(检查页面标题是否等于给定值)
this.demoTest = function (browser) {
browser.assert.title("Nightwatch.js");
};
.urlContains(expectedText [, msg])
Checks if the current URL contains the given value.(检查当前URL是否包含给定值)
this.demoTest = function (browser) {
browser.assert.urlContains('google');
};
.urlEquals(expected [, msg])
this.demoTest = function (browser) {
browser.assert.urlEquals('http://www.google.com');
};
.value(cssSelector, expectedText [, msg])
Checks if the given form element's value equals the expected value.(检查给定表单元素的值是否等于预期值)
this.demoTest = function (browser) {
browser.assert.value("form.login input[type=text]", "username");
};
.valueContains(cssSelector, expectedText [, msg])
Checks if the given form element's value contains the expected value.(检查给定表单元素的值是否包含期望值)
this.demoTest = function (browser) {
browser.assert.valueContains("form.login input[type=text]", "username");
};
.visible(cssSelector [, msg])
Checks if the given element is visible on the page.(检查给定元素是否在页面上可见)
this.demoTest = function (browser) {
browser.assert.visible(".should_be_visible");
};
Page Object API
Page objects provide an additional layer of abstraction for test case creation(页面对象为测试用例创建提供了额外的抽象层)
module.exports = {
// can be string or function
url: function () {
return this.api.launchUrl;
},
elements: {
// shorthand, specifies selector
mySubmitButton: 'input[type=submit]'
// full
myTextInput: {
selector: 'input[type=text]',
locateStrategy: 'css selector'
}
},
commands: [
{
myCustomPause: function () {
this.api.pause(this.props.myPauseTime);
}
}
],
// object version (best considered immutable)
props: {
myPauseTime: 1000
},
sections: {
myFooterSection: {
selector: '#my-footer',
locateStrategy: 'css selector',
elements: {
myLogo: {
selector: '.my-logo',
locateStrategy: 'css selector'
}
},
commands: [
{
myMoveToLogo: function () {
this.moveToElement('@myLogo', this.props.myLogoX, this.props.myLogoY);
}
}
],
// function version (recommended)
props: function () {
return {
myLogoX: 10,
myLogoY: 10
};
},
sections: {
// additional, nested sections
}
}
}
};
Page Object Module(页面对象模块)
Name
Type
description
commands
Array
A list of objects containing functions to represent methods added to the page object instance
commands
Array
包含函数的对象列表,用于表示添加到页面对象实例的方法
elements
Object/Array
An object, or array of objects, of named element definitions to be used as element selectors within element commands called from the page object
elements
Object/Array
命名元素定义的对象或对象数组,用作从页面对象调用的元素命令中的元素选择器
props
Object/Function
An object or a function returning an object representing a container for user variables. Props objects are copied directly into the props property of the page object instance.
props
Object/Function
返回表示用户变量容器的对象的对象或函数。 Props对象被直接复制到页面对象实例的props属性中
sections
Object
An object of named sections definitions defining the sections within the page object.
sections
Object
命名节定义的对象,用于定义页面对象中的节
url
String/Function
A url or function returning a url to be used in a url() command when the page's navigate() method is called.
url
String/Function
调用页面的navigate()方法时返回url()命令中使用的url或函数的url或函数
Page Object Instance
页面对象模块定义用于在调用标准命令API的页面引用中的各自工厂函数时定义页面对象实例
const myPageObject = browser.page.MyPage(); // defined in MyPage.js module