我有一个奇怪的问题(至少对我来说),其中在nightwatch.conf.js中分配了默认值并随后在页面对象命令中更改其值的全局变量随后将其值恢复为原始值作为参数传递给后续页面对象命令时的值。令人困惑?有关完整的测试用例和输出,请参见下文。
有人可以向我解释1)为什么会发生这种情况,以及2)我怎样才能实现自己的目标(能够传递值,在这种情况下是通过页面抓取方式更新的全局值)上一个页面对象命令中的值,转换为页面对象命令并使其实际使用该更新值)?
谢谢!
Butch
nightwatch.conf.js
/* eslint-env es6 */ /* jshint esversion: 6 */
const seleniumServer = require("selenium-server");
const chromedriver = require("chromedriver");
// we use a nightwatch.conf.js file so we can include comments and helper functions
module.exports = {
"src_folders": [
"tests/e2e"// Where you are storing your Nightwatch e2e tests
],
"page_objects_path": "tests/page_objects",
"output_folder": "./reports", // reports (test outcome) output by nightwatch
"selenium": {
"start_process": true, // tells nightwatch to start/stop the selenium process
"server_path": seleniumServer.path,
"host": "127.0.0.1",
"port": 4444, // standard selenium port
"cli_args": {
"webdriver.chrome.driver" : chromedriver.path
}
},
"test_settings": {
"default": {
"screenshots": {
"enabled": false // if you want to keep screenshots
},
"globals": {
"waitForConditionTimeout": 5000, // sometimes internet is slow so wait.
"testGlobal": "Original value set in nightwatch.conf.js"
},
"desiredCapabilities": { // use Chrome as the default browser for tests
"browserName": "chrome"
}
},
"chrome-local": {
"desiredCapabilities": {
"name" : "Nightwatch - chrome-local",
"browserName": "chrome",
"acceptSslCerts": true,
"javascriptEnabled": true // turn off to test progressive enhancement
},
"globals" : {
"propertyData" : {
"environment":"chrome-local"
}
},
"selenium_host" : "localhost",
"selenium" : {
"start_process" : true
}
}
}
};
GlobalTest.js
/* eslint-env es6 */ /* jshint esversion: 6 */
var config = require("../../nightwatch.conf.js");
module.exports = {
before: function (browser) {
globalTestPage1 = browser.page.GlobalTestPage1();
globalTestPage2 = browser.page.GlobalTestPage2();
},
"1) Test global set/get": function (browser) {
browser.perform(function() {
console.log("GlobalTest.js: testGlobal:", browser.globals.testGlobal);
});
globalTestPage1.setGlobal();
browser.perform(function() {
console.log("GlobalTest.js: testGlobal:", browser.globals.testGlobal);
});
globalTestPage1.printGlobal();
globalTestPage1.printGlobalAsParam(browser.globals.testGlobal);
globalTestPage2.printGlobal();
globalTestPage2.printGlobalAsParam(browser.globals.testGlobal);
}
};
GlobalTestPage1.js
/* eslint-env es6 */ /* jshint esversion: 6 */
module.exports = {
elements: {
},
commands: [{
setGlobal: function() {
return this.perform(function() {
this.api.globals.testGlobal = "New value set in GlobalTestPage1.js:setGlobal()";
console.log("GlobalTestPage1.js: setGlobal: globals.testGlobal: ", this.api.globals.testGlobal);
}.bind(this))
.pause(1);
},
printGlobal: function() {
return this.perform(function() {
console.log("GlobalTestPage1.js: printGlobal: globals.testGlobal: ", this.api.globals.testGlobal);
}.bind(this))
.pause(1);
},
printGlobalAsParam: function(value) {
return this.perform(function() {
console.log("GlobalTestPage1.js: printGlobalAsParam: globals.testGlobal: ", value);
})
.pause(1);
}
}]
};
GlobalTestPage2.js
/* eslint-env es6 */ /* jshint esversion: 6 */
module.exports = {
elements: {
},
commands: [{
printGlobal: function() {
return this.perform(function() {
console.log("GlobalTestPage2.js: printGlobal: globals.testGlobal: ", this.api.globals.testGlobal);
}.bind(this))
.pause(1);
},
printGlobalAsParam: function(value) {
return this.perform(function() {
console.log("GlobalTestPage2.js: printGlobalAsParam: globals.testGlobal: ", value);
})
.pause(1);
}
}]
};
输出
$ ./node_modules/.bin/nightwatch --env chrome-local --test ./tests/e2e/GlobalTest.js
[Global Test] Test Suite
========================
Running: 1) Test global set/get
GlobalTest.js: testGlobal: Original value set in nightwatch.conf.js
GlobalTestPage1.js: setGlobal: globals.testGlobal: New value set in GlobalTestPage1.js:setGlobal()
GlobalTest.js: testGlobal: New value set in GlobalTestPage1.js:setGlobal()
GlobalTestPage1.js: printGlobal: globals.testGlobal: New value set in GlobalTestPage1.js:setGlobal()
GlobalTestPage1.js: printGlobalAsParam: globals.testGlobal: Original value set in nightwatch.conf.js
GlobalTestPage2.js: printGlobal: globals.testGlobal: New value set in GlobalTestPage1.js:setGlobal()
GlobalTestPage2.js: printGlobalAsParam: globals.testGlobal: Original value set in nightwatch.conf.js
No assertions ran.