0

In my application there is a Click to add email link , on clicking on the same link multiple times , alert message comes that you have reached the maximum limit.

How should I implement using protractor with typescript. I used for loop, but it didn't work

for (let i:number=1; i<5; i++){   
    if (EmailLinkpageElement.isDisplayed())
    {   
        // click on the link
    }
    else
    {
        // verify the text message
    }
}

Please suggest what to do as I am trying but couldn't reach to any solution.

jknotek
  • 1,778
  • 2
  • 15
  • 23
Akash Raj
  • 13
  • 4
  • Please share the error for your implementation. try using `isPresent()` instead of `.isDisplayed()` – Madhan Raj Jun 11 '19 at 09:26
  • https://stackoverflow.com/questions/27910331/using-protractor-with-loops This might be helpful – E SV Jun 11 '19 at 09:43

1 Answers1

0

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

Community
  • 1
  • 1
Silvan Bregy
  • 2,544
  • 1
  • 8
  • 21