We're using Selenium with the Java API and some Javascript user extensions. We use a lot of AJAX calls in our app. A lot of our tests fail randomly because sometimes the AJAX calls finish slower than other times so the page isn't fully loaded. We fix that by waiting for specific elements or Thread.sleep. I was trying to find a way to instead just wait for the network traffic to finish. So that we could do this:
selenium.click("some JS button");
selenium.waitForNetwork();
assertTrue(something);
That way we can get rid of the thread sleep and have tests pass faster when the server responds faster and not have so many tests fail due to timing issues.
I haven't been able to find a way to do this searching Google. Does anyone have any ideas how we can accomplish this? (Preferably either through Javascript or the Java API but all suggestions are welcome).
Note: the other variations of "waitFor" are not what I'm looking for. We're already using those in clicks and other things. I'm looking for something that waits for the NETWORK TRAFFIC. Thanks for all the feedback, I'll be trying out a couple of the suggestions, but I'm still open to other ideas.
Thanks.
There are three ways to implement Selenium wait for page to load: Using Implicit Wait. Using Explicit Wait. Using Fluent Wait.
We can make Selenium wait for 10 seconds. This can be done by using the Thread. sleep method. Here, the wait time (10 seconds) is passed as a parameter to the method.
There are few types of waitFor available in Selenium. In your test case if you are aware that a particular text would appear on page load you can use waitForText. If there are different DOM elements getting populated (via Ajax/pageload) you can sequentially add waitForText.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With