简体   繁体   English

守夜中的全局变量。 在node.js的for循环中发出

[英]Global variable in nightwatch. Issue in for loop for node.js

I am currently working with node + nightwatch + selenium for automation. 我目前正在使用node + nightwatch + selenium进行自动化。 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: 在自定义命令中的自定义js脚本中,我正在执行for循环以从全局变量中获取此链接:

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. 当您运行for(var menu_link in data.checkforLink) ,变量'menu_link'将是一个字符串。

To get the property from an object with a string use 'object[string]' syntax. 要使用字符串从对象获取属性,请使用'object [string]'语法。 Try data.mainMenuLink[menu_link]; 试试data.mainMenuLink[menu_link];

This should work as long as data.mainMenuLink.link1 exists. 只要data.mainMenuLink.link1存在,它就应该起作用。

I noticed in your question all of the properties have the value 'link1'. 我在您的问题中注意到所有属性的值都为'link1'。 Hopefully they are different property names in your code. 希望它们在您的代码中是不同的属性名称。

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;" 我不确定您的实际问题仅与循环有关,还是无法使用“ 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". 因此您的变量数据没有{}的值。如果是这种情况,请将Dev.js重命名为“ globals.js”,并在“ nightwatch.json”中提供其路径作为键“ globals_path”的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM