问题
I am currently working with node + nightwatch + selenium for automation. I came across a scenario:
I have defined an array as Global array in nightwatch:
Dev.js
'checkforLink':{
link1:"Some Xpath 1",
link1:"Some Xpath 2",
link1:"Some Xpath 3",
link1:"Some Xpath 4"
},
In my custom js script in custom-commands, I am doing a for
loop to fetch this links from global variable:
exports.command = function(callback) {
browser = this;
var data = browser.globals;
console.log("Before all loop");
for(var menu_link in data.checkforLink) {
linkss1 = data.checkforLink.link1; // returns `Some Xpath 1`
reqvar = data.mainMenuLink.menu_link; // Even though menu_link have value as link1, reqvar is undefined
browser.click('######') // Click the path
}
return this;
};
回答1:
When you run for(var menu_link in data.checkforLink)
, the variable 'menu_link' will be a string.
To get the property from an object with a string use 'object[string]' syntax. Try data.mainMenuLink[menu_link];
This should work as long as data.mainMenuLink.link1 exists.
I noticed in your question all of the properties have the value 'link1'. Hopefully they are different property names in your code.
回答2:
I am not sure your actual problem is related to loop only or you are not able to create a instance using "var data = browser.globals;" so your variable data having {} no value.If this is a case please rename your Dev.js as "globals.js" and provide a path of it as value of a key "globals_path" in "nightwatch.json".
来源:https://stackoverflow.com/questions/27617463/global-variable-in-nightwatch-issue-in-for-loop-for-node-js