Generally, you want to click an element for unknown amount times until alert is present. So, maybe you can go with this approach:
This function takes an ElementFinder
object which is clicked repeatively until either a given timeout
is reached OR the alert box is present.
const clickElementUntilAlertPresent = async (el: ElementFinder, timeout: number = 10 * 1000): Promise<boolean>=> {
const clickDelay = 100
let done = false
// start interval for clicking element repeatively
const interval = setInterval(() => {
// IF the function is done, clear interval,
if(done) {
console.log("stop clicking element now", el.locator.toString())
clearInterval(interval)
return
}
console.log("clicking element", el.locator.toString())
el.click()
}, clickDelay)
// wait until alert box is present. After timeout, returns false.
const result = await browser.wait<boolean>(browser.ExpectedConditions.alertIsPresent(), timeout)
// Result is now true if alert is present, or false, if timeout reached.
// Important here, to set done to true. We want to stop clicking interval!
done = true
return result
}
Note
You should learn about Promise
class.
For example,
if (EmailLinkpageElement.isDisplayed()) { }
Will always be true, since this returns a Promise
object, and an object is evaluated as truethy
in javascript.
If you are not familiar with Promise
class, then your first priority now should be to learn them! You need them for async
tasks and in testing it's a lot..
Here's one ressource: Promise