I have a working solution, but I am hoping there is a better way.
In automating a website, there are many places where a spinner is shown to tell the user that a process is running. The div is added and removed from the DOM as needed; as opposed to a class change or setting display:none.
When I first starting automating I had a simple wait for the element to no longer be there: numberOfElementsToBe equal to 0. But I kept having timing issues where it took too long for the spinner to appear so the code would assume everything was done, continue on, and fail because the page was not ready yet.
To combat this, I added a try/catch to first look/wait for the spinner to appear for 2 seconds.
try {
quickWait.until(ExpectedConditions.numberOfElementsToBeMoreThan(By.className(identifier), 0));
} catch (Exception e) {
//Nope
}
wait.until(ExpectedConditions.numberOfElementsToBe(By.className(identifier), 0));
}
This way, I catch all situations:
- Normal - Wait for spinner, it shows up normally, then wait for it to go away
- Slow - Wait for spinner, it takes a second to show up but is still caught by the first wait, and then wait for it to go away
- Fast - Wait for spinner, but the operation was so fast that the spinner has already come and gone. So the code waits for the spinner for two seconds and doesn't see it, so it falls to the catch which does nothing, and then waits for the spinner to go away (which it already has) so immediately continues on.
All this is fine except for all the "fast" situations. Since the wait fails, it keeps throwing a WARNING in the console log. This makes diagnosing issues a pain as I have to wade through a bunch of warnings trying to find the real issue.
I don't want to turn off all warnings as there could be important info in other areas. So does anyone have any suggestions/alternate methods for handling "wait for situation to exist/wait for it to stop" situations that wouldn't throw out a ton of useless warnings?
UPDATE #1 The timing of the spinner is variable. Sometimes it's so quick it is barely a flicker on the screen, and other times it takes a second to even show on the screen. The tests can not be so brittle that a mistimed spinner causes it to fail. It is only used as a signal that the results are available.
- The warning happens during runtime, so using @SuppressWarnings() is not an option
- As stated, I don't want to universally suppress all warnings so "just turning them off" will not work