Skip to content

Commit

Permalink
fix: Retry when WebDriver updates are not available yet (#24)
Browse files Browse the repository at this point in the history
Some WebDriver updates for Chrome and Edge lag behind the corresponding
browser releases. This will automatically retry with the previous 2
releases when a 404 is encountered.

This fixes the failure on Chrome 115 today (ChromeDriver 115 is not out
yet).
  • Loading branch information
joeyparrish authored Jul 19, 2023
1 parent 07c8644 commit e0312c8
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 8 deletions.
8 changes: 5 additions & 3 deletions chrome.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ class ChromeWebDriverInstaller extends WebDriverInstallerBase {
* @return {!Promise<string>}
*/
async getBestDriverVersion(browserVersion) {
const majorVersion = browserVersion.split('.')[0];
const versionUrl = `${CDN_URL}/LATEST_RELEASE_${majorVersion}`;
return await InstallerUtils.fetchVersionUrl(versionUrl);
const idealMajorVersion = parseInt(browserVersion.split('.')[0], 10);
return await InstallerUtils.fetchVersionUrlWithAutomaticDowngrade(
idealMajorVersion,
/* minMajorVersion */ idealMajorVersion - 2,
(majorVersion) => `${CDN_URL}/LATEST_RELEASE_${majorVersion}`);
}

/**
Expand Down
13 changes: 10 additions & 3 deletions edge.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class EdgeWebDriverInstaller extends WebDriverInstallerBase {
* @return {!Promise<string>}
*/
async getBestDriverVersion(browserVersion) {
const majorVersion = browserVersion.split('.')[0];
const idealMajorVersion = parseInt(browserVersion.split('.')[0], 10);

let platform;
if (os.platform() == 'linux') {
Expand All @@ -76,8 +76,15 @@ class EdgeWebDriverInstaller extends WebDriverInstallerBase {
throw new Error(`Unrecognized platform: ${os.platform()}`);
}

const versionUrl = `${CDN_URL}/LATEST_RELEASE_${majorVersion}_${platform}`;
return await InstallerUtils.fetchVersionUrl(versionUrl, 'UTF-16LE');
const urlFormatter = (majorVersion) => {
return `${CDN_URL}/LATEST_RELEASE_${majorVersion}_${platform}`;
};

return await InstallerUtils.fetchVersionUrlWithAutomaticDowngrade(
idealMajorVersion,
/* minMajorVersion */ idealMajorVersion - 2,
urlFormatter,
'UTF-16LE');
}

/**
Expand Down
49 changes: 47 additions & 2 deletions utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,8 @@ class InstallerUtils {
const response = await fetch(url);
if (!response.ok) {
throw new Error(
`Failed to fetch ${url}: ${response.status} ${response.statusText}`);
`Failed to fetch ${url}: ${response.status} ${response.statusText}`,
{ cause: response });
}
return response;
}
Expand Down Expand Up @@ -228,7 +229,7 @@ class InstallerUtils {
}

/**
* Fetch a version number from a URL. Both Chrome and Edge use this.
* Fetch a version number from a URL.
*
* @param {string} url
* @param {string=} encoding
Expand All @@ -241,6 +242,50 @@ class InstallerUtils {
return data.toString(encoding).trim();
}

/**
* Fetch a version number from a URL. If not found, downgrade the major
* version and try again. If a WebDriver release lags the browser release
* (which seems common), this will compensate. Both Chrome and Edge use
* this.
*
* @param {number} idealMajorVersion
* @param {number} minMajorVersion
* @param {function(number): string} urlFormatter
* @param {string=} encoding
* @return {!Promise<string>}
*/
static async fetchVersionUrlWithAutomaticDowngrade(
idealMajorVersion, minMajorVersion, urlFormatter, encoding) {
let majorVersion = idealMajorVersion;
let firstError = null;

while (majorVersion >= minMajorVersion) {
const versionUrl = urlFormatter(majorVersion);
try {
return await InstallerUtils.fetchVersionUrl(versionUrl, encoding);
} catch (error) {
if (error.cause.status != 404) {
// Any unexpected error (other than HTTP 404) is thrown immediately.
throw error;
}

// Save the first error in case we run out this loop. We'll throw this
// one if none of the allowed versions can be found.
if (firstError == null) {
firstError = error;
}

// For 404 errors, decrease the major version, fall through, loop, and
// try again.
majorVersion--;
}
}

// We tried all allowed versions. Throw the initial error, which will have
// details of the first URL we tried.
throw firstError;
}

/**
* Fetch the latest tag from a GitHub repo.
*
Expand Down

0 comments on commit e0312c8

Please sign in to comment.