Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

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 fails. How can i make my selenium to 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 everytime or it sets the maximum time limit to 4 secs . I even tried this but it really slowed my test execution. I need some help.

share|improve this question

6 Answers 6

Look at the Wait commands rather than sleep - a search on this site or Google will give you lots of examples

share|improve this answer
    
The single most helpful method for avoiding problems like the OP has is to use a "waitForPageToLoad" method. Totally worth it. –  joshin4colours Oct 10 '13 at 11:35

A Sleep method is almost always the least acceptable way to synchronize an automated test. A better approach might be to register an event and check the event is raised when in the desired state, or use a polling loop which will can enable the test to proceed when the system is ready (e.g. an element id appears), or a timeout occurs if it takes more than a pre-determined max wait time. (See Sleepy Automated Tests)

share|improve this answer

You can use WebDriverWait to synch with page load. Something like the following might help

WebDriverWait wait = new WebDriverWait(driver, 15); //you can play with the time integer  to wait for longer than 15 seconds.`
wait.until(ExpectedConditions.titleContains("title")); //if you want to wait for a particular title to show up

OR

wait.until(ExpectedConditions.presenseOfElementLocated(ByLocator)); //in case you want to wait for a particular element to appear on the page.`
share|improve this answer

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.

To wait for an element to appear, 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/.

share|improve this answer
    
I don't think this is going to give you a 60 second fuse - all it's going to give is 60 iterations which take whatever time they take - to do something like this you really need to include an explicit wait call for 1 second. –  Kate Paulk Sep 17 '13 at 18:33

Don't ever use sleep in your test. Every sleep you have indicates to a bad test design. Let's say you have a sleep of 1000ms, but it takes 1002ms to load something - your test will break. If it takes 996ms, you are wasting time. That been said, I just answered pretty much the same question in stackoverflow - http://stackoverflow.com/questions/18843581/wait-for-elment-webdriver-pageobject-pattern/18844275#18844275

share|improve this answer
    
I think you have a point . But for page load can we use WaitForPageLoad() ? . Some times due to slow network my application becomes slow or page load time gets slower . How to handle selenium in those situations ?? –  selenium_vtiger Sep 26 '13 at 5:55
1  
There is no such thing has "page has finised loading" unless you are serving static html. nowadays stuff getting loaded async all the time and this conception does not exist anymore. you as a tester must know when some specific stuff happens and wait for that very thing to happen before you continue. So you rather wait for (for example) a submit button to be visible and assume the page in a state so you can continue. If you run your tests thru a proxy, the browsermob has something like WaitForNetworkTrafficToStop, but i would go with WebDriverWait. –  Erki M. Sep 27 '13 at 7:50
synchronized(driver) {
      driver.wait(4000);
}
share|improve this answer
3  
Welcome to SQA, Raman. Could you elaborate on this? As a rule of thumb, a snippet of code is less helpful than the accompanying explanation. –  corsiKa Aug 19 '13 at 14:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.