Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium - Wait for network traffic

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.

like image 965
Joel Avatar asked Nov 05 '10 22:11

Joel


People also ask

Does Selenium wait for page to load?

There are three ways to implement Selenium wait for page to load: Using Implicit Wait. Using Explicit Wait. Using Fluent Wait.

How do I make Selenium wait for some time?

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.


1 Answers

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.

like image 113
ch4nd4n Avatar answered Sep 30 '22 22:09

ch4nd4n