1

I am using selenium webdriver along with TestNG in eclipse.The problem is the page relaods in the midway for some data and the time of this reload is flexible thats why I am not able apply explicit wait time.I want to make webdriver wait until this reload completes.

I am trying to do this through this code...but it is not working

public void waitForPageLoadingToComplete() throws Exception {
        ExpectedCondition<Boolean> expectation = new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver driver) {
                return ((JavascriptExecutor) driver).executeScript(
                        "return      document.readyState").equals("complete");
            }
        };
        Wait<WebDriver> wait = new WebDriverWait(driver, 30);
        wait.until(expectation);
    }
2

6 Answers 6

1

try the below code for handling page load/page refresh time outs

WebDriver driver = new FireFoxDriver();
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);

please use latest version of chrome driver, as the page wait is not handled in older version of chrome driver.

Sign up to request clarification or add additional context in comments.

Comments

1

Waiting for an indefinite time is not a good idea. Timing of a website is also a part of testing. If possible find out the Service Level Agreement of the "page" you are testing. If not run a speed test for the website(here is a method to test : http://tools.pingdom.com/fpt/ ) and use an average of time you get. If this also doesn't work the last option is to work with industry wide standards.

Comments

1

document.readyState() does not reflect the correct page load time(example- it does not wait for images/scripts to load fully). It is suggested and tested option to wait for an element on the page(preferrably the one you will operate upon in your next step of test). As others have suggested use WebDriverWait with expected conditions methods like "visibilityOf", "presenceOfElement" or many more and it should be fine.

Comments

0

You should use WebDriverWait and set the timeout to the maximum time you can wait. As soon as you discover that the page loaded the required data (e.g. by checking for visibility of a certain element), you may proceed with the test case.

See an example in the selenium docs.

2 Comments

relaod time varies from 2sec to 40 sec, so i can't always put waiting time more than 40s ,i want to make it wait as soon as it completes its loading
That's the beauty of WebDriverWait; it only waits as long as necessary to meet the desired condition, then returns.
0
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

Comments

0

For java 8 onwards:

    JavascriptExecutor js = (JavascriptExecutor) driver;
    new WebDriverWait(driver, 10).until(webDriver ->(js).executeScript("return document.readyState;").equals("complete"));

For java below 8 you can try the below solution from the below link. I am using it and it's working for me.

Wait for page load in Selenium

2 Comments

While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
@rgettman Previously I had posted my answer the you have mentioned above.But the same was also deleted by someone by mentioning that don't post same answer again and again. Now again as per your suggestion I am editing it... Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.