Detect chromium version for CI ChromeDriver
This commit is contained in:
@@ -11,6 +11,10 @@ public class ChromeDriverFactory implements DriverFactory {
|
||||
final ChromeOptions options = new ChromeOptions();
|
||||
options.addArguments("--start-maximized");
|
||||
options.addArguments("--disable-notifications");
|
||||
final String binaryPath = System.getProperty("chrome.binary", "");
|
||||
if (!binaryPath.isBlank()) {
|
||||
options.setBinary(binaryPath);
|
||||
}
|
||||
final boolean headless =
|
||||
Boolean.parseBoolean(System.getProperty("selenide.headless", "false"));
|
||||
if (headless) {
|
||||
|
||||
@@ -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,8 +33,13 @@ public final class WebDriverProvider {
|
||||
|
||||
private WebDriver createDecoratedDriver() {
|
||||
if (!TestConfig.isSelenoidMode()) {
|
||||
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())
|
||||
.decorate(raw);
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user