如何根据量角器中的条件执行方法执行?

时间:2018-06-05 22:19:12

标签: protractor

我是一名Java开发人员,也是新的量角器。我试图遍历网页中的动态网络表并尝试找出特定的用户名并打开他的个人资料。一旦我打开他的个人资料,我希望我的测试方法停止执行并返回。但是return语句不起作用,for循环仍然运行。有人可以帮我这个吗?以下是我的方法。

searchUser() {
// Iterate through the user listing table 
 browser.driver.findElement(by.css('.slds-max-medium-table--stacked-horizontal')).then(function (table) {
  table.findElement(by.tagName('tbody')).then(function (tbody) {
    tbody.findElements(by.tagName('tr')).then(function (rows) {
       for (var i = 0; i < rows.length; i++) {
        rows[i].findElements(by.tagName('td')).then(function (cols) {
          cols[1].getText().then(function (user) {
            if (user == "ADAM SMITH") {
              // found user, open his profile
             cols[1].click();
             return;

            }
          });

        });
      }
      // user not found in the current page. Click on next page and continue search
      element(by.xpath("//*[@id='main']/div/app-users-profile/users-listing/form/div[2]/div/div/div/div[2]/div/button")).click();
      browser.sleep(3000);
      var currentPage = new LoginPage();
      currentPage.searchUser(); 

        });
    });
  }); 

}

1 个答案:

答案 0 :(得分:0)

我尽量减少差异。

async function searchUser () {
  let result = false;
  while (!result) {
    const table = browser.$('.slds-max-medium-table--stacked-horizontal');
    const tbody = table.$('tbody');
    const rows = tbody.$$('tr');
    await rows.map(async (row) => {
      const column = row.findElements(by.tagName('td')).get(1);
      const name = await column.getText();
      if (name === 'ADAM SMITH') {
        await column.click();
        result = true;
      }
    });
    if (!result) {
      await element(by.xpath("//*[@id='main']/div/app-users-profile/users-listing/form/div[2]/div/div/div/div[2]/div/button")).click();
      await browser.sleep(3000);
      const currentPage = new LoginPage();
      await currentPage.searchUser();
    }
  }
}