Selenium Java Lambda Implementation for Explicit Waits

I am trying to implement Java Lambda concept for selenium webdriver waits. I need to convert custom webdriver wait something like this

  (new WebDriverWait(driver(), 5))
            .until(new ExpectedCondition<WebElement>() {
                public WebElement apply(WebDriver d) {
                    return d.findElement(By.linkText(""));
                }
            });

to

 (new WebDriverWait(driver(), 5)).until((driver) -> driver.findElement(By.linkText("")));

But it does not matches the functional interface of ‘until’ refers to and throws error.

So i tried passing the Lambda as it supports.

Attempt1

Predicate<WebDriver> isVisible = (dr) -> dr.findElement(
     By.linkText("")).isDisplayed();
     webDriverWait.until(isVisible);

It kind of works but is not what i require because it returns only void.

Need your help or advice on this.

Answer

The problem is with your syntax.The below worked perfectly for me

WebElement wer = new WebDriverWait(driver, 5).until((WebDriver dr1) -> dr1.findElement(By.id("q")));

Your code Problem

 //What is this driver() is this a function that returns the driver or what
 //You have to defined the return type of driver variable in until() function
 //And you cant use the same  variable names in both new WebdriverWait() and until() see my syntax

(new WebDriverWait(driver(), 5)).until((driver) -> driver.findElement(By.linkText("")));

Leave a Reply

Your email address will not be published. Required fields are marked *