Search code examples
javaseleniumselenium-webdriverselenium3

Selenium 3.4 how to use changed wait.until


So with Selenium 3.4 my previously working wait.untils aren't working (been replaced by new method). I can't seem to get the new method working though.

I'm using

import com.google.common.base.Function;

Old code:

public boolean waitForURLToMatch(String expectedURL, int waitTime){
    WebDriverWait wait = new WebDriverWait(driver, waitTime);
    wait.until(EcpectedConditions.urlMatches(expectedURL));
}

New code:

public boolean waitForURLToMatch(String expectedURL, int waitTime){
    WebDriverWait wait = new WebDriverWait(driver, waitTime);
    wait.until(new Function<WebDriver, boolean>){

        @Override
        public boolean apply(WebDriver driver) {
            return driver.getCurrentUrl().equals(expectedURL);
        }
    }
}

The new code has an error in eclipse: Syntax error on tokens, InterfaceHeader expected instead

Any ideas on where I've gone wrong?


Solution

  • So after much googling I eventually found the issue is just the syntax.

    This works:

    public boolean waitForURLToMatch(String expectedURL, int waitTime){
        Wait<WebDriver> wait = new WebDriverWait(driver, waitTime);
        Function<WebDriver, Boolean> function = new Function<WebDriver, Boolean>() {
            public Boolean apply(WebDriver driver) {
                String currentURL = driver.getCurrentUrl();
                if(currentURL.equals(expectedURL))
                {
                    truefalse = true;
                    return truefalse;
                }
                truefalse = false;
                return truefalse;
            }
        };
        try{
            wait.until(function);
        } catch (TimeoutException e){   
        }
        return truefalse;
    }
    

    EDIT: Ok so it seems this was just a classpath conflict and all now works, the classpath conflict in conjunction with Selenium removing the deprecated until(predicate) confused the matter.