Global variable in nightwatch. Issue in for loop for node.js

眉间皱痕 提交于 2019-12-13 05:08:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!