Skip to content

Commit

Permalink
Support for chromedriver 115+ (CfT endpoints) for NPM mirror (#1264)
Browse files Browse the repository at this point in the history
  • Loading branch information
bonigarcia committed Apr 4, 2024
1 parent 55fb8f3 commit 9193a3b
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 22 deletions.
24 changes: 23 additions & 1 deletion src/main/java/io/github/bonigarcia/wdm/WebDriverManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
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.CHROME;
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;
Expand Down Expand Up @@ -1373,6 +1374,12 @@ protected boolean isUseMirror() {
return getMirrorUrl().isPresent() && config().isUseMirror();
}

protected boolean isChrome() {
DriverManagerType managerType = getDriverManagerType();
return managerType != null
&& (managerType == CHROME || managerType == CHROMIUM);
}

protected String getCurrentVersion(URL url) {
String urlFile = url.getFile();
if (isUseMirror()) {
Expand Down Expand Up @@ -1439,10 +1446,13 @@ protected UrlHandler createUrlHandler(String driverVersion)

boolean getLatest = isUnknown(driverVersion);
boolean continueSearchingVersion;
boolean isCfT = isChrome() && VersionDetector.isCfT(driverVersion);

do {
// Filter by driver name
urlHandler.filterByDriverName(shortDriverName);
if (!isCfT) {
urlHandler.filterByDriverName(shortDriverName);
}

// Filter for latest or concrete driver version
if (getLatest) {
Expand Down Expand Up @@ -1495,11 +1505,23 @@ protected UrlHandler createUrlHandler(String driverVersion)
candidateUrls = urlHandler.getCandidateUrls();
}
} while (continueSearchingVersion);

if (isCfT) {
List<URL> driversFromMirror = getMirrorUrls(
urlHandler.getCandidateUrl(), "");
urlHandler.setCandidateUrls(driversFromMirror);
urlHandler.filterByDriverName(shortDriverName);
}

return urlHandler;
}

protected List<URL> getDriversFromMirror(URL driverUrl,
String driverVersion) throws IOException {
if (isChrome() && VersionDetector.isCfT(driverVersion)) {
driverUrl = config().getChromeDriverCfTMirrorUrl();
config().setChromeDriverCfTMirrorUrl(driverUrl);
}
List<URL> urls = new ArrayList<>();
if (isNullOrEmpty(driverVersion)) {
List<URL> mirrorUrls = getMirrorUrls(driverUrl, "");
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/io/github/bonigarcia/wdm/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ public class Config {
URL.class);
ConfigKey<URL> chromeDriverMirrorUrl = new ConfigKey<>(
"wdm.chromeDriverMirrorUrl", URL.class);
ConfigKey<URL> chromeDriverCfTMirrorUrl = new ConfigKey<>(
"wdm.chromeDriverCfTMirrorUrl", URL.class);
ConfigKey<String> chromeDownloadUrlPattern = new ConfigKey<>(
"wdm.chromeDownloadUrlPattern", String.class);
ConfigKey<String> chromeGoodVersionsUrl = new ConfigKey<>(
Expand Down Expand Up @@ -765,6 +767,15 @@ public Config setChromeDriverMirrorUrl(URL value) {
return this;
}

public URL getChromeDriverCfTMirrorUrl() {
return resolve(chromeDriverCfTMirrorUrl);
}

public Config setChromeDriverCfTMirrorUrl(URL value) {
this.chromeDriverCfTMirrorUrl.setValue(value);
return this;
}

public String getChromeDownloadUrlPattern() {
return resolve(chromeDownloadUrlPattern);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,6 @@
*/
public class ChromeDriverManager extends WebDriverManager {

public static final int MIN_CHROMEDRIVER_IN_CFT = 115;

private static final String CHROMEDRIVER_DOWNLOAD_OLD_PATTERN = "https://chromedriver.storage.googleapis.com/%s/chromedriver_%s%s.zip";

@Override
Expand Down Expand Up @@ -173,9 +171,7 @@ Optional<URL> buildUrl(String driverVersion, Config config) {

String builtUrl = String.format(downloadUrlPattern, driverVersion,
label, label);
if (!isNullOrEmpty(driverVersion)
&& Integer.parseInt(VersionDetector.getMajorVersion(
driverVersion)) < MIN_CHROMEDRIVER_IN_CFT) {
if (!VersionDetector.isCfT(driverVersion)) {
archLabel = os.isWin() ? "32" : "64";
builtUrl = String.format(CHROMEDRIVER_DOWNLOAD_OLD_PATTERN,
driverVersion, os.getName(), archLabel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import static io.github.bonigarcia.wdm.config.Architecture.ARM64;
import static io.github.bonigarcia.wdm.config.DriverManagerType.CHROME;
import static io.github.bonigarcia.wdm.config.DriverManagerType.CHROMIUM;
import static io.github.bonigarcia.wdm.managers.ChromeDriverManager.MIN_CHROMEDRIVER_IN_CFT;
import static java.io.File.separator;
import static java.lang.invoke.MethodHandles.lookup;
import static java.nio.file.Files.createTempDirectory;
Expand Down Expand Up @@ -101,10 +100,7 @@ public File getTarget(String driverVersion, String driverName,
OperatingSystem os = config.getOperatingSystem();
String architecture = config.getArchitecture().toString()
.toLowerCase(ROOT);
int majorDriverVersion = Integer
.parseInt(VersionDetector.getMajorVersion(driverVersion));

if (os.isWin() && majorDriverVersion < MIN_CHROMEDRIVER_IN_CFT
if (os.isWin() && !VersionDetector.isCfT(driverVersion)
&& (driverManagerType == CHROME
|| driverManagerType == CHROMIUM)) {
log.trace(
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/io/github/bonigarcia/wdm/online/UrlHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -290,4 +290,8 @@ public URL getCandidateUrl() {
return candidateUrls.iterator().next();
}

public void setCandidateUrls(List<URL> candidateUrls) {
this.candidateUrls = candidateUrls;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

import static io.github.bonigarcia.wdm.WebDriverManager.loadXML;
import static io.github.bonigarcia.wdm.config.Config.isNullOrEmpty;
import static io.github.bonigarcia.wdm.managers.ChromeDriverManager.MIN_CHROMEDRIVER_IN_CFT;
import static io.github.bonigarcia.wdm.versions.Shell.runAndWait;
import static java.lang.invoke.MethodHandles.lookup;
import static java.nio.charset.StandardCharsets.UTF_8;
Expand Down Expand Up @@ -74,6 +73,7 @@ public class VersionDetector {
static final String COMMANDS_PROPERTIES = "commands.properties";
static final String FILE_PROTOCOL = "file";
static final String CFT_URL = "https://googlechromelabs.github.io/chrome-for-testing/";
static final int MIN_CHROMEDRIVER_IN_CFT = 115;

final Logger log = getLogger(lookup().lookupClass());

Expand Down Expand Up @@ -102,8 +102,7 @@ public Optional<String> getDriverVersionFromRepository(
if (driverName.equalsIgnoreCase("chromedriver")) {
String cftUrl = null;
try {
if (driverVersion.isPresent() && Integer.parseInt(
driverVersion.get()) >= MIN_CHROMEDRIVER_IN_CFT) {
if (driverVersion.isPresent() && isCfT(driverVersion.get())) {
// Parse JSON using GoodVersions
cftUrl = config.getChromeGoodVersionsUrl();

Expand Down Expand Up @@ -400,6 +399,11 @@ public static String getMajorVersion(String version) {
}
}

public static boolean isCfT(String driverVersion) {
return isNullOrEmpty(driverVersion) || Integer.parseInt(VersionDetector
.getMajorVersion(driverVersion)) >= MIN_CHROMEDRIVER_IN_CFT;
}

protected File findFileLocation(String filename) {
// Alternative #1: in System32 folder
File system32Folder = new File(System.getenv("SystemRoot"), "System32");
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/webdrivermanager.properties
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ wdm.serverTimeoutSec=60

wdm.chromeDriverUrl=https://chromedriver.storage.googleapis.com/
wdm.chromeDriverMirrorUrl=https://registry.npmmirror.com/-/binary/chromedriver/
wdm.chromeDriverCfTMirrorUrl=https://registry.npmmirror.com/-/binary/chrome-for-testing/
wdm.chromeDriverExport=webdriver.chrome.driver
wdm.chromeDownloadUrlPattern=https://storage.googleapis.com/chrome-for-testing-public/%s/%s/chromedriver-%s.zip
wdm.chromeGoodVersionsUrl=https://googlechromelabs.github.io/chrome-for-testing/known-good-versions-with-downloads.json
Expand Down
21 changes: 13 additions & 8 deletions src/test/java/io/github/bonigarcia/wdm/test/mirror/MirrorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import static org.assertj.core.api.Assertions.assertThatThrownBy;

import java.io.File;
import java.net.URL;
import java.util.List;

import org.junit.jupiter.api.Disabled;
Expand All @@ -39,20 +38,26 @@
class MirrorTest {

@Test
void testMirrorChrome() throws Exception {
void testMirrorChrome() {
WebDriverManager wdm = WebDriverManager.chromedriver()
.browserVersion("100").avoidBrowserDetection()
.avoidResolutionCache().clearResolutionCache().useMirror()
.clearResolutionCache().useMirror().forceDownload();
wdm.setup();
File driver = new File(wdm.getDownloadedDriverPath());
assertThat(driver).exists();
}

@Test
void testOldMirrorChrome() {
WebDriverManager wdm = WebDriverManager.chromedriver()
.browserVersion("100").clearResolutionCache().useMirror()
.forceDownload();
wdm.config().setChromeDriverMirrorUrl(new URL(
"https://registry.npmmirror.com/-/binary/chromedriver/"));
wdm.setup();
File driver = new File(wdm.getDownloadedDriverPath());
assertThat(driver).exists();
}

@Test
void testMirrorFirefox() throws Exception {
void testMirrorFirefox() {
WebDriverManager wdm = WebDriverManager.firefoxdriver().useMirror()
.forceDownload();
wdm.setup();
Expand All @@ -61,7 +66,7 @@ void testMirrorFirefox() throws Exception {
}

@Test
void testMirrorOpera() throws Exception {
void testMirrorOpera() {
WebDriverManager wdm = WebDriverManager.operadriver().useMirror()
.browserVersion("97").forceDownload();
wdm.setup();
Expand Down

0 comments on commit 9193a3b

Please sign in to comment.