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);
    }
imtiyaz
  • 67
  • 3
  • 11
  • Take a look at this - https://selenium.googlecode.com/git/docs/api/java/org/openqa/selenium/support/ui/FluentWait.html – Shamik Dec 25 '14 at 19:22
  • 2
    Possible duplicate of [How I can check whether a page is loaded completely or not in web driver?](http://stackoverflow.com/questions/11001030/how-i-can-check-whether-a-page-is-loaded-completely-or-not-in-web-driver) – Narendra Chandratre Jul 15 '16 at 12:38

6 Answers6

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.

pavanraju
  • 663
  • 4
  • 5
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.

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.

Akbar
  • 1,513
  • 4
  • 20
  • 34
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.

fracz
  • 20,536
  • 18
  • 103
  • 149
  • 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 – imtiyaz Dec 16 '13 at 07:26
  • That's the beauty of `WebDriverWait`; it only waits as long as necessary to meet the desired condition, then returns. – JimEvans Dec 16 '13 at 10:40
0
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
Nakilon
  • 34,866
  • 14
  • 107
  • 142
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

MTK
  • 51
  • 5
  • 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](/review/late-answers/32388939) – rgettman Aug 05 '22 at 22:45
  • @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 – MTK Aug 07 '22 at 05:29