Skip to content

Commit 67253e1

Browse files
ksathyanmgiggio
authored andcommittedJan 28, 2020
Detect chromedriver version that corresponds to the version of Chrome installed
1 parent b1f0cab commit 67253e1

File tree

4 files changed

+47
-4
lines changed

4 files changed

+47
-4
lines changed
 

‎README.md

+25
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,31 @@ You can also force a different version of chromedriver by replacing `LATEST` wit
241241
CHROMEDRIVER_VERSION=75.0.3770.140 npm install chromedriver
242242
```
243243

244+
## Detect ChromeDriver Version
245+
246+
The NPM package version may not be always compatible to your Chrome version.
247+
To get the chromedriver that corresponds to the version of Chrome installed,
248+
you can use the npm config property `detect_chromedriver_version`.
249+
250+
```shell
251+
npm install chromedriver --detect_chromedriver_version
252+
```
253+
254+
Or add property into your [`.npmrc`](https://docs.npmjs.com/files/npmrc) file.
255+
256+
```
257+
detect_chromedriver_version=true
258+
```
259+
260+
Another option is to use environment variable `DETECT_CHROMEDRIVER_VERSION`.
261+
262+
```shell
263+
DETECT_CHROMEDRIVER_VERSION=true npm install chromedriver
264+
```
265+
266+
**Note:** When the property `detect_chromedriver_version` is provided,
267+
`chromedriver_version` and `chromedriver_filepath` properties are ignored.
268+
244269
## A Note on chromedriver
245270

246271
Chromedriver is not a library for NodeJS.

‎install.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const path = require('path');
99
const del = require('del');
1010
const child_process = require('child_process');
1111
const os = require('os');
12+
const { getChromeVersion } = require('@testim/chrome-version');
1213

1314
const skipDownload = process.env.npm_config_chromedriver_skip_download || process.env.CHROMEDRIVER_SKIP_DOWNLOAD;
1415
if (skipDownload === 'true') {
@@ -24,6 +25,7 @@ const configuredfilePath = process.env.npm_config_chromedriver_filepath || proce
2425
cdnUrl = cdnUrl.replace(/\/+$/, '');
2526
let platform = process.platform;
2627

28+
const detect_chromedriver_version = process.env.npm_config_detect_chromedriver_version || process.env.DETECT_CHROMEDRIVER_VERSION;
2729
let chromedriver_version = process.env.npm_config_chromedriver_version || process.env.CHROMEDRIVER_VERSION || helper.version;
2830
if (platform === 'linux') {
2931
if (process.arch === 'arm64' || process.arch === 'x64') {
@@ -50,13 +52,23 @@ let chromedriverBinaryFilePath;
5052
let downloadedFile = '';
5153

5254
Promise.resolve().then(function () {
55+
if (detect_chromedriver_version === 'true') {
56+
// Refer http://chromedriver.chromium.org/downloads/version-selection
57+
return getChromeVersion().then(function (chromeVersion) {
58+
console.log("Your Chrome version is " + chromeVersion);
59+
const chromeVersionWithoutPatch = /^(.*?)\.\d+$/.exec(chromeVersion)[1];
60+
return getChromeDriverVersion(getRequestOptions(cdnUrl + '/LATEST_RELEASE_' + chromeVersionWithoutPatch));
61+
}).then(function () {
62+
console.log("Compatible ChromeDriver version is " + chromedriver_version);
63+
});
64+
}
5365
if (chromedriver_version === 'LATEST') {
54-
return getLatestVersion(getRequestOptions(`${cdnUrl}/LATEST_RELEASE`));
66+
return getChromeDriverVersion(getRequestOptions(`${cdnUrl}/LATEST_RELEASE`));
5567
} else {
5668
const latestReleaseForVersionMatch = chromedriver_version.match(/LATEST_(\d+)/);
5769
if (latestReleaseForVersionMatch) {
5870
const majorVersion = latestReleaseForVersionMatch[1];
59-
return getLatestVersion(getRequestOptions(`${cdnUrl}/LATEST_RELEASE_${majorVersion}`));
71+
return getChromeDriverVersion(getRequestOptions(`${cdnUrl}/LATEST_RELEASE_${majorVersion}`));
6072
}
6173
}
6274
})
@@ -79,7 +91,7 @@ Promise.resolve().then(function () {
7991
});
8092

8193
function downloadFile() {
82-
if (configuredfilePath) {
94+
if (detect_chromedriver_version !== 'true' && configuredfilePath) {
8395
downloadedFile = configuredfilePath;
8496
console.log('Using file: ', downloadedFile);
8597
return Promise.resolve();
@@ -220,7 +232,7 @@ function getRequestOptions(downloadPath) {
220232
return options;
221233
}
222234

223-
function getLatestVersion(requestOptions) {
235+
function getChromeDriverVersion(requestOptions) {
224236
const deferred = new Deferred();
225237
request(requestOptions, function (err, response, data) {
226238
if (err) {

‎package-lock.json

+5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"update-chromedriver": "node update.js"
2727
},
2828
"dependencies": {
29+
"@testim/chrome-version": "^1.0.3",
2930
"del": "^4.1.1",
3031
"extract-zip": "^1.6.7",
3132
"mkdirp": "^0.5.1",

0 commit comments

Comments
 (0)
Please sign in to comment.