0

I have been using a java-script wait condition to wait for page load and my code is like this:

public void untilPageIsLoaded() {
        ExpectedCondition<Boolean> condition = new ExpectedCondition<Boolean>() {
            public Boolean apply(WebDriver driver) {
                return ((JavascriptExecutor) driver).executeScript("return document.readyState").equals("complete");
            }
        };
        WebDriverWait wait = new WebDriverWait(driver, timeout);
        wait.until(condition);
    }

Initially this code was working very well but few days later my tests started getting stuck at random points. So I investigated the issue and came to know that the culprit is above java-script wait condition which stops execution.

Even I was not getting any exception and it never timeout.I know this is very strange but this is not only with me, have a look here;

https://code.google.com/p/selenium/issues/detail?id=6955

I tried with Upgraded/downgraded version of selenium and browser,changed system configuration but none of them worked.

Now I want to replace above java-script wait condition with some other code.I don't want to use thread.sleep();

Please suggest me something good.

Priyanshu
  • 3,040
  • 3
  • 27
  • 34
  • Something like this http://stackoverflow.com/questions/23186851/how-to-locate-and-type-something-in-the-textbox/23192948#23192948 or this http://stackoverflow.com/questions/15122864/selenium-wait-until-document-is-ready – Sriram Sep 08 '14 at 07:37
  • What versions of the browser and Selenium are you running? – Arran Sep 08 '14 at 15:13
  • @Arran I upgraded/downgraded almost every version of selenium and firefox to try all combinations. But It did not work for me. As of now I am using selenium 2.42 and firefox 31.0 – Priyanshu Sep 09 '14 at 04:23

1 Answers1

1

This code would never execute until the document is already ready anyway.

So there is no need to execute this JS from selenium - because it can only ever return one result - "complete."

This is because you can't execute JS from SE until you are on a page. The only way to get on a page is to call Selenium's .get(), which blocks any following execution until the DOM is fully loaded. So .get() is already doing what your .untilPageIsLoaded() would otherwise do.

Dingredient
  • 2,191
  • 22
  • 47