Skip to content

Commit

Permalink
Merge pull request #5766 from effective-webwork/3-5-chrome-driver
Browse files Browse the repository at this point in the history
[3.5] Update retrieval of Chrome driver
  • Loading branch information
solth authored Sep 1, 2023
2 parents f1c9211 + ca24dbe commit 39a35ae
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,7 @@ public static void Initialize() throws IOException {
}

private static void provideChromeDriver() throws IOException {
String driverFileName = "chromedriver";
if (SystemUtils.IS_OS_WINDOWS) {
driverFileName = driverFileName.concat(".exe");
}
File driverFile = new File(DRIVER_DIR + driverFileName);
File driverFile = getDriverFile();

if (!driverFile.exists()) {
logger.debug("{} does not exist, providing chrome driver now", driverFile.getAbsolutePath());
Expand All @@ -113,6 +109,18 @@ private static void provideChromeDriver() throws IOException {
webDriver = new ChromeDriver(service, options);
}

private static File getDriverFile() {
String driver = WebDriverProvider.CHROME_DRIVER;
if (SystemUtils.IS_OS_WINDOWS) {
driver = WebDriverProvider.CHROME_DRIVER_WIN_SUBDIR + "/" + driver.concat(WebDriverProvider.EXE);
} else if (SystemUtils.IS_OS_MAC_OSX) {
driver = WebDriverProvider.CHROME_DRIVER_MAC_SUBDIR + "/" + driver;
} else {
driver = WebDriverProvider.CHROME_DRIVER_LINUX_SUBDIR + "/" + driver;
}
return new File(DRIVER_DIR + driver);
}

private static void provideGeckoDriver() throws IOException {
String driverFileName = "geckodriver";
if (SystemUtils.IS_OS_WINDOWS) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import javax.json.Json;
import javax.json.JsonObject;
import javax.json.JsonReader;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand All @@ -32,13 +37,22 @@ public class WebDriverProvider {

private static final Logger logger = LogManager.getLogger(WebDriverProvider.class);

// https://sites.google.com/a/chromium.org/chromedriver/downloads/version-selection
// Please don't rely on the LATEST_RELEASE file without a version suffix.
// It exists for backward compatibility only, and will be removed in the near future.
private static final String CHROME_DRIVER_LATEST_RELEASE_URL = "https://chromedriver.storage.googleapis.com/LATEST_RELEASE";

private static UnArchiver zipUnArchiver = new ZipUnArchiver();
private static UnArchiver tarGZipUnArchiver = new TarGZipUnArchiver();
// https://chromedriver.chromium.org/downloads
private static final String CHROME_DRIVER_LAST_GOOD_VERSIONS_URL
= "https://googlechromelabs.github.io/chrome-for-testing/last-known-good-versions.json";
private static final String CHROME_FOR_TESTING_URL = "https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/";
public static final String CHROME_DRIVER = "chromedriver";
private static final String CHROME_DRIVER_MAC_PREFIX = "mac-x64";
private static final String CHROME_DRIVER_WIN_PREFIX = "win32";
private static final String CHROME_DRIVER_LINUX_PREFIX = "linux64";
public static final String CHROME_DRIVER_MAC_SUBDIR = CHROME_DRIVER + "-" + CHROME_DRIVER_MAC_PREFIX;
public static final String CHROME_DRIVER_WIN_SUBDIR = CHROME_DRIVER + "-" + CHROME_DRIVER_WIN_PREFIX;
public static final String CHROME_DRIVER_LINUX_SUBDIR = CHROME_DRIVER + "-" + CHROME_DRIVER_LINUX_PREFIX;
private static final String ZIP = ".zip";
public static final String EXE = ".exe";
private static final String ZIP_FILE = CHROME_DRIVER + ZIP;
private static final UnArchiver zipUnArchiver = new ZipUnArchiver();
private static final UnArchiver tarGZipUnArchiver = new TarGZipUnArchiver();

/**
* Downloads Geckodriver, extracts archive file and set system property
Expand Down Expand Up @@ -68,8 +82,8 @@ public static void provideGeckoDriver(String geckoDriverVersion, String download
FileUtils.copyURLToFile(new URL(geckoDriverUrl + "geckodriver-v" + geckoDriverVersion + "-macos.tar.gz"),
geckoDriverTarFile);
File theDir = new File(extractFolder);
if (!theDir.exists()) {
theDir.mkdir();
if (!theDir.mkdir()) {
logger.error("Unable to create directory '" + theDir.getPath() + "'!");
}
extractTarFileToFolder(geckoDriverTarFile, theDir);
} else {
Expand Down Expand Up @@ -107,35 +121,36 @@ public static void provideGeckoDriver(String geckoDriverVersion, String download
*/
public static void provideChromeDriver(String downloadFolder, String extractFolder)
throws IOException {

String chromeDriverVersion = fetchLatestStableChromeDriverVersion();

String chromeDriverUrl = "https://chromedriver.storage.googleapis.com/" + chromeDriverVersion + "/";
String chromeDriverFileName;
String chromeDriverUrl = CHROME_FOR_TESTING_URL + chromeDriverVersion + File.separator;
String driverFilename = CHROME_DRIVER;
File chromeDriverFile;
if (SystemUtils.IS_OS_WINDOWS) {
chromeDriverFileName = "chromedriver.exe";
File chromeDriverZipFile = new File(downloadFolder + "chromedriver.zip");
FileUtils.copyURLToFile(new URL(chromeDriverUrl + "chromedriver_win32.zip"), chromeDriverZipFile);
extractZipFileToFolder(chromeDriverZipFile, new File(extractFolder));

driverFilename = driverFilename + EXE;
File chromeDriverZipFile = new File(downloadFolder + CHROME_DRIVER_WIN_SUBDIR + File.separator + ZIP_FILE);
FileUtils.copyURLToFile(new URL(chromeDriverUrl + CHROME_DRIVER_WIN_PREFIX + File.separator
+ CHROME_DRIVER_WIN_SUBDIR + ZIP), chromeDriverZipFile);
chromeDriverFile = extractZipFileToFolder(chromeDriverZipFile, new File(extractFolder), driverFilename,
CHROME_DRIVER_WIN_SUBDIR);
} else if (SystemUtils.IS_OS_MAC_OSX) {
chromeDriverFileName = "chromedriver";
File chromeDriverZipFile = new File(downloadFolder + "chromedriver.zip");
FileUtils.copyURLToFile(new URL(chromeDriverUrl + "chromedriver_mac64.zip"), chromeDriverZipFile);
File chromeDriverZipFile = new File(downloadFolder + CHROME_DRIVER_MAC_SUBDIR + File.separator + ZIP_FILE);
FileUtils.copyURLToFile(new URL(chromeDriverUrl + CHROME_DRIVER_MAC_PREFIX + File.separator
+ CHROME_DRIVER_MAC_SUBDIR + ZIP), chromeDriverZipFile);
File theDir = new File(extractFolder);
if (!theDir.exists()) {
theDir.mkdir();
if (!theDir.mkdir()) {
logger.error("Unable to create directory '" + theDir.getPath() + "'!");
}
}
extractZipFileToFolder(chromeDriverZipFile, new File(extractFolder));

chromeDriverFile = extractZipFileToFolder(chromeDriverZipFile, new File(extractFolder), driverFilename,
CHROME_DRIVER_MAC_SUBDIR);
} else {
chromeDriverFileName = "chromedriver";
File chromeDriverZipFile = new File(downloadFolder + "chromedriver.zip");
FileUtils.copyURLToFile(new URL(chromeDriverUrl + "chromedriver_linux64.zip"), chromeDriverZipFile);
extractZipFileToFolder(chromeDriverZipFile, new File(extractFolder));
File chromeDriverZipFile = new File(downloadFolder + CHROME_DRIVER_LINUX_SUBDIR + File.separator + ZIP_FILE);
FileUtils.copyURLToFile(new URL(chromeDriverUrl + CHROME_DRIVER_LINUX_PREFIX + File.separator
+ CHROME_DRIVER_LINUX_SUBDIR + ZIP), chromeDriverZipFile);
chromeDriverFile = extractZipFileToFolder(chromeDriverZipFile, new File(extractFolder), driverFilename,
CHROME_DRIVER_LINUX_SUBDIR);
}
File chromeDriverFile = new File(extractFolder, chromeDriverFileName);

if (chromeDriverFile.exists()) {
if (!SystemUtils.IS_OS_WINDOWS) {
ExecutionPermission.setExecutePermission(chromeDriverFile);
Expand All @@ -151,6 +166,13 @@ public static void provideChromeDriver(String downloadFolder, String extractFold
}
}

private static File extractZipFileToFolder(File zipFile, File destinationFolder, String chromeDriverFilename,
String chromeDriverVersion) {
extractZipFileToFolder(zipFile, destinationFolder);
return new File(destinationFolder.toURI().resolve(chromeDriverVersion + '/')
.resolve(chromeDriverFilename));
}

private static void extractZipFileToFolder(File zipFile, File destinationFolder) {
zipUnArchiver.setSourceFile(zipFile);
zipUnArchiver.extract("", destinationFolder);
Expand All @@ -170,24 +192,19 @@ private static void extractTarFileToFolder(File file, File destinationFolder) {
}

private static String fetchLatestStableChromeDriverVersion() {

String version = "";
try {

URL url = new URL(CHROME_DRIVER_LATEST_RELEASE_URL);
URLConnection urlConnection = url.openConnection();

BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

StringBuilder content = new StringBuilder();
content.append(bufferedReader.readLine());

bufferedReader.close();

version = content.toString();

logger.info("Latest Chrome Driver Release found: {}", version);

URL url = new URL(CHROME_DRIVER_LAST_GOOD_VERSIONS_URL);
try (BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(url.openStream()))) {
String content = bufferedReader.readLine();
JsonReader jsonReader = Json.createReader(new StringReader(content));
JsonObject jsonObject = jsonReader.readObject();
version = jsonObject.get("channels").asJsonObject().get("Stable").asJsonObject().get("version")
.toString();
version = StringUtils.strip(version, "\"");
jsonReader.close();
logger.info("Latest Chrome Driver Release found: {}", version);
}
} catch (MalformedURLException exception) {
logger.error("URL for fetching Chrome Release is malformed.");
} catch (IOException exception) {
Expand Down

0 comments on commit 39a35ae

Please sign in to comment.