What should be done if selenium wait until page loads?
I run nearly 30-40 test cases twice a day. And everytime some test case fails due to page load time. Infact i have explicitly mentioned Thread.sleep("2000"); wherever I feel it takes more time to load. But still some 5-6 testCases failed. How can I make my selenium wait until the page loads ?? And what would the below code do ? driver.manage().timeouts().implicitlyWait(4000, TimeUnit.SECONDS); Will it wait for 4 seconds every time or it sets the maximum time limit to 4 secs .
This code tells Selenium to search up to 60 seconds for id-of-element to appear on the page. Once it finds the element, you can interact with it. If the element is not found within 60 seconds, a java.lang.AssertionError will be thrown with the reason being "timeout".
for (int second = 0;; second++){
if (second >= 60)
fail("timeout");
try{
if (1 == driver.findElements(By.xpath("//a[@id="id-of-element"]")).size()){
driver.findElement(By.xpath("//a[@id="id-of-element")).click();
break;
}
} catch (Exception e){
}
}
As per Kate's comment, this code will not produce a wait time of 60 seconds.
If selenium wait until page loads, I would use an implicit wait: driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS); This means that whenever you call WaitForElement() or WaitForElements(), the driver will wait up to 60 seconds for the specified element or elements to appear. See http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp#implicit-waits and http://www.bizalgo.com/2012/01/14/timing-races-selenium-2-implicit-waits-explicit-waits/.