BDDテスト構文
概要
Nightwatch バージョン1.3からは、人気のBDDインターフェースを使用してテストを作成できます。BDDインターフェースを使用するために追加の設定は必要ありません。これらはすぐに使用できます。
BDDの`describe`とExportsインターフェースで記述されたテストを同時に実行することもできます。以前のバージョンでは、この機能を有効にするにはMochaテストランナーを使用する必要がありましたが、現在は追加のプラグインやライブラリなしで可能です。
NightwatchのBDDインターフェースでは、次の関数が提供されています。
describe()
/context()
test()
/it()
/specify()
before()
after()
beforeEach()
afterEach()
describe
/context
宣言をサポートしていません。テストスイートの名前を定義するには、describe
のみを使用できます。例
describe('Ecosia', function() {
// test() and specify() is also available
it('demo test', function(browser) {
browser
.url('https://www.ecosia.org/')
.setValue('input[type=search]', 'nightwatch')
.click('button[type=submit]')
.assert.containsText('.mainline-results', 'Nightwatch.js')
.end();
});
});
import {NightwatchTests} from 'nightwatch';
const Ecosia: NightwatchTests = {
'demo test': () => {
browser
.url('https://www.ecosia.org/')
.setValue('input[type=search]', 'nightwatch')
.click('button[type=submit]')
.assert.containsText('.mainline-results', 'Nightwatch.js')
.end();
}
};
export default Ecosia;
通常のBDD構文に加えて、Nightwatchは独自の動作を定義するためのいくつかの方法を提供しています。
テストスイート固有の機能
describe('homepage test with describe', function() {
// testsuite specific capabilities
this.desiredCapabilities = {
browserName: 'firefox'
};
it('...', function() {...});
});
テストスイート固有のタグ
describe('homepage test with describe', function() {
// defining tags using bdd
this.tags = ['login', 'authentication''];
it('...', function() {...});
});
テストスイート固有のリトライ
describe('homepage test with describe', function() {
// how many time to retry a failed testcase inside this test suite
this.retries(3);
// how many times to retry the current test suite in case of an assertion failure or error
this.suiteRetries(2);
it('...', function() {...});
});
完全なBDD構文
設定の取得
現在設定されているすべての設定は、this.settings
から利用できます。
describe('homepage test with describe', function() {
console.log('Settings', this.settings);
it('...', function() {
// ...
});
});
必要な機能
テストスイート固有の機能。
describe('homepage test with describe', function() {
this.desiredCapabilities = {};
it('...', function() {
// ...
});
});
単体テスト
現在のテストがユニット/統合テストである場合(つまり、Webdriverセッションは作成されません)、これを有効にします。
describe('homepage test with describe', function() {
this.unitTest = true;
it('...', function() {
// ...
});
});
失敗時のセッションの終了
失敗またはエラーが発生した場合にブラウザーウィンドウを開いたままにしておきたい場合は、これをfalse
に設定します(デバッグに役立ちます)。
describe('homepage test with describe', function() {
this.endSessionOnFail = false
it('...', function() {
// ...
});
});
失敗時の残りのテストケースのスキップ
アサーションの失敗/エラーが発生した場合でも、残りのテストケース/テストステップを実行したい場合は、これをfalse
に設定します。
describe('homepage test with describe', function() {
this.skipTestcasesOnFail = true
it('...', function() {
// ...
});
});
テストスイートの無効化/スキップ
テストランナーによってこのテストスイートをスキップしたい場合は、これをtrueに設定します。
describe('homepage test with describe', function() {
this.disabled = true
it('...', function() {
// ...
});
});
リトライ
describe('homepage test with describe', function() {
this.retries(3);
this.suiteRetries(2);
it('...', function() {
// ...
});
});
アサーションタイムアウトの制御
要素が配置されるまで、またはアサーションが合格するまでのアサーションと要素コマンドのタイムアウトを制御します。
describe('homepage test with describe', function() {
this.timeout(1000)
it('...', function() {
// ...
});
});
ポーリング間隔の制御
アサーションまたは要素コマンドのリトライ間のポーリング間隔を制御します。
describe('homepage test with describe', function() {
this.retryInterval(100);
it('...', function() {
// ...
});
});
タグの定義
このテストスイートのタグを定義します。
describe('homepage test with describe', function() {
this.tags = ['login']
it('...', function() {
// ...
});
});
テスト関数とフック
describe('homepage test with describe', function() {
before(function(browser) {
this.homepage = browser.page.home();
});
it('startHomepage', () => {
this.homepage.navigate();
this.homepage.expect.section('@indexContainer').to.be.not.visible;
});
// Run only this testcase
//*
it.only('startHomepage', () => {
this.homepage.navigate();
});
*//
// skipped testcase: equivalent to: test.skip(), it.skip(), and xit()
xtest('async testcase', async browser => {
const result = await browser.getText('#navigation');
console.log('result', result.value)
});
test('version dropdown is enabled', browser => {
const navigation = this.homepage.section.navigation;
const navbarHeader = navigation.section.navbarHeader;
navbarHeader.expect.element('@versionDropdown').to.be.enabled;
});
after(browser => browser.end());
});
例となるGitHubリポジトリ
いくつかの例を含む完全なGitHubテンプレートリポジトリを用意しており、定期的に更新しています。GitHub Actionsワークフローも含まれており、すぐに始めることができます。