全域測試鉤子
與每個測試套件相同的鉤子集也可在測試範圍之外的全域範圍內使用。在全域鉤子的情況下,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();
};