使用 Protractor 自动化框架循环事件

Jag*_*ags 0 javascript dom-events protractor

我正在使用量角器 - Javascript 运行自动化测试..我试图从表格格式的 UI 中获取值..

我编写了如下脚本。问题是当我进入“i”循环时。它正在获取“i”的所有值,而不是“i”的第一个值,而不是继续进行“j”循环。

然而,一旦i循环完成......它会正确地进入下一个循环jk其中j&k循环按预期工作(根据其他编程语言中的通用循环标准)。

    this.cate = function () {
    let acct = [];
    return this.getCountOfCate().then((values) => {
        for (let i = 0; i < values; i++) {
            openAccounts.push(browser.findElement(locators.someField).getText());
            this.getRowCount().then((rowValues) => {
                console.log('rowValues is' +rowValues);
                for (let j = 0; j < rowValues; j++) {
                    this.getColumnCount().then((columnValues) => {
                        for (let k = 0; k < columnValues; k++) {
                            acct.push(element.all(classes.accountReports).all(by.css(getAccountCategoryCnt(i))).all(by.css('.title')).get(j).all(by.css('td')).get(k).getText());
                        }
                    })
                }
            });
        }
        return acct;
    });
}
Run Code Online (Sandbox Code Playgroud)

上面cate是一个函数,它将作为承诺从其他步骤中调用,并期望acct在返回后返回数组中的值(值在这段代码中返回),我将其与数组值进行比较。

唯一的问题是我没有按照正确的顺序进行操作,因为“i”循环一次完成所有操作,然后继续j循环k

请建议这里可能存在什么问题..

cni*_*ina 5

由于我对您的表格的外观没有任何背景...以下是对示例 HTML 代码段使用 map 函数的示例。Promise 和 for 循环不能一起工作,这就是为什么我们有其他方法,如mapreducefiltereach

HTML 片段示例:

 <table>
   <tr>
     <td class="name">Foo</td>
     <td class="title">Developer</td>
   </tr>
   <tr>
     <td class="name">Bar</td>
     <td class="title">Designer</td>
   </tr>
  </table>
Run Code Online (Sandbox Code Playgroud)

您可以有一个使用element.allmap函数的方法

  // returns a promise of a list of accounts
  getAccounts() => {
    return element.all(by.tagName('tr')).map(elem => {
      let account = new Account();
      let promises = [];
      let tds = elem.all(by.tagName('td'));

      promises.push(tds(by.css('.name')).getText().then(text => {
        account.name = text;
      }));
      promises.push(tds(by.css('.title')).getText().then(text => {
        account.title = text;
      }));

      return Promise.all(promises).then(() => {
        return account;
      });
    });
}
Run Code Online (Sandbox Code Playgroud)

因此,要获取这些值,您可以调用:

getAccounts().then(accounts => {
  console.log(accounts);
});
Run Code Online (Sandbox Code Playgroud)