0

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.

Sitam Jana
  • 3,123
  • 2
  • 21
  • 40

2 Answers2

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);
        }
    }
}
Sitam Jana
  • 3,123
  • 2
  • 21
  • 40
-1
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?

Anthony Klotz
  • 567
  • 1
  • 5
  • 19