问题描述
我目前正在使用node + nightwatch + selenium进行自动化。
我遇到了一个场景:
我在Nightwatch中定义了一个数组作为全局数组:
Dev.js
'checkforLink':{
link1:Some Xpath 1,
link1 :Some Xpath 2,
link1:Some Xpath 3,
link1:Some Xpath 4
},
在自定义命令中的自定义js脚本中,我为循环执行来从全局中获取此链接变量:
exports.command = function(callback){
browser = this;
var data = browser.globals;
console.log(Before all loop);
(data.checkforLink中的var menu_link){
linkss1 = data.checkforLink.link1; //返回`Some Xpath 1`
reqvar = data.mainMenuLink.menu_link; //尽管menu_link的值为link1,但reqvar未定义
browser.click('######')//单击路径
}
return this ;
};
data.checkforLink中的var menu_link),变量'menu_link'将是一个字符串。
要从一个带有字符串的对象获取属性,使用'object [string]'语法。尝试 data.mainMenuLink [menu_link];
只要data.mainMenuLink.link1存在,就应该有效。
我在你的问题中注意到所有的属性都有值'link1'。希望它们在代码中是不同的属性名称。
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;
};
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.
这篇关于夜间全局变量。问题为node.js的for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!