Nightwatchjs elementIdText函数在循环中

时间:2016-12-05 18:29:00

标签: node.js asynchronous nightwatch.js

我很难使用elementIdText中的Nightwatchjs函数从多个ID 返回文字,它可能来自异步功能。

我正在获取一系列网络元素ID 并尝试制作一个包含的文字数组。

我这样做了,因为原来的getText函数并没有遍历所有元素,只是第一次出现。

我的常用功能:

module.exports = {
  'ChildOf' : function(browser, path, callback) {
    browser
    .elements('css selector', path, function(elements) {
      var array = [];
      elements.value.forEach( function(element) {
        array.push(element.ELEMENT);
      })
      //console.log(array); out: Final array filled; Works great
      callback(array);
    })
  },

  'getText' : function(browser, elements, callback) {
    var array = [];
    elements.forEach( function(element) {
      browser.elementIdText(element, function(result) {
        array.push(result.value);
        //console.log(array); out: Multiple incremented arrays; Need the final array :\
      })
      //console.log(array); out: Multiple empty arrays; Doesn't work
    })
    //console.log(array); out: Single empty array; Doesn't work
    callback(array);
  }
]

我的电话:

module.exports = {
  'myStep' : function(browser, expect) {
    common.ChildOf(browser, '.txt', function(elements) {
      common.getText(browser, elements, function(result) {
        //browser.verify.equal(result, expect)
      })
    })
  }
};

我需要从我的自定义getText函数中获取一个完整的字符串数组,我可以将它与另一个数组(数据驱动测试)进行比较,任何想法?

1 个答案:

答案 0 :(得分:0)

使用promise(Q),添加了4行,感谢this Nightwatchjs Github问题。