Protractor click if displayed is not working using

2019-09-01 03:18发布

Protractor click if displayed is not working using async await. I have tried with the following method:

public static async clickIfDisplayed(targetElement: ElementFinder) {
    if (await targetElement.isPresent() && await targetElement.isDisplayed()) {
        await PageHelper.click(targetElement);
    }
}

The above sometime clicks even if element is not present or displayed. Please help to understand where I am going wrong here.

标签: protractor
2条回答
乱世女痞
2楼-- · 2019-09-01 04:06

The following worked well with async-await:

public static async clickIfDisplayed(targetElement: ElementFinder) {
    const isPresent = await targetElement.isPresent();
    if (isPresent) {
        const isDisplayed = await targetElement.isDisplayed();
        if (isDisplayed) {
            await PageHelper.click(targetElement);
        }
    }
}
查看更多
地球回转人心会变
3楼-- · 2019-09-01 04:06
public static async clickIfDisplayed(targetElement: ElementFinder) {

    await targetElement.isPresent().then(bool1 => {
        await targetElement.isDisplayed().then (bool2 => {
            if (bool1 && bool2) {
                await PageHelper.click(targetElement);
            }
        });
    }
}

Does this work?

查看更多
登录 后发表回答