-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathchromedriver.ts
282 lines (264 loc) · 8.73 KB
/
chromedriver.ts
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
278
279
280
281
282
import * as fs from 'fs';
import * as os from 'os';
import * as path from 'path';
import {OUT_DIR, ProviderClass, ProviderConfig, ProviderInterface} from './provider';
import {convertXmlToVersionList, updateXml} from './utils/cloud_storage_xml';
import {changeFilePermissions, generateConfigFile, getBinaryPathFromConfig, removeFiles, renameFileWithVersion, unzipFile, zipFileList,} from './utils/file_utils';
import {requestBinary} from './utils/http_utils';
import {getVersion} from './utils/version_list';
export class ChromeDriver extends ProviderClass implements ProviderInterface {
cacheFileName = 'chromedriver.xml';
configFileName = 'chromedriver.config.json';
ignoreSSL = false;
osType = os.type();
osArch = os.arch();
outDir = OUT_DIR;
proxy: string = null;
requestUrl = 'https://chromedriver.storage.googleapis.com/';
seleniumFlag = '-Dwebdriver.chrome.driver';
version: string = null;
maxVersion: string = null;
constructor(config?: ProviderConfig) {
super();
this.cacheFileName = this.setVar('cacheFileName', this.cacheFileName, config);
this.configFileName = this.setVar('configFileName', this.configFileName, config);
this.ignoreSSL = this.setVar('ignoreSSL', this.ignoreSSL, config);
this.osArch = this.setVar('osArch', this.osArch, config);
this.osType = this.setVar('osType', this.osType, config);
this.outDir = this.setVar('outDir', this.outDir, config);
this.proxy = this.setVar('proxy', this.proxy, config);
this.requestUrl = this.setVar('requestUrl', this.requestUrl, config);
this.version = this.setVar('version', this.version, config);
this.maxVersion = this.setVar('maxVersion', this.maxVersion, config);
}
/**
* Should update the cache and download, find the version to download,
* then download that binary.
* @param version Optional to provide the version number or latest.
* @param maxVersion Optional to provide the max version.
*/
async updateBinary(version?: string, maxVersion?: string): Promise<void> {
if (!version) {
version = this.version;
}
if (!maxVersion) {
maxVersion = this.maxVersion;
}
await updateXml(this.requestUrl, {
fileName: path.resolve(this.outDir, this.cacheFileName),
ignoreSSL: this.ignoreSSL,
proxy: this.proxy
});
const versionList = convertXmlToVersionList(
path.resolve(this.outDir, this.cacheFileName), '.zip', versionParser,
semanticVersionParser);
const versionObj = getVersion(
versionList, osHelper(this.osType, this.osArch),
formatVersion(version), maxVersion);
const chromeDriverUrl = this.requestUrl + versionObj.url;
const chromeDriverZip = path.resolve(this.outDir, versionObj.name);
// We should check the zip file size if it exists. The size will
// be used to either make the request, or quit the request if the file
// size matches.
let fileSize = 0;
try {
fileSize = fs.statSync(chromeDriverZip).size;
} catch (err) {
}
await requestBinary(chromeDriverUrl, {
fileName: chromeDriverZip,
fileSize,
ignoreSSL: this.ignoreSSL,
proxy: this.proxy
});
// Unzip and rename all the files (a grand total of 1) and set the
// permissions.
const fileList = zipFileList(chromeDriverZip);
const fileItem = path.resolve(this.outDir, fileList[0]);
unzipFile(chromeDriverZip, this.outDir);
const renamedFileName =
renameFileWithVersion(fileItem, '_' + versionObj.version);
changeFilePermissions(renamedFileName, '0755', this.osType);
generateConfigFile(
this.outDir, path.resolve(this.outDir, this.configFileName),
matchBinaries(this.osType), renamedFileName);
return Promise.resolve();
}
/**
* Gets the binary file path.
* @param version Optional to provide the version number or latest.
*/
getBinaryPath(version?: string): string|null {
try {
const configFilePath = path.resolve(this.outDir, this.configFileName);
return getBinaryPathFromConfig(configFilePath, version);
} catch (_) {
return null;
}
}
/**
* Gets a comma delimited list of versions downloaded. Also has the "latest"
* downloaded noted.
*/
getStatus(): string|null {
try {
const configFilePath = path.resolve(this.outDir, this.configFileName);
const configJson = JSON.parse(fs.readFileSync(configFilePath).toString());
const versions: string[] = [];
for (const binaryPath of configJson['all']) {
let version = '';
let regex = /.*chromedriver_(\d+.\d+.*)/g;
if (this.osType === 'Windows_NT') {
regex = /.*chromedriver_(\d+.\d+.*).exe/g;
}
try {
const exec = regex.exec(binaryPath);
if (exec && exec[1]) {
version = exec[1];
}
} catch (_) {
}
if (configJson['last'] === binaryPath) {
version += ' (latest)';
}
versions.push(version);
}
return versions.join(', ');
} catch (_) {
return null;
}
}
/**
* Get a line delimited list of files removed.
*/
cleanFiles(): string {
return removeFiles(this.outDir, [/chromedriver.*/g]);
}
}
/**
* Helps translate the os type and arch to the download name associated
* with composing the download link.
* @param ostype The operating stystem type.
* @param osarch The chip architecture.
* @returns The download name associated with composing the download link.
*/
export function osHelper(ostype: string, osarch: string): string {
if (ostype === 'Darwin') {
return 'mac';
} else if (ostype === 'Windows_NT') {
if (osarch === 'x64') {
return 'win32';
} else if (osarch === 'x32') {
return 'win32';
}
} else if (ostype === 'Linux') {
if (osarch === 'x64') {
return 'linux64';
} else if (osarch === 'x32') {
return null;
}
}
return null;
}
/**
* Captures the version name which includes the semantic version and extra
* metadata. So an example for 12.34/chromedriver_linux64.zip,
* the version is 12.34.
*
* The new version is 70.0.3538.16/chromedriver_linux64.zip. This will return
* 70.0.3538.16.
* @param xmlKey The xml key including the partial url.
*/
export function versionParser(xmlKey: string) {
const newRegex = /([0-9]*\.[0-9]*\.[0-9]*\.[0-9]*)\/chromedriver_.*\.zip/g;
try {
const exec = newRegex.exec(xmlKey);
if (exec) {
return exec[1];
}
} catch (_) {
}
const oldRegex = /([0-9]*\.[0-9]*)\/chromedriver_.*\.zip/g;
try {
const exec = oldRegex.exec(xmlKey);
if (exec) {
return exec[1];
}
} catch (_) {
}
return null;
}
/**
* Captures the version name which includes the semantic version and extra
* metadata. So an example for 12.34/chromedriver_linux64.zip,
* the version is 12.34.0.
*
* The new version is 70.0.3538.16/chromedriver_linux64.zip. This will return
* 70.0.3538.
* @param xmlKey The xml key including the partial url.
*/
export function semanticVersionParser(xmlKey: string): string|null {
const newRegex = /([0-9]*\.[0-9]*\.[0-9]*).[0-9]*\/chromedriver_.*\.zip/g;
try {
const exec = newRegex.exec(xmlKey);
if (exec) {
return exec[1];
}
} catch (_) {
}
const oldRegex = /([0-9]*\.[0-9]*)\/chromedriver_.*\.zip/g;
try {
const exec = oldRegex.exec(xmlKey);
if (exec) {
return exec[1] + '.0';
}
} catch (_) {
}
return null;
}
/**
* Matches the installed binaries depending on the operating system.
* @param ostype The operating stystem type.
*/
export function matchBinaries(ostype: string): RegExp|null {
if (ostype === 'Darwin' || ostype === 'Linux') {
return /chromedriver_\d+.\d+.*/g;
} else if (ostype === 'Windows_NT') {
return /chromedriver_\d+.\d+.*.exe/g;
}
return null;
}
/**
* Specifically to chromedriver, when downloading a version, the version
* you give webdriver-manager is not in the same as the output from the
* semanticVersionParser. So getting the version to the versionList will not
* be just a dictionary look up versionList[version]. The version will have
* to be formatted.
*
* Example:
* 2.44 will return with the formatted semantic version 2.44.0
* 70.0.1.1 will return with the formatted semantic version 70.0.1
*
* @param version The actual version.
*/
export function formatVersion(version: string): string|null {
const newRegex = /([0-9]*\.[0-9]*\.[0-9]*)(\.[0-9]*)?/g;
try {
const exec = newRegex.exec(version);
if (exec) {
return exec[1];
}
} catch (_) {
// no-op: if exec[1] is not reachable, move to the next regex.
}
const oldRegex = /([0-9]*\.[0-9]*)/g;
try {
const exec = oldRegex.exec(version);
if (exec) {
return exec[1] + '.0';
}
} catch (_) {
// no-op: if exec[1] is not reachable, move on to return null.
}
return null;
}