@@ -9,6 +9,7 @@ const path = require('path');
9
9
const del = require ( 'del' ) ;
10
10
const child_process = require ( 'child_process' ) ;
11
11
const os = require ( 'os' ) ;
12
+ const { getChromeVersion } = require ( '@testim/chrome-version' ) ;
12
13
13
14
const skipDownload = process . env . npm_config_chromedriver_skip_download || process . env . CHROMEDRIVER_SKIP_DOWNLOAD ;
14
15
if ( skipDownload === 'true' ) {
@@ -24,6 +25,7 @@ const configuredfilePath = process.env.npm_config_chromedriver_filepath || proce
24
25
cdnUrl = cdnUrl . replace ( / \/ + $ / , '' ) ;
25
26
let platform = process . platform ;
26
27
28
+ const detect_chromedriver_version = process . env . npm_config_detect_chromedriver_version || process . env . DETECT_CHROMEDRIVER_VERSION ;
27
29
let chromedriver_version = process . env . npm_config_chromedriver_version || process . env . CHROMEDRIVER_VERSION || helper . version ;
28
30
if ( platform === 'linux' ) {
29
31
if ( process . arch === 'arm64' || process . arch === 'x64' ) {
@@ -50,13 +52,23 @@ let chromedriverBinaryFilePath;
50
52
let downloadedFile = '' ;
51
53
52
54
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
+ }
53
65
if ( chromedriver_version === 'LATEST' ) {
54
- return getLatestVersion ( getRequestOptions ( `${ cdnUrl } /LATEST_RELEASE` ) ) ;
66
+ return getChromeDriverVersion ( getRequestOptions ( `${ cdnUrl } /LATEST_RELEASE` ) ) ;
55
67
} else {
56
68
const latestReleaseForVersionMatch = chromedriver_version . match ( / L A T E S T _ ( \d + ) / ) ;
57
69
if ( latestReleaseForVersionMatch ) {
58
70
const majorVersion = latestReleaseForVersionMatch [ 1 ] ;
59
- return getLatestVersion ( getRequestOptions ( `${ cdnUrl } /LATEST_RELEASE_${ majorVersion } ` ) ) ;
71
+ return getChromeDriverVersion ( getRequestOptions ( `${ cdnUrl } /LATEST_RELEASE_${ majorVersion } ` ) ) ;
60
72
}
61
73
}
62
74
} )
@@ -79,7 +91,7 @@ Promise.resolve().then(function () {
79
91
} ) ;
80
92
81
93
function downloadFile ( ) {
82
- if ( configuredfilePath ) {
94
+ if ( detect_chromedriver_version !== 'true' && configuredfilePath ) {
83
95
downloadedFile = configuredfilePath ;
84
96
console . log ( 'Using file: ' , downloadedFile ) ;
85
97
return Promise . resolve ( ) ;
@@ -220,7 +232,7 @@ function getRequestOptions(downloadPath) {
220
232
return options ;
221
233
}
222
234
223
- function getLatestVersion ( requestOptions ) {
235
+ function getChromeDriverVersion ( requestOptions ) {
224
236
const deferred = new Deferred ( ) ;
225
237
request ( requestOptions , function ( err , response , data ) {
226
238
if ( err ) {
0 commit comments