テストグローバル
概要
Nightwatchは、テストスイート間およびグローバルテストフック定義間のデータ永続性を、テストグローバルを通じてサポートします。最もシンプルな形式では、設定ファイルで定義される名前と値のペアの辞書です。
グローバルは、設定ファイル内の"globals"
プロパティとして、または"globals_path"
プロパティとして指定された外部ファイルとして定義できます。
例
以下は、nightwatch.json
設定ファイルでglobals
プロパティを使用する定義の例です。
nightwatch.json
{
"src_folders": [],
"globals": {
"myGlobalVar" : "some value",
"otherGlobal" : "some other value"
},
"test_settings": {
"default": {
"launch_url": "https://nightwatch.dokyumento.jp",
}
}
}
globals
オブジェクトは、テストに渡され、browser.globals
経由で利用可能なNightwatch APIオブジェクトに直接ロードされます。
sampleTest.js
describe('test globals example', function() {
it('Demo test', function(browser) {
console.log(browser.globals.myGlobalVar); // myGlobalVar == "some value"
});
})
外部テストグローバル
テストグローバルは、以下のように設定ファイルでglobals_path
設定によって指定された外部ファイルでも定義できます。
nightwatch.json
{
"src_folders": [],
"globals_path": "lib/globals.js",
"test_settings": {
"default": {
"launch_url": "https://nightwatch.dokyumento.jp"
}
}
外部グローバルファイルには、以下を含めることもできます。
- グローバルテストフック
- カスタムレポーター
- テスト固有の設定
事前定義済みグローバル
以下のglobals
を使用して、テストランナーの動作を制御でき、以下に示すデフォルト値で定義されています。
これらは2つの方法で定義できます。
globals_path
設定プロパティで指定された外部グローバルファイル内(例:lib/globals.js
)nightwatch.conf.js
設定ファイル内で直接
lib/globals.js
module.exports = {
// this controls whether to abort the test execution when an assertion failed and skip the rest
// it's being used in waitFor commands and expect assertions
abortOnAssertionFailure: true,
// this will overwrite the default polling interval (currently 500ms) for waitFor commands
// and expect assertions that use retry
waitForConditionPollInterval: 500,
// default timeout value in milliseconds for waitFor commands and implicit waitFor value for
// expect assertions
waitForConditionTimeout : 5000,
// since 1.4.0 – this controls whether to abort the test execution when an element cannot be located; an error
// is logged in all cases, but this also enables skipping the rest of the testcase;
// it's being used in element commands such as .click() or .getText()
abortOnElementLocateError: false,
// this will cause waitFor commands on elements to throw an error if multiple
// elements are found using the given locate strategy and selector
throwOnMultipleElementsReturned: false,
// By default a warning is printed if multiple elements are found using the given locate strategy
// and selector; set this to true to suppress those warnings
suppressWarningsOnMultipleElementsReturned: false,
// controls the timeout value for async hooks. Expects the done() callback to be invoked within this time
// or an error is thrown
asyncHookTimeout : 10000,
// controls the timeout value for when running async unit tests. Expects the done() callback to be invoked within this time
// or an error is thrown
unitTestsTimeout : 2000,
// controls the timeout value for when executing the global async reporter. Expects the done() callback to be
// invoked within this time or an error is thrown
customReporterCallbackTimeout: 20000,
// Automatically retrying failed assertions - You can tell Nightwatch to automatically retry failed assertions
// until a given timeout is reached, before the test runner gives up and fails the test.
retryAssertionTimeout: 5000,
// use the same browser session to run the individual test suites
reuseBrowserSession: false,
// Custom reporter
reporter: function(results, done) {
// do something with the results
done(results);
},
// External before hook is ran at the beginning of the tests run, before creating the Selenium session
before(done) {
done();
},
// External after hook is ran at the very end of the tests run, after closing the Selenium session
after(done) {
done();
},
// This will be run before each test suite is started
beforeEach(browser, done) {
done();
},
// This will be run after each test suite is finished
afterEach(browser, done) {
done();
},
// Called right after the command .navigateTo() is finished
async onBrowserNavigate(browser) {
return Promise.resolve();
},
// Called right before the command .quit() is finished
async onBrowserQuit(browser) {
return Promise.resolve();
}
}
環境固有のグローバル
他のテスト設定と同様に、globals
はテスト環境ごとに上書きする機能を持っています。
この設定を検討してください。
nightwatch.json
{
"src_folders": [],
"test_settings": {
"default": {
"launch_url": "https://nightwatch.dokyumento.jp",
"globals": {
"myGlobalVar" : "some value",
"otherGlobal" : "some other value"
}
},
"integration": {
"globals": {
"myGlobalVar" : "integrated global"
}
}
}
}
非常に基本的なテストでこれを試してみましょう。
sampleTest.js
module.exports = {
'Demo test' : function (browser) {
console.log('myGlobalVar is: "', browser.globals.myGlobalVar, '"');
}
};
ランナーに--env integration
オプションを渡します。
npx nightwatch --env integration
すると、グローバルオブジェクトは次のようになります。
myGlobalVar is: "integrated global"