Wednesday, 1 August 2018

SE - 30 - Setting timer for a page to load completely by JavaScriptExecutor


Apart from waits there is one more synchronisation point through which we can check that the page is loaded or not or we can set a timeout for it. This can be achieved through java script executor.

Below code demonstrates that how we can use Javascriptexecutor to set a timeout and check that the page is loaded :
public void waitForLoad(WebDriver driver) {
    ExpectedCondition<Boolean> pageLoadCondition = new ExpectedCondition<Boolean>() {
                public Boolean apply(WebDriver driver) {
                    return ((JavascriptExecutor)driver)
                      .executeScript("return document.readyState").equals("complete");
                }
            };
    WebDriverWait wait = new WebDriverWait(driver, 30);
    wait.until(pageLoadCondition);

So here we can see that we are checking the document.readystate should be equal to true. We have kept this inside the expected condition and we are using this with webdriver explicitwait.

No comments:

Post a Comment