io.github.bonigarcia.wdm.WebDriverManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of webdrivermanager Show documentation
Show all versions of webdrivermanager Show documentation
Automated driver management and other helper features for Selenium
WebDriver in Java
/*
* (C) Copyright 2015 Boni Garcia (https://bonigarcia.github.io/)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package io.github.bonigarcia.wdm;
import static io.github.bonigarcia.wdm.config.Architecture.ARM64;
import static io.github.bonigarcia.wdm.config.Architecture.X32;
import static io.github.bonigarcia.wdm.config.Architecture.X64;
import static io.github.bonigarcia.wdm.config.Config.isNullOrEmpty;
import static io.github.bonigarcia.wdm.config.DriverManagerType.CHROMIUM;
import static io.github.bonigarcia.wdm.config.DriverManagerType.EDGE;
import static io.github.bonigarcia.wdm.config.DriverManagerType.FIREFOX;
import static io.github.bonigarcia.wdm.config.DriverManagerType.IEXPLORER;
import static io.github.bonigarcia.wdm.config.DriverManagerType.OPERA;
import static io.github.bonigarcia.wdm.config.OperatingSystem.LINUX;
import static io.github.bonigarcia.wdm.config.OperatingSystem.MAC;
import static io.github.bonigarcia.wdm.config.OperatingSystem.WIN;
import static io.github.bonigarcia.wdm.docker.DockerService.NETWORK_HOST;
import static io.github.bonigarcia.wdm.online.Downloader.deleteFile;
import static io.github.bonigarcia.wdm.versions.Shell.runAndWait;
import static io.github.bonigarcia.wdm.versions.VersionDetector.getWdmVersion;
import static java.lang.Integer.parseInt;
import static java.lang.String.valueOf;
import static java.lang.System.getenv;
import static java.lang.invoke.MethodHandles.lookup;
import static java.nio.charset.Charset.defaultCharset;
import static java.util.Collections.sort;
import static java.util.Locale.ROOT;
import static java.util.Optional.empty;
import static java.util.regex.Pattern.CASE_INSENSITIVE;
import static java.util.regex.Pattern.compile;
import static javax.xml.xpath.XPathConstants.NODESET;
import static javax.xml.xpath.XPathFactory.newInstance;
import static org.apache.commons.io.FileUtils.cleanDirectory;
import static org.apache.commons.io.FilenameUtils.removeExtension;
import static org.apache.commons.io.FilenameUtils.separatorsToUnix;
import static org.apache.commons.lang3.StringUtils.isNumeric;
import static org.apache.commons.lang3.SystemUtils.IS_OS_LINUX;
import static org.slf4j.LoggerFactory.getLogger;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Scanner;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Function;
import java.util.logging.Level;
import java.util.regex.Matcher;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import javax.xml.namespace.NamespaceContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.openqa.selenium.Capabilities;
import org.openqa.selenium.MutableCapabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.chromium.ChromiumOptions;
import org.openqa.selenium.edge.EdgeOptions;
import org.openqa.selenium.firefox.HasExtensions;
import org.openqa.selenium.logging.LogEntries;
import org.openqa.selenium.logging.LogEntry;
import org.openqa.selenium.logging.LogType;
import org.openqa.selenium.logging.LoggingPreferences;
import org.openqa.selenium.remote.Augmenter;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.SessionId;
import org.slf4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.internal.LinkedTreeMap;
import io.github.bonigarcia.wdm.cache.CacheHandler;
import io.github.bonigarcia.wdm.cache.ResolutionCache;
import io.github.bonigarcia.wdm.config.Architecture;
import io.github.bonigarcia.wdm.config.Config;
import io.github.bonigarcia.wdm.config.DriverManagerType;
import io.github.bonigarcia.wdm.config.OperatingSystem;
import io.github.bonigarcia.wdm.config.WebDriverManagerException;
import io.github.bonigarcia.wdm.docker.DockerContainer;
import io.github.bonigarcia.wdm.docker.DockerService;
import io.github.bonigarcia.wdm.managers.ChromeDriverManager;
import io.github.bonigarcia.wdm.managers.ChromiumDriverManager;
import io.github.bonigarcia.wdm.managers.EdgeDriverManager;
import io.github.bonigarcia.wdm.managers.FirefoxDriverManager;
import io.github.bonigarcia.wdm.managers.InternetExplorerDriverManager;
import io.github.bonigarcia.wdm.managers.OperaDriverManager;
import io.github.bonigarcia.wdm.managers.SafariDriverManager;
import io.github.bonigarcia.wdm.managers.VoidDriverManager;
import io.github.bonigarcia.wdm.online.Downloader;
import io.github.bonigarcia.wdm.online.GitHubApi;
import io.github.bonigarcia.wdm.online.HttpClient;
import io.github.bonigarcia.wdm.online.NpmMirror;
import io.github.bonigarcia.wdm.online.S3NamespaceContext;
import io.github.bonigarcia.wdm.online.UrlHandler;
import io.github.bonigarcia.wdm.versions.VersionComparator;
import io.github.bonigarcia.wdm.versions.VersionDetector;
import io.github.bonigarcia.wdm.webdriver.WebDriverBrowser;
import io.github.bonigarcia.wdm.webdriver.WebDriverCreator;
/**
* Parent driver manager.
*
* @author Boni Garcia
* @since 2.1.0
*/
public abstract class WebDriverManager {
protected static final Logger log = getLogger(lookup().lookupClass());
protected static final String SLASH = "/";
protected static final String DASH = "-";
protected static final String LATEST_RELEASE = "LATEST_RELEASE";
protected static final String CFT_LABEL = "chrome-for-testing-public";
protected static final NamespaceContext S3_NAMESPACE_CONTEXT = new S3NamespaceContext();
protected static final String IN_DOCKER = "-in-docker";
protected static final String CLI_SERVER = "server";
protected static final String CLI_RESOLVER = "resolveDriverFor";
protected static final String CLI_DOCKER = "runInDocker";
protected static final String BROWSER_WATCHER_ID = "kbnnckbeejhjlljpgelfponodpecfapp";
protected abstract List getDriverUrls(String driverVersion)
throws IOException;
protected abstract String getDriverName();
protected abstract String getDriverVersion();
protected abstract void setDriverVersion(String driverVersion);
protected abstract String getBrowserVersion();
protected abstract void setBrowserVersion(String browserVersion);
protected abstract void setDriverUrl(URL url);
protected abstract URL getDriverUrl();
protected abstract Optional getMirrorUrl();
protected abstract Optional getExportParameter();
public abstract DriverManagerType getDriverManagerType();
public abstract WebDriverManager exportParameter(String exportParameter);
protected Config config;
protected HttpClient httpClient;
protected Downloader downloader;
protected ResolutionCache resolutionCache;
protected CacheHandler cacheHandler;
protected VersionDetector versionDetector;
protected WebDriverCreator webDriverCreator;
protected DockerService dockerService;
protected int retryCount = 0;
protected Capabilities capabilities;
protected boolean shutdownHook = false;
protected boolean dockerEnabled = false;
protected boolean androidEnabled = false;
protected boolean watchEnabled = false;
protected boolean displayEnabled = false;
protected boolean disableCsp = false;
protected boolean isHeadless = false;
protected List webDriverList;
protected String resolvedBrowserVersion;
protected String downloadedDriverVersion;
protected String downloadedDriverPath;
protected WebDriverManager() {
Optional wdVersion = getWdmVersion(getClass());
if (wdVersion.isPresent()) {
log.debug("Using WebDriverManager {}", wdVersion.get());
}
config = new Config();
webDriverList = new CopyOnWriteArrayList<>();
}
public synchronized Config config() {
return Optional.ofNullable(config).orElse(new Config());
}
public static synchronized WebDriverManager chromedriver() {
return new ChromeDriverManager();
}
public static synchronized WebDriverManager chromiumdriver() {
return new ChromiumDriverManager();
}
public static synchronized WebDriverManager firefoxdriver() {
return new FirefoxDriverManager();
}
public static synchronized WebDriverManager operadriver() {
return new OperaDriverManager();
}
public static synchronized WebDriverManager edgedriver() {
return new EdgeDriverManager();
}
public static synchronized WebDriverManager iedriver() {
return new InternetExplorerDriverManager();
}
public static synchronized WebDriverManager safaridriver() {
return new SafariDriverManager();
}
protected static synchronized WebDriverManager voiddriver() {
return new VoidDriverManager();
}
public static synchronized WebDriverManager getInstance(
DriverManagerType driverManagerType) {
// This condition is necessary for compatibility between Selenium 3 and
// 4 (since in Selenium 4, the class
// org.openqa.selenium.chromium.ChromiumDriver is not available)
if (driverManagerType == CHROMIUM) {
return chromiumdriver();
}
return getDriver(driverManagerType.browserClass());
}
public static synchronized WebDriverManager getInstance(
String browserName) {
DriverManagerType managerType;
String browserNameUpperCase = browserName.toUpperCase(ROOT);
switch (browserNameUpperCase) {
case "OPERABLINK":
managerType = OPERA;
break;
case "MSEDGE":
case "MICROSOFTEDGE":
managerType = EDGE;
break;
case "INTERNET EXPLORER":
managerType = IEXPLORER;
break;
default:
try {
managerType = DriverManagerType.valueOf(browserNameUpperCase);
} catch (Exception e) {
String errorMessage = String.format(
"The browser name '%s' is not recognized", browserName);
log.trace(errorMessage);
throw new WebDriverManagerException(errorMessage, e);
}
break;
}
return getInstance(managerType);
}
public static synchronized WebDriverManager getInstance(
Class extends WebDriver> webDriverClass) {
return getDriver(webDriverClass.getName());
}
protected static synchronized WebDriverManager getDriver(
String webDriverClass) {
switch (webDriverClass) {
case "org.openqa.selenium.chrome.ChromeDriver":
return chromedriver();
case "org.openqa.selenium.chromium.ChromiumDriver":
return chromiumdriver();
case "org.openqa.selenium.firefox.FirefoxDriver":
return firefoxdriver();
case "org.openqa.selenium.opera.OperaDriver":
return operadriver();
case "org.openqa.selenium.ie.InternetExplorerDriver":
return iedriver();
case "org.openqa.selenium.edge.EdgeDriver":
return edgedriver();
case "org.openqa.selenium.safari.SafariDriver":
return safaridriver();
default:
return voiddriver();
}
}
public static synchronized WebDriverManager getInstance() {
WebDriverManager manager = voiddriver();
String defaultBrowser = manager.config().getDefaultBrowser();
try {
if (defaultBrowser.contains(IN_DOCKER)) {
defaultBrowser = defaultBrowser.substring(0,
defaultBrowser.indexOf(IN_DOCKER));
manager = getInstance(DriverManagerType
.valueOf(defaultBrowser.toUpperCase(ROOT)));
manager.dockerEnabled = true;
} else {
manager = getInstance(DriverManagerType
.valueOf(defaultBrowser.toUpperCase(ROOT)));
}
return manager;
} catch (Exception e) {
log.error("Error trying to get manager for browser {}",
defaultBrowser, e);
}
return manager;
}
public static Path zipFolder(Path sourceFolder) {
Path zipFile = null;
try {
zipFile = Files.createTempFile("", ".zip");
try (ZipOutputStream zipOutputStream = new ZipOutputStream(
Files.newOutputStream(zipFile));
Stream paths = Files.walk(sourceFolder)) {
paths.filter(path -> !Files.isDirectory(path)).forEach(path -> {
ZipEntry zipEntry = new ZipEntry(separatorsToUnix(
sourceFolder.relativize(path).toString()));
try {
zipOutputStream.putNextEntry(zipEntry);
Files.copy(path, zipOutputStream);
zipOutputStream.closeEntry();
} catch (IOException e) {
log.warn("Exception adding entry {} to zip", zipEntry,
e);
}
});
}
log.debug("Zipping {} folder to {}", sourceFolder, zipFile);
} catch (IOException e) {
log.warn("Exception zipping folder {}", sourceFolder, e);
}
return zipFile;
}
public static boolean isDockerAvailable() {
String dockerInfo = runAndWait(false, "docker", "info");
return !isNullOrEmpty(dockerInfo) && !dockerInfo.contains("error")
&& dockerInfo.contains("linux");
}
public static boolean isOnline(String url) {
try {
return isOnline(new URL(url));
} catch (MalformedURLException e) {
return false;
}
}
public static boolean isOnline(URL url) {
try {
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
return connection.getResponseCode() == 200;
} catch (Exception e) {
return false;
}
}
public synchronized void setup() {
cacheHandler = new CacheHandler(config());
httpClient = new HttpClient(config());
downloader = new Downloader(getHttpClient(), config(),
this::postDownload);
if (config().isClearDriverCache()) {
clearDriverCache();
}
if (config().isClearResolutionCache()) {
clearResolutionCache();
}
if (isUsingDocker() || !isNullOrEmpty(config().getRemoteAddress())) {
return;
}
if (getDriverManagerType() != null) {
manage(getDriverVersion());
}
}
public synchronized WebDriver create() {
setup();
return instantiateDriver();
}
public synchronized List create(int numberOfBrowser) {
List browserList = new ArrayList<>();
for (int i = 0; i < numberOfBrowser; i++) {
if (i == 0) {
setup();
}
browserList.add(instantiateDriver());
}
return browserList;
}
public Optional getBrowserPath() {
return getVersionDetector().getBrowserPath(
getDriverManagerType().getBrowserNameLowerCase());
}
public WebDriverManager browserInDocker() {
dockerEnabled = true;
return this;
}
protected boolean isUsingDocker() {
return dockerEnabled && getDockerService().getDockerClient() != null;
}
public WebDriverManager browserInDockerAndroid() {
throw new WebDriverManagerException(
getDriverManagerType().getBrowserName()
+ " is not available in Docker Android");
}
public WebDriverManager dockerEnvVariables(String... defaultEnvs) {
config().setDockerEnvVariables(defaultEnvs);
return this;
}
public WebDriverManager dockerDefaultArgs(String defaultArgs) {
config().setDockerDefaultArgs(defaultArgs);
return this;
}
public WebDriverManager dockerDaemonUrl(String daemonUrl) {
config().setDockerDaemonUrl(daemonUrl);
return this;
}
public WebDriverManager dockerNetwork(String network) {
config().setDockerNetwork(network);
return this;
}
public WebDriverManager dockerNetworkHost() {
config().setDockerNetwork(NETWORK_HOST);
return this;
}
public WebDriverManager dockerTimezone(String timezone) {
config().setDockerTimezone(timezone);
return this;
}
public WebDriverManager dockerLang(String lang) {
config().setDockerLang(lang);
return this;
}
public WebDriverManager dockerShmSize(String size) {
config().setDockerShmSize(size);
return this;
}
public WebDriverManager dockerTmpfsSize(String size) {
config().setDockerTmpfsSize(size);
return this;
}
public WebDriverManager dockerTmpfsMount(String mount) {
config().setDockerTmpfsMount(mount);
return this;
}
public WebDriverManager dockerVolumes(String... volumes) {
config().setDockerVolumes(String.join(",", volumes));
return this;
}
public WebDriverManager dockerExtraHosts(String... hosts) {
config().setDockerExtraHosts(String.join(",", hosts));
return this;
}
public WebDriverManager dockerScreenResolution(String screenResolution) {
config().setDockerScreenResolution(screenResolution);
return this;
}
public WebDriverManager dockerRecordingFrameRate(int frameRate) {
config().setDockerRecordingFrameRate(frameRate);
return this;
}
public WebDriverManager dockerAvoidPulling() {
config().setDockerAvoidPulling(true);
return this;
}
public WebDriverManager avoidDockerLocalFallback() {
config().setDockerLocalFallback(false);
return this;
}
public WebDriverManager avoidShutdownHook() {
config().setAvoidShutdownHook(true);
return this;
}
public WebDriverManager avoidExternalConnections() {
config().setAvoidExternalConnections(true);
return this;
}
public WebDriverManager enableVnc() {
config().setDockerEnabledVnc(true);
return this;
}
public WebDriverManager viewOnly() {
config().setDockerViewOnly(true);
return this;
}
public WebDriverManager enableRecording() {
config().setDockerEnabledRecording(true);
return this;
}
public WebDriverManager disableTracing() {
config().setEnableTracing(false);
return this;
}
public WebDriverManager dockerRecordingPrefix(String prefix) {
config().setDockerRecordingPrefix(prefix);
return this;
}
public WebDriverManager dockerRecordingOutput(String path) {
return dockerRecordingOutput(Paths.get(path));
}
public WebDriverManager dockerRecordingOutput(Path path) {
config().setDockerRecordingOutput(path);
return this;
}
public WebDriverManager dockerPrivateEndpoint(String endpoint) {
config().setDockerPrivateEndpoint(endpoint);
return this;
}
public WebDriverManager dockerStopTimeoutSec(Integer timeout) {
config().setDockerStopTimeoutSec(timeout);
return this;
}
public WebDriverManager watch() {
watchEnabled = true;
return this;
}
public WebDriverManager watchAndDisplay() {
displayEnabled = true;
return this;
}
public WebDriverManager disableCsp() {
disableCsp = true;
return this;
}
public WebDriverManager capabilities(Capabilities capabilities) {
this.capabilities = capabilities;
return this;
}
public WebDriverManager remoteAddress(String remoteAddress) {
config().setRemoteAddress(remoteAddress);
return this;
}
public WebDriverManager remoteAddress(URL remoteAddress) {
config().setRemoteAddress(remoteAddress.toString());
return this;
}
public WebDriverManager dockerCustomImage(String dockerImage) {
config().setDockerCustomImage(dockerImage);
return this;
}
public WebDriverManager driverVersion(String driverVersion) {
setDriverVersion(driverVersion);
return this;
}
public WebDriverManager browserVersion(String browserVersion) {
setBrowserVersion(browserVersion);
return this;
}
public WebDriverManager architecture(Architecture architecture) {
config().setArchitecture(architecture);
return this;
}
public WebDriverManager arch32() {
architecture(X32);
return this;
}
public WebDriverManager arch64() {
architecture(X64);
return this;
}
public WebDriverManager arm64() {
architecture(ARM64);
return this;
}
public WebDriverManager win() {
operatingSystem(WIN);
return this;
}
public WebDriverManager linux() {
operatingSystem(LINUX);
return this;
}
public WebDriverManager mac() {
operatingSystem(MAC);
return this;
}
public WebDriverManager operatingSystem(OperatingSystem os) {
config().setOs(os.name());
return this;
}
public WebDriverManager forceDownload() {
config().setForceDownload(true);
return this;
}
public WebDriverManager driverRepositoryUrl(URL url) {
setDriverUrl(url);
return this;
}
public WebDriverManager useMirror() {
Optional mirrorUrl = getMirrorUrl();
if (!mirrorUrl.isPresent()) {
throw new WebDriverManagerException("Mirror URL not available");
}
config().setUseMirror(true);
return this;
}
public WebDriverManager proxy(String proxy) {
config().setProxy(proxy);
return this;
}
public WebDriverManager proxyUser(String proxyUser) {
config().setProxyUser(proxyUser);
return this;
}
public WebDriverManager proxyPass(String proxyPass) {
config().setProxyPass(proxyPass);
return this;
}
public WebDriverManager useBetaVersions() {
config().setUseBetaVersions(true);
return this;
}
public WebDriverManager ignoreDriverVersions(String... driverVersions) {
config().setIgnoreVersions(driverVersions);
return this;
}
public WebDriverManager gitHubToken(String gitHubToken) {
config().setGitHubToken(gitHubToken);
return this;
}
public WebDriverManager timeout(int timeout) {
config().setTimeout(timeout);
return this;
}
public WebDriverManager properties(String properties) {
config().setProperties(properties);
return this;
}
public WebDriverManager cachePath(String cachePath) {
config().setCachePath(cachePath);
return this;
}
public WebDriverManager resolutionCachePath(String resolutionCachePath) {
config().setResolutionCachePath(resolutionCachePath);
return this;
}
public WebDriverManager avoidExport() {
config().setAvoidExport(true);
return this;
}
public WebDriverManager avoidOutputTree() {
config().setAvoidOutputTree(true);
return this;
}
public WebDriverManager avoidBrowserDetection() {
config().setAvoidBrowserDetection(true);
return this;
}
public WebDriverManager avoidResolutionCache() {
config().setAvoidResolutionCache(true);
return this;
}
public WebDriverManager avoidFallback() {
config().setAvoidFallback(true);
return this;
}
public WebDriverManager avoidTmpFolder() {
config().setAvoidTmpFolder(true);
return this;
}
public WebDriverManager avoidUseChromiumDriverSnap() {
config().setUseChromiumDriverSnap(false);
return this;
}
public WebDriverManager ttl(int seconds) {
config().setTtl(seconds);
return this;
}
public WebDriverManager ttlBrowsers(int seconds) {
config().setTtlForBrowsers(seconds);
return this;
}
public WebDriverManager browserVersionDetectionCommand(
String browserVersionCommand) {
config().setBrowserVersionDetectionCommand(browserVersionCommand);
return this;
}
public WebDriverManager useLocalCommandsPropertiesFirst() {
config().setCommandsPropertiesOnlineFirst(false);
return this;
}
public WebDriverManager commandsPropertiesUrl(URL url) {
config().setCommandsPropertiesUrl(url);
return this;
}
public WebDriverManager clearResolutionCache() {
getResolutionCache().clear();
return this;
}
public WebDriverManager clearDriverCache() {
File cacheFolder = config().getCacheFolder();
try {
log.debug("Clearing driver cache at {}", cacheFolder);
cleanDirectory(cacheFolder);
} catch (Exception e) {
log.warn("Exception deleting driver cache at {}", cacheFolder, e);
}
return this;
}
public WebDriverManager browserVersionDetectionRegex(String regex) {
config().setBrowserVersionDetectionRegex(regex);
return this;
}
public void reset() {
config().reset();
retryCount = 0;
shutdownHook = false;
dockerEnabled = false;
androidEnabled = false;
watchEnabled = false;
displayEnabled = false;
capabilities = null;
resolvedBrowserVersion = null;
}
// ------------
public String getDownloadedDriverPath() {
return downloadedDriverPath;
}
public String getDownloadedDriverVersion() {
return downloadedDriverVersion;
}
public List getDriverVersions() {
List driverVersionList = new ArrayList<>();
try {
List driverUrls = isUseMirror()
? getDriversFromMirror(getMirrorUrl().get(), "")
: getDriverUrls("");
for (URL url : driverUrls) {
String driverVersion = getCurrentVersion(url);
if (driverVersion.isEmpty()
|| driverVersion.equalsIgnoreCase("icons")
|| driverVersion.equalsIgnoreCase(getDriverName())) {
continue;
}
if (!driverVersionList.contains(driverVersion)) {
driverVersionList.add(driverVersion);
}
}
log.trace("Driver version list before sorting {}",
driverVersionList);
sort(driverVersionList, new VersionComparator());
return driverVersionList;
} catch (IOException e) {
throw new WebDriverManagerException(e);
}
}
public WebDriver getWebDriver() {
List driverList = getWebDriverList();
return driverList.isEmpty() ? null : driverList.iterator().next();
}
public List getWebDriverList() {
List webdriverList = new ArrayList<>();
if (webDriverList.isEmpty()) {
log.warn("WebDriver object(s) not available");
} else {
webdriverList = webDriverList.stream()
.map(WebDriverBrowser::getDriver)
.collect(Collectors.toList());
}
return webdriverList;
}
public synchronized void quit() {
webDriverList.stream().forEach(this::quit);
webDriverList.clear();
}
public synchronized void quit(WebDriver driver) {
Optional webDriverBrowser = findWebDriverBrowser(
driver);
if (webDriverBrowser.isPresent()) {
WebDriverBrowser driverBrowser = webDriverBrowser.get();
quit(driverBrowser);
webDriverList.remove(driverBrowser);
}
}
public synchronized void stopDockerRecording() {
webDriverList.stream().forEach(this::stopDockerRecording);
}
public synchronized void stopDockerRecording(WebDriver driver) {
Optional webDriverBrowser = findWebDriverBrowser(
driver);
if (webDriverBrowser.isPresent()) {
stopDockerRecording(webDriverBrowser.get());
}
}
protected synchronized void stopDockerRecording(
WebDriverBrowser driverBrowser) {
List dockerContainerList = driverBrowser
.getDockerContainerList();
if (dockerContainerList != null && !dockerContainerList.isEmpty()) {
DockerContainer recorderContainer = dockerContainerList.get(0);
if (recorderContainer.getImageId()
.equals(config().getDockerRecordingImage())) {
getDockerService().stopAndRemoveContainer(recorderContainer);
dockerContainerList.remove(0);
}
}
}
protected synchronized void quit(WebDriverBrowser driverBrowser) {
try {
WebDriver driver = driverBrowser.getDriver();
if (driver != null) {
SessionId sessionId = ((RemoteWebDriver) driver).getSessionId();
if (sessionId != null) {
log.debug("Quitting {}", driver);
driver.quit();
}
}
List dockerContainerList = driverBrowser
.getDockerContainerList();
if (dockerContainerList != null) {
dockerContainerList.stream()
.forEach(getDockerService()::stopAndRemoveContainer);
}
} catch (Exception e) {
log.warn("Exception closing {} ({})", driverBrowser.getDriver(),
e.getMessage(), e);
}
}
public String getDockerBrowserContainerId(WebDriver driver) {
return (String) getPropertyFromWebDriverBrowser(driver,
WebDriverBrowser::getBrowserContainerId);
}
public String getDockerBrowserContainerId() {
return (String) getPropertyFromFirstWebDriverBrowser(
WebDriverBrowser::getBrowserContainerId);
}
public URL getDockerSeleniumServerUrl(WebDriver driver) {
return (URL) getPropertyFromWebDriverBrowser(driver,
WebDriverBrowser::getSeleniumServerUrl);
}
public URL getDockerSeleniumServerUrl() {
return (URL) getPropertyFromFirstWebDriverBrowser(
WebDriverBrowser::getSeleniumServerUrl);
}
public URL getDockerNoVncUrl(WebDriver driver) {
return (URL) getPropertyFromWebDriverBrowser(driver,
WebDriverBrowser::getNoVncUrl);
}
public URL getDockerNoVncUrl() {
return (URL) getPropertyFromFirstWebDriverBrowser(
WebDriverBrowser::getNoVncUrl);
}
public String getDockerVncUrl(WebDriver driver) {
return (String) getPropertyFromWebDriverBrowser(driver,
WebDriverBrowser::getVncUrl);
}
public String getDockerVncUrl() {
return (String) getPropertyFromFirstWebDriverBrowser(
WebDriverBrowser::getVncUrl);
}
public Path getDockerRecordingPath(WebDriver driver) {
return (Path) getPropertyFromWebDriverBrowser(driver,
WebDriverBrowser::getRecordingPath);
}
public Path getDockerRecordingPath() {
return (Path) getPropertyFromFirstWebDriverBrowser(
WebDriverBrowser::getRecordingPath);
}
public void startRecording(WebDriver driver) {
Optional webDriverBrowser = findWebDriverBrowser(
driver);
if (webDriverBrowser.isPresent()) {
webDriverBrowser.get().startRecording();
}
}
public void startRecording() {
webDriverList.get(0).startRecording();
}
public void startRecording(WebDriver driver, String recordingName) {
Optional webDriverBrowser = findWebDriverBrowser(
driver);
if (webDriverBrowser.isPresent()) {
webDriverBrowser.get().startRecording(recordingName);
}
}
public void startRecording(String recordingName) {
webDriverList.get(0).startRecording(recordingName);
}
public void stopRecording(WebDriver driver) {
Optional webDriverBrowser = findWebDriverBrowser(
driver);
if (webDriverBrowser.isPresent()) {
webDriverBrowser.get().stopRecording();
}
}
public void stopRecording() {
webDriverList.get(0).stopRecording();
}
@SuppressWarnings("unchecked")
public List