-
Notifications
You must be signed in to change notification settings - Fork 304
/
selenium-init.js
178 lines (150 loc) · 5.53 KB
/
selenium-init.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
var util = require('util');
var fs = require('fs');
var path = require('path');
var child_process = require('child_process');
var request = require('request');
var fse = require('fs-extra');
var dmg = require('dmg');
function spawnHelper(command, args) {
return new Promise(function(resolve, reject) {
var child = child_process.spawn(command, args);
child.stdout.on('data', function(data) {
process.stdout.write(data);
});
child.stderr.on('data', function(data) {
process.stderr.write(data);
});
child.on('exit', function(code) {
if (code === 0) {
console.log('Done ' + command + ' with args: ' + args);
resolve();
} else {
console.log('Error running ' + command + ' with args: ' + args);
reject();
}
});
});
return
}
function wget(dir, url) {
return spawnHelper('wget', [ '-P', dir, '-N', url ]);
}
function untar(dir, file) {
return spawnHelper('tar', [ '-x', '-C', dir, '-f', file ]);
}
function unzip(dir, file) {
return spawnHelper('unzip', [ '-o', '-d', dir, file ]);
}
var destDir = 'test_tools';
try {
fs.mkdirSync(destDir);
} catch (e) {
}
if (process.platform !== 'linux' && process.platform !== 'darwin') {
throw new Error('Platform ' + process.platform + ' not supported.');
}
if (process.arch !== 'x86' && process.arch !== 'x64') {
throw new Error('Architecture ' + process.arch + ' not supported.');
}
// Download Firefox Nightly
var firefoxVersionFile = path.join(destDir, 'firefoxVersion');
var firefoxVersion = -Infinity;
if (fs.existsSync(firefoxVersionFile)) {
firefoxVersion = Number(fs.readFileSync(firefoxVersionFile, 'utf8'));
}
var firefoxFileNameFmt = 'firefox-%d.0a1.en-US.%s';
var firefoxBaseURL = 'https://ftp.mozilla.org/pub/mozilla.org/firefox/nightly/latest-mozilla-central/';
var firefoxPlatform;
if (process.platform === 'linux') {
firefoxPlatform = 'linux-';
if (process.arch === 'x86') {
firefoxPlatform += 'i686';
} else if (process.arch === 'x64') {
firefoxPlatform += 'x86_64';
}
firefoxPlatform += '.tar.bz2'
} else if (process.platform === 'darwin') {
firefoxPlatform = 'mac.dmg';
}
request(firefoxBaseURL + 'test_packages.json', function(error, response, body) {
if (error) {
console.error(error);
return;
}
var obj = JSON.parse(body);
var version = Number(obj.mochitest[0].substr(8, 2));
if (version > firefoxVersion) {
fs.writeFileSync(firefoxVersionFile, version, 'utf8');
if (firefoxVersion !== -Infinity) {
var firefoxOldFileName = util.format(firefoxFileNameFmt, firefoxVersion, firefoxPlatform);
fs.unlinkSync(path.join(destDir, firefoxOldFileName));
}
}
var firefoxFileName = util.format(firefoxFileNameFmt, version, firefoxPlatform);
var firefoxURL = firefoxBaseURL + firefoxFileName;
wget(destDir, firefoxURL)
.then(function() {
if (process.platform === 'linux') {
untar(destDir, path.join(destDir, firefoxFileName));
} else if (process.platform === 'darwin') {
dmg.mount(path.join(destDir, firefoxFileName), function(err, extractedPath) {
fse.copySync(path.join(extractedPath, 'FirefoxNightly.app'), path.join(destDir, 'FirefoxNightly.app'));
dmg.unmount(extractedPath, function() {});
});
}
});
});
// Download Chrome Canary
var chromePlatform, chromeZipPlatform;
if (process.platform === 'linux') {
chromeZipPlatform = 'linux';
chromePlatform = 'Linux_';
if (process.arch === 'x86') {
throw new Error('TODO');
} else if (process.arch === 'x64') {
chromePlatform += 'x64';
}
} else if (process.platform === 'darwin') {
chromeZipPlatform = 'mac';
chromePlatform = 'Mac';
}
var chromeVersionFile = path.join(destDir, 'chromeVersion');
var chromeVersion = -Infinity;
if (fs.existsSync(chromeVersionFile)) {
chromeVersion = Number(fs.readFileSync(chromeVersionFile, 'utf8'));
}
wget(destDir, 'https://www.googleapis.com/download/storage/v1/b/chromium-browser-snapshots/o/' + chromePlatform + '%2FLAST_CHANGE?alt=media')
.then(function() {
var newVersion = Number(fs.readFileSync(path.join(destDir, chromePlatform + '%2FLAST_CHANGE?alt=media'), 'utf8'));
fs.renameSync(path.join(destDir, chromePlatform + '%2FLAST_CHANGE?alt=media'), chromeVersionFile);
if (newVersion > chromeVersion) {
fse.removeSync('test_tools/chrome-' + chromeZipPlatform);
wget(destDir, 'https://www.googleapis.com/download/storage/v1/b/chromium-browser-snapshots/o/' + chromePlatform + '%2F' + newVersion + '%2Fchrome-' + chromeZipPlatform + '.zip?alt=media')
.then(function() {
unzip(destDir, 'test_tools/' + chromePlatform + '%2F' + newVersion + '%2Fchrome-' + chromeZipPlatform + '.zip?alt=media')
.then(function() {
fs.unlink('test_tools/' + chromePlatform + '%2F' + newVersion + '%2Fchrome-' + chromeZipPlatform + '.zip?alt=media');
});
});
}
});
// Download ChromeDriver
var chromeDriverPlatform;
if (process.platform === 'linux') {
chromeDriverPlatform = 'linux';
if (process.arch === 'x86') {
chromeDriverPlatform += '32';
} else if (process.arch === 'x64') {
chromeDriverPlatform += '64';
}
} else if (process.platform === 'darwin') {
chromeDriverPlatform = 'mac32';
}
wget(destDir, 'http://chromedriver.storage.googleapis.com/LATEST_RELEASE')
.then(function() {
var version = fs.readFileSync(path.join(destDir, 'LATEST_RELEASE'), 'utf8');
wget(destDir, 'http://chromedriver.storage.googleapis.com/' + version + '/chromedriver_' + chromeDriverPlatform + '.zip')
.then(function() {
unzip(destDir, 'test_tools/chromedriver_' + chromeDriverPlatform + '.zip');
});
});