Expect API
概要
Nightwatchは、主要なNightwatchインスタンスのexpect名前空間に定義された要素に対するアサーションを実行するための、流暢なBDDスタイルのインターフェースを提供します。これはChai Expectアサーションライブラリに基づいており、より高い柔軟性を提供し、従来のassertインターフェースよりも新しい機能を追加しています。
これは、css/xpathセレクタで指定された要素を指定してアサーションを構築する、チェーン可能な言語を使用しています。簡単な例を以下に示します。
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;
});
}言語チェーン
アサーションの可読性を向上させるために、チェーン可能なゲッターとして以下のものが提供されています。これらはテスト機能を提供せず、順序は重要ではありません。
- to
- be
- been
- is
- that
- which
- and
- has
- have
- with
- at
- does
- of
.equal(value)/.contain(value)/.match(regex)
これらのメソッドは、現在の要素の指定されたターゲットに対してアサーションを実行します。ターゲットは、属性値、要素の内部テキスト、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');
};.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または外部グローバルファイルでグローバルプロパティとして、waitForConditionPollIntervalプロパティ(ミリ秒単位)を定義することで、ポーリング間隔を変更できます。同様に、デフォルトのタイムアウトは、グローバル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') %>