Reputation: 11
When I perform an action on my page, a spinner is displayed which disappears once the action is completed. I want to wait for the spinner to disappear so as to execute the assert statements.
I read the documentation which tells me how to wait for an element to appear but does not give info on how to wait for the element to disappear I don't know how to implement this in Cucumber, Geb, Groovy project.
Upvotes: 1
Views: 2096
Reputation: 332
I'll edit/explain this in a bit, when i have more time:
In your page object:
static content = {
loadingSpinner(wait:3, required:false) { $("mat-spinner") }
//this wait:3 is redundant (i think) if we also give the waitFor() a timeout
//required:false allows our wait !displayed to pass even if the element isnt there
}
def "Handle the loader"() {
try {
waitFor(2) { loadingSpinner.isDisplayed() }
} catch (WaitTimeoutException wte) {
//do nothing, if spinner doesnt load then thats ok
//most likely the spinner has come and gone before we finished page load
//if this is not the case, up our waitFor timeout
return true;
}
waitFor(10) { !loadingSpinner.isDisplayed() }
}
Upvotes: 2
Reputation: 2683
As described in the documentation, the waitFor
block uses Groovy Truth to know when it waited long enough. When you put a Navigator in it and the element is currently not present, it will wait for it to appear or until the maximum waiting time it elapsed.
So if want to wait for an element to disappear, you can simply put it in a waitFor
like this:
// go to the page
waitFor(2) { $(".loadingspinner").displayed }
waitFor(10) { !$(".loadingspinner").displayed }
// do your assertions
In case the loading spinner already disappeared, waitFor
will return immediately. In case it never disappears, it will throw a WaitTimeoutException
after 10 seconds, which will make your test fail.
Upvotes: 0