量角器迭代元素列表时无法单击元素

时间:2019-05-29 12:14:07

标签: typescript protractor

clickClaimIDFromList(claimId){

   element.all(by.xpath('//*[@data-qa="dashboard.claim"]')).then(async (items) => {
            // console.log(items[0].getText());
            await expect(items[0].getText()).to.eventually.equal('12345');

            items.forEach(item => {
                     item.getText().then(async (text) => {
                            if (text === '12345') {
                                await console.log(item.getText());
                                await this.wait(10000);
                                return item.click();
                            }
                        }
                    );
                }
            );
        }


     );

}

无需单击该元素即可成功执行此代码。有人可以帮我重写吗?

2 个答案:

答案 0 :(得分:0)

当您使用async/await关键字时,我假设您已禁用框架中的控制流(通过在conf.js中设置SELENIUM_PROMISE_MANAGER: false)。

如果是这种情况,则无需使用太多的.then,因为await提供了一种更轻松的方式来处理它们。

您的测试可能通过了,因为它击中了text === '12345'检查是否失败,因此从不尝试单击。

//Make function async
async clickClaimIDFromList(claimId) {

  let allElems = await element.all(by.xpath('//*[@data-qa="dashboard.claim"]'));

  //don't await expects, await what is occuring inside them if you need to
  expect(await allElems[0].getText()).to.eventually.equal('12345');

  for (let i = 0; i < allElems.length; i++) {
    let item = allElems[i];

    //Save the result of the getText to a variable
    let itemText = await item.getText();
    console.log(itemText);

    if (itemText === '12345') {
      await this.wait(10000);

      //No need to return the click, we can await it here
      await item.click();
    } else {
      console.log('else is being hit');
  }
}

注意:我尚未测试此代码,但逻辑应正确

答案 1 :(得分:0)

可执行代码:

async clickClaimIDFromList(){

    const allElems = await element.all(by.xpath('//*[@data-qa="dashboard.claim"]'));


    for (let i = 0; i < allElems.length; i++) {
        const item = allElems[i];

        // Save the result of the getText to a variable
        const itemText = await item.getText();
        console.log(itemText);

        if (itemText === '123124214') {
            await this.wait(10000);

            // No need to return the click, we can await it here
            await item.click();
        } else {
            console.log('else is being hit');
        }

    }
}