-
-
Notifications
You must be signed in to change notification settings - Fork 221
/
Copy pathcompute-download-urls.js
277 lines (249 loc) · 8.3 KB
/
compute-download-urls.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
module.exports = computeDownloadUrls;
const got = require('got');
const util = require('util');
const { isSelenium4 } = require('./isSelenium4');
const urls = {
selenium: '%s/%s/selenium-server-standalone-%s.jar',
seleniumV4: '%s/%s/selenium-server-%s.jar',
chrome: '%s/%s/chromedriver_%s.zip',
ie: '%s/%s/IEDriverServer_%s_%s.zip',
firefox: '%s/%s/%s-%s-%s',
chromiumedge: '%s/%s/edgedriver_%s.zip',
};
/**
* Computes the URL to download selenium.jar and drivers.
*
* When passing in the `opts.drivers` param, it must be of the following format. Each of the drivers listed
* inside is optional
*
* {
* chrome: { ... },
* ie: { ... },
* firefox: { ... }
* }
*
* Each driver must have the following format.
*
* {
* baseURL: '', // Base URL for where to download driver from
* version: '', // Version number string that matches the desired driver version
* arch: '' // Architecture for the desired driver
* }
*
* @param {string} opts.seleniumVersion - Version number string that matches the desired selenium.jar
* @param {string} opts.seleniumBaseURL - Base URL for where to download selenium.jar from
* @param {Object} opts.drivers - Object containing options for various drivers. See comment for object format.
*/
async function computeDownloadUrls(opts) {
const downloadUrls = {
selenium:
opts.seleniumFullURL ||
util.format(
isSelenium4(opts.seleniumVersion) ? urls.seleniumV4 : urls.selenium,
opts.seleniumBaseURL,
isSelenium4(opts.seleniumVersion)
? `selenium-${getVersionWithZeroedPatchPart(opts.seleniumVersion)}`
: `selenium-${opts.seleniumVersion}`,
opts.seleniumVersion
),
};
if (opts.drivers.chrome) {
await resolveLatestVersion(opts, 'chrome', opts.drivers.chrome.baseURL + '/LATEST_RELEASE');
downloadUrls.chrome =
opts.drivers.chrome.fullURL ||
util.format(
urls.chrome,
opts.drivers.chrome.baseURL,
opts.drivers.chrome.version,
getChromeDriverArchitecture(opts.drivers.chrome.arch, opts.drivers.chrome.version)
);
}
if (opts.drivers.ie) {
downloadUrls.ie =
opts.drivers.ie.fullURL ||
util.format(
urls.ie,
opts.drivers.ie.baseURL,
opts.drivers.ie.version.slice(0, opts.drivers.ie.version.lastIndexOf('.')),
getIeDriverArchitecture(opts.drivers.ie.arch),
opts.drivers.ie.version
);
}
if (opts.drivers.firefox) {
await resolveLatestVersion(opts, 'firefox', 'https://api.github.com/repos/mozilla/geckodriver/releases/latest');
downloadUrls.firefox =
opts.drivers.firefox.fullURL ||
util.format(
urls.firefox,
opts.drivers.firefox.baseURL,
`v${opts.drivers.firefox.version}`,
'geckodriver',
`v${opts.drivers.firefox.version}`,
getFirefoxDriverArchitecture(opts.drivers.firefox.arch)
);
}
if (opts.drivers.edge) {
downloadUrls.edge = opts.drivers.edge.fullURL || getEdgeDriverUrl(opts.drivers.edge.version);
}
if (opts.drivers.chromiumedge) {
await resolveLatestVersion(
opts,
'chromiumedge',
'https://msedgewebdriverstorage.blob.core.windows.net/edgewebdriver/LATEST_STABLE'
);
downloadUrls.chromiumedge =
opts.drivers.chromiumedge.fullURL ||
util.format(
urls.chromiumedge,
opts.drivers.chromiumedge.baseURL,
opts.drivers.chromiumedge.version,
getChromiumEdgeDriverArchitecture(opts.drivers.chromiumedge.arch, opts.drivers.chromiumedge.version)
);
}
return downloadUrls;
}
function getChromeDriverArchitecture(wantedArchitecture, version) {
let platform;
if (process.platform === 'linux') {
platform = 'linux64';
} else if (process.platform === 'darwin') {
if (process.arch === 'arm64') {
const [major] = version.split('.');
platform = parseInt(major, 10) > 105 ? 'mac_arm64' : 'mac64_m1';
} else {
platform = 'mac64';
}
} else {
platform = 'win32';
}
return platform;
}
function getIeDriverArchitecture(wanted) {
let platform;
if (wanted === 'ia32') {
platform = 'Win32';
} else {
platform = 'x64';
}
return platform;
}
function getFirefoxDriverArchitecture(wantedArchitecture) {
const extension = '.tar.gz';
switch (process.platform) {
case 'linux':
return getLinuxFirefoxDriverArchitecture(extension, wantedArchitecture);
case 'darwin':
return getMacFirefoxDriverArchitecture(extension);
case 'win32':
return getWindowsFirefoxDriverArchitecture(wantedArchitecture);
default:
throw new Error('No Firefox driver is available for platform "' + process.platform + '"');
}
}
function getLinuxFirefoxDriverArchitecture(extension, wantedArchitecture = 'x64') {
const arch = wantedArchitecture === 'x64' ? '64' : '32';
return 'linux' + arch + extension;
}
function getMacFirefoxDriverArchitecture(extension) {
return 'macos' + (process.arch === 'arm64' ? '-aarch64' : '') + extension;
}
function getWindowsFirefoxDriverArchitecture(wantedArchitecture = '64') {
const arch = wantedArchitecture.substr(-2) === '64' ? '64' : '32';
return `win${arch}.zip`;
}
const microsoftEdgeReleases = require('./microsoft-edge-releases');
function getEdgeDriverUrl(version) {
const release = microsoftEdgeReleases[version];
if (!release) {
throw new Error(
'Invalid Microsoft Edge Web Driver version see https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/#downloads'
);
}
return release.url;
}
function getChromiumEdgeDriverArchitecture(wantedArchitecture, version) {
let platform;
if (process.platform === 'linux') {
platform = 'linux64';
} else if (process.platform === 'darwin') {
if (process.arch === 'arm64') {
const [major] = version.split('.');
platform = parseInt(major, 10) > 104 ? 'mac64_m1' : 'mac64';
} else {
platform = 'mac64';
}
} else if (wantedArchitecture === 'x32') {
platform = 'win32';
} else {
platform = 'win64';
}
return platform;
}
async function chromiumEdgeBundleAvailable(opts) {
const url = util.format(
urls.chromiumedge,
opts.drivers.chromiumedge.baseURL,
opts.drivers.chromiumedge.version,
getChromiumEdgeDriverArchitecture(opts.drivers.chromiumedge.platform)
);
try {
await got.head(url, { timeout: 10000 });
} catch (_) {
return false;
}
return true;
}
async function resolveLatestVersion(opts, browserDriver, url) {
if (opts.drivers[browserDriver].version === 'latest') {
try {
if (browserDriver === 'firefox') {
if (await getLatestGeckodriver(opts, browserDriver, url)) {
return true;
}
/**
* it seems that linux releases are not for every version
*/
} else if (browserDriver === 'chromiumedge') {
if (await getLatestChromium(opts, browserDriver, url)) {
if (await chromiumEdgeBundleAvailable(opts)) {
return true;
}
}
} else if (await getLatestChromium(opts, browserDriver, url)) {
return true;
}
} catch (_) {
// eslint-disable-next-line no-empty
}
// eslint-disable-next-line no-param-reassign
opts.drivers[browserDriver].version = opts.drivers[browserDriver].fallbackVersion;
}
}
async function getLatestChromium(opts, browserDriver, url) {
const response = await got(url, { timeout: 10000 });
// edgewebdriver latest version file contains invalid characters
const version = response.body.replace(/\r|\n/g, '').replace(/[^\d|.]/g, '');
if (!version) {
return false;
}
// eslint-disable-next-line no-param-reassign
opts.drivers[browserDriver].version = version;
return true;
}
async function getLatestGeckodriver(opts, browserDriver, url) {
const response = await got(url, { timeout: 10000, responseType: 'json' });
if (typeof response.body.name === 'string' && response.body.name) {
// eslint-disable-next-line no-param-reassign
opts.drivers[browserDriver].version = response.body.name;
return true;
}
}
function getVersionWithZeroedPatchPart(fullVersion) {
if (!/^\d+\.\d+\.\d+$/i.test(fullVersion)) {
// If version longer than just 3 numbers, like '4.0.0-beta-1', do nothing
return fullVersion;
}
// else make version patch part zero: '4.1.1' => '4.1.0'
const [major, minor] = fullVersion.split('.');
return `${major}.${minor}.0`;
}