グローバルテストフック
テストスイートごとの同じフックセットは、テストの範囲外であるグローバルでも利用できます。グローバルフックの場合、beforeEach
とafterEach
はテストスイート(つまり、テストファイル)を参照し、テストスイートの前後に実行されます。
グローバル before[Each] および after[Each]
テストランナーを開始する前と、すべてのテストの実行が完了した直後の終了前に操作を実行できるグローバルbefore
およびafter
[非同期]メソッドを使用することもできます。
同様に、グローバルbeforeEach
およびafterEach
は、各テストスイート(つまり、テストファイル)の前後に呼び出されます。これらは、Nightwatch browser
オブジェクトを受け取ります。
これらのメソッドは、外部のglobals
ファイルで定義され、コンテキストとしてglobals
オブジェクトを使用して呼び出されます。callback
は渡される唯一の引数であり、操作が完了したら必ず呼び出す必要があります。
例
module.exports = {
'default' : {
isLocal : true,
},
'integration' : {
isLocal : false
},
// External before hook is ran at the beginning of the tests run, before creating the Selenium session
before(done) {
// run this only for the local-env
if (this.isLocal) {
// start the local server
App.startServer(function() {
// server listening
done();
});
} else {
done();
}
},
// External after hook is ran at the very end of the tests run, after closing the Selenium session
after(done) {
// run this only for the local-env
if (this.isLocal) {
// stop the local server
App.stopServer(function() {
// shutting down
done();
});
} else {
done();
}
},
// This will be run before each test suite is started
beforeEach(browser, done) {
// getting the session info
browser.status(function(result) {
console.log(result.value);
done();
});
},
// This will be run after each test suite is finished
afterEach(browser, done) {
console.log(browser.currentTest);
done();
},
// Called right after the command .navigateTo() is finished
async onBrowserNavigate(browser) {
return Promise.resolve();
},
// Called right before the command .quite() is finished
async onBrowserQuit(browser) {
return Promise.resolve();
};