Survive the Guava Removal in Selenium 4.51
intermediate Java 15 minAfter 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 existSelenium 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.commonimports remain. visible()returnsExpectedCondition<WebElement>(orjava.util.function.Function<WebDriver, WebElement>).- Guava’s
Predicate.applybecomesjava.util.function.Predicate.test. ImmutableList.ofbecomesList.of.- Bonus: a
withMessageon 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.
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
- Custom Wait Conditions Write your own expected conditions with lambdas and classes, wait for attribute and text changes, element counts, JavaScript state, network idleness via BiDi, and compose conditions cleanly.
- What's New in Selenium 4 Every feature Selenium 4 added since 4.0, from relative locators and Selenium Manager to BiDi, Electron support and Kubernetes-native Grid, and which release introduced each.