BDD 測試語法
概觀
從 Nightwatch 版本 1.3 開始,您可以使用流行的 BDD 介面來編寫測試。您不需要額外設定即可使用 BDD 介面。這些現在已直接支援。
您也可以同時執行以 BDD describe 和 Exports 介面編寫的測試。在此版本之前,您必須使用 Mocha 測試執行器才能啟用此功能,現在無需額外外掛程式或程式庫即可實現。
Nightwatch 中的 BDD 介面提供下列功能
describe()
/context()
test()
/it()
/specify()
before()
after()
beforeEach()
afterEach()
Nightwatch 目前不支援巢狀
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 工作流程。