使用测试全局变量
Nightwatch 提供的另一个有用概念是测试全局变量。最简单的形式,它是在配置文件中定义的键值对字典。
全局变量可以定义为 "globals"
属性,也可以定义为外部文件,该文件指定为 "globals_path"
属性。
以下是使用 nightwatch.json
中的 "globals"
属性的示例定义。
nightwatch.conf.js
{
"src_folders": [],
"globals": {
"myGlobalVar" : "some value",
"otherGlobal" : "some other value"
},
"test_settings": {
"default": {
"launch_url": "https://nightwatch.node.org.cn",
}
}
}
与 launch_url
属性类似,globals
对象直接在传递给测试的 Nightwatch api 上可用。
tests/sampleTest.js
module.exports = {
'Demo test' : function (browser) {
console.log(browser.globals.myGlobalVar); // myGlobalVar == "some value"
}
};
预定义全局变量
以下全局属性可用于控制测试运行器的行为,并使用以下默认值定义。
nightwatch/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,
// Custom reporter
reporter: function(results, done) {
// do something with the results
done(results);
}
}
环境特定全局变量
与其他测试设置类似,全局变量可以被每个测试环境覆盖。考虑以下配置。
nightwatch.json
{
"src_folders": [],
"test_settings": {
"default": {
"launch_url": "https://nightwatch.node.org.cn",
"globals": {
"myGlobalVar" : "some value",
"otherGlobal" : "some other value"
}
},
"integration": {
"globals": {
"myGlobalVar" : "integrated global"
}
}
}
}
如果我们仍然将 --env integration
选项传递给运行器,那么我们的全局变量对象将如下所示。
nightwatch --env integration
module.exports = {
'Demo test' : function (browser) {
console.log(browser.globals.myGlobalVar); // myGlobalVar == "integrated global"
}
};