Nightwatch 执行命令,迭代

Nightwatch execute command, iteration

我想知道是否可以使用 Nightwatch.js 提供的 execute() 命令来实现我想要的东西。

对于 Nightwatch.js,我想根据其拥有的行数多次对 table 元素执行操作。显然,我的代码不起作用,有人知道如何做吗?

// here is the execute() command
.exeute(
  function() {
    const rows = document.querySelectorAll('table tbody tr');
    return rows.length;  // suppose we have 3 rows in the table
  }, 
  [],
  function(result) {
    // here I want to perform an action to all the 3 rows, but I 
    // could not find a way to do that, here is my original thought
    for (let i = 1; i <= result; i++) {
      client.element('css selector', `table tbody tr:nth-child(${i})`, function() {
        // Action, do something here
      });
    }

  }
)
.waitForElementPresent('.mainpage', 15000)

为什么不全部在execute调用的函数中完成呢?

// here is the execute() command
.exeute(
  function() {
    const rows = document.querySelectorAll('table tbody tr');
    for (let i = 0; i < rows.length; i++) {
      let row = rows[i];
      // Action, do something here
    }
  }
)
.waitForElementPresent('.mainpage', 15000)

如果您更新为在 for 循环中引用 result.value 以从 execute 调用中获取 return 值,则您的原始代码可能有效。

使用 .elements 获取匹配结果列表并对其进行迭代。

.elements('css selector', 'td[class=someColumn]', (results) => {
          for(let i = 0; i < results.value.length; i++){
                client.elementIdText(results.value[i].ELEMENT, (element) => {
                       //do whatever you want with the element
                })
          }
});