Priyanshu
Priyanshu

Reputation: 3058

Selenium webdriver execution gets stuck due to a javascript wait condition

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.

Upvotes: 0

Views: 2523

Answers (1)

Dingredient
Dingredient

Reputation: 2199

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.

Upvotes: 1

Related Questions