Skip to content

Commit

Permalink
feat: add get latest known good version (#438)
Browse files Browse the repository at this point in the history
* feat: add get latest known good version

* remove _

* rename

* revert const of LATEST
  • Loading branch information
KazuCocoa authored Oct 22, 2024
1 parent a825709 commit f9b64d8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 34 deletions.
27 changes: 26 additions & 1 deletion lib/storage-client/storage-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
CPU,
} from '../constants';
import {parseGoogleapiStorageXml} from './googleapis';
import {parseKnownGoodVersionsWithDownloadsJson} from './chromelabs';
import {parseKnownGoodVersionsWithDownloadsJson, parseLatestKnownGoodVersionsJson} from './chromelabs';
import {compareVersions} from 'compare-versions';
import semver from 'semver';

Expand All @@ -32,6 +32,8 @@ const STORAGE_INFOS = /** @type {readonly StorageInfo[]} */ ([{
accept: 'application/json',
}]);

const CHROME_FOR_TESTING_LAST_GOOD_VERSIONS = `${CHROMELABS_URL}/chrome-for-testing/last-known-good-versions.json`;

const log = logger.getLogger('ChromedriverStorageClient');

/**
Expand Down Expand Up @@ -407,6 +409,29 @@ export class ChromedriverStorageClient {
}
return synchronizedDrivers;
}

/**
* Return latest chromedriver version for Chrome for Testing.
* @returns {Promise<string>}
*/
async getLatestKnownGoodVersion () {
let jsonStr;
try {
jsonStr = await retrieveData(
CHROME_FOR_TESTING_LAST_GOOD_VERSIONS,
{
'user-agent': USER_AGENT,
accept: `application/json, */*`,
}, {timeout: STORAGE_REQ_TIMEOUT_MS}
);
} catch (e) {
const err = /** @type {Error} */ (e);
throw new Error(`Cannot fetch the latest Chromedriver version. ` +
`Make sure you can access ${CHROME_FOR_TESTING_LAST_GOOD_VERSIONS} from your machine or provide a mirror by setting ` +
`a custom value to CHROMELABS_URL enironment variable. Original error: ${err.message}`);
}
return parseLatestKnownGoodVersionsJson(jsonStr);
}
}

export default ChromedriverStorageClient;
Expand Down
38 changes: 5 additions & 33 deletions test/helpers/install.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,11 @@
import _ from 'lodash';
import { fs, mkdirp } from '@appium/support';
import ChromedriverStorageClient from '../../lib/storage-client/storage-client';
import {parseLatestKnownGoodVersionsJson} from '../../lib/storage-client/chromelabs';
import {
CD_VER, retrieveData, getOsInfo, getChromedriverDir,
CD_VER, getOsInfo, getChromedriverDir,
} from '../../lib/utils';
import { USER_AGENT, STORAGE_REQ_TIMEOUT_MS, CHROMELABS_URL } from '../../lib/constants';

const LATEST_VERSION = 'LATEST';

/**
*
* @param {string} ver
* @returns {Promise<string>}
*/
async function formatCdVersion (ver) {
if (_.toUpper(ver) !== LATEST_VERSION) {
return ver;
}

let jsonStr;
const url = `${CHROMELABS_URL}/chrome-for-testing/last-known-good-versions.json`;
try {
jsonStr = await retrieveData(
url, {
'user-agent': USER_AGENT,
accept: `application/json, */*`,
}, {timeout: STORAGE_REQ_TIMEOUT_MS}
);
} catch (e) {
const err = /** @type {Error} */ (e);
throw new Error(`Cannot fetch the latest Chromedriver version. ` +
`Make sure you can access ${url} from your machine or provide a mirror by setting ` +
`a custom value to CHROMELABS_URL enironment variable. Original error: ${err.message}`);
}
return parseLatestKnownGoodVersionsJson(jsonStr);
}

/**
*
* @param {string} platformName
Expand All @@ -54,8 +23,11 @@ export async function install () {
const client = new ChromedriverStorageClient({
chromedriverDir: await prepareChromedriverDir(osInfo.name),
});
const chromeDriverVersion = CD_VER === LATEST_VERSION
? await client.getLatestKnownGoodVersion()
: CD_VER;
await client.syncDrivers({
osInfo,
versions: [await formatCdVersion(CD_VER)],
versions: [chromeDriverVersion],
});
}

0 comments on commit f9b64d8

Please sign in to comment.