Skip to main content
SeleniumDecoded
Back to Challenges

Survive the Guava Removal in Selenium 4.51

intermediate Java 15 min

After upgrading the Java client to 4.51, a wait helper class no longer compiles. Migrate it off Guava types to plain Java so it builds on 4.51 and later.

The Scenario

Your team bumps selenium-java from 4.50 to 4.51 as part of its quarterly dependency review. Hundreds of tests compile fine, but the shared Waits helper does not:

error: incompatible types: ExpectedCondition<WebElement> cannot be converted
to com.google.common.base.Function<WebDriver,WebElement>
error: package com.google.common.collect does not exist

Selenium 4.51 removed the Java client’s dependency on Guava, and ExpectedCondition now extends only java.util.function.Function. Code that passes conditions straight to wait.until(...) or uses lambdas is unaffected. Code that stored conditions in Guava-typed variables, or quietly relied on Selenium putting Guava on the classpath, breaks.

Your Task

Migrate Waits so it compiles against 4.51 without adding Guava back as a dependency. Keep the public behaviour of every method.

What Good Looks Like

  • No com.google.common imports remain.
  • visible() returns ExpectedCondition<WebElement> (or java.util.function.Function<WebDriver, WebElement>).
  • Guava’s Predicate.apply becomes java.util.function.Predicate.test.
  • ImmutableList.of becomes List.of.
  • Bonus: a withMessage on the custom wait so a timeout says what it was waiting for.

Note: If other parts of your codebase genuinely use Guava, declare it as a direct dependency in your own build instead of relying on a transitive one. The 4.51 upgrade only exposed a dependency you already had.

Your task

After upgrading the Java client to 4.51, a wait helper class no longer compiles. Migrate it off Guava types to plain Java so it builds on 4.51 and later.

Problem Code
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.*;
import java.time.Duration;
import java.util.List;

public final class Waits {
    // Compiled on 4.50. On 4.51: incompatible types:
    // ExpectedCondition<WebElement> cannot be converted to com.google.common.base.Function
    public static Function<WebDriver, WebElement> visible(By locator) {
        return ExpectedConditions.visibilityOfElementLocated(locator);
    }

    public static Predicate<WebElement> hasText(String text) {
        return el -> el.getText().contains(text);
    }

    public static final List<String> ADMIN_ROLES = ImmutableList.of("owner", "admin");

    public static WebElement waitFor(WebDriver driver, By locator) {
        return new WebDriverWait(driver, Duration.ofSeconds(10)).until(visible(locator));
    }

    public static WebElement waitForText(WebDriver driver, By locator, String text) {
        return new WebDriverWait(driver, Duration.ofSeconds(10)).until(d -> {
            WebElement el = d.findElement(locator);
            return hasText(text).apply(el) ? el : null;
        });
    }
}

Need Help?

Solution

Try to solve the challenge yourself before revealing the solution.

Lessons that cover this