Protractor click if displayed is not working using

2019-09-01 03:31发布

问题:

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.

回答1:

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);
        }
    }
}


回答2:

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?



标签: protractor