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