Changing Selenium implicit wait inside test process Changing Selenium implicit wait inside test process selenium selenium

Changing Selenium implicit wait inside test process


Perfectly valid to do this. More importantly correct way to avoid mixing up implicit and explicit waits. That mixture of waits leads to some hard to debug issues with long wait times. Problem is you could forget to turn off and on the implicit wait. Better to write a utility function to wrap this logic by passing the explicit wait criterion and use it across your code base.

I took a stab at creating a generic method to use. Though generic ain't my strong point.

protected static <T> T waitECTimeoutWrapped(WebDriver driver, ExpectedCondition<T> ec, int timeout) {    driver.manage().timeouts().implicitlyWait(0, TimeUnit.SECONDS);    WebDriverWait wait = new WebDriverWait(driver, timeout);    T data = waitDriver.until(ec);    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);    return data;}

You can ignore the return data for many cases but this way you can retrieve elements that you may be looking for in one call.


If UI is slow, what about adding some waiter to wait until page is fully loaded? Below is what I do to make sure page is fully loaded.

    public static void waitForDomToComplete(WebDriver driver) {    FluentWait<WebDriver> wait = new FluentWait<WebDriver>(driver)            .withTimeout(60, TimeUnit.SECONDS)            .pollingEvery(700, TimeUnit.MILLISECONDS)            .withMessage("Time out is reached. Page is not loaded!");    wait.until(new ExpectedCondition<Boolean>() {        @Override        public Boolean apply(WebDriver driver) {            JavascriptExecutor js = (JavascriptExecutor) driver;            String domLoadStatus = (String) js.executeScript("return document.readyState");            if (domLoadStatus.equals("complete")) {                return true;            } else {                return false;            }        }    });}