Detect chromium version for CI ChromeDriver

This commit is contained in:
2026-04-08 13:30:37 +03:00
parent 5cab207685
commit 81b1446e36
2 changed files with 57 additions and 1 deletions
@@ -8,6 +8,12 @@ import io.github.bonigarcia.wdm.WebDriverManager;
import ru.kovbasa.config.TestConfig;
import ru.kovbasa.listeners.HighlightElementListener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public final class WebDriverProvider {
private WebDriver driver;
@@ -27,7 +33,12 @@ public final class WebDriverProvider {
private WebDriver createDecoratedDriver() {
if (!TestConfig.isSelenoidMode()) {
WebDriverManager.chromedriver().setup();
final String chromeVersion = detectChromeMajorVersion().orElse(null);
if (chromeVersion != null) {
WebDriverManager.chromedriver().browserVersion(chromeVersion).setup();
} else {
WebDriverManager.chromedriver().setup();
}
}
final WebDriver raw = driverFactory.createDriver();
return new EventFiringDecorator(new HighlightElementListener())
@@ -40,4 +51,45 @@ public final class WebDriverProvider {
driver = null;
}
}
private Optional<String> detectChromeMajorVersion() {
final String os = System.getProperty("os.name", "").toLowerCase();
if (!os.contains("linux")) {
return Optional.empty();
}
final String[] commands = new String[] {
"chromium --version",
"chromium-browser --version",
"google-chrome --version"
};
for (String cmd : commands) {
final Optional<String> version = runVersionCommand(cmd);
if (version.isPresent()) {
return version;
}
}
return Optional.empty();
}
private Optional<String> runVersionCommand(String command) {
try {
final Process process = new ProcessBuilder("sh", "-c", command)
.redirectErrorStream(true)
.start();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
final String output = reader.readLine();
if (output == null) {
return Optional.empty();
}
final Matcher matcher = Pattern.compile("(\\d+)\\.").matcher(output);
if (matcher.find()) {
return Optional.of(matcher.group(1));
}
}
} catch (Exception ignored) {
// Best-effort detection only.
}
return Optional.empty();
}
}