-
Notifications
You must be signed in to change notification settings - Fork 53
/
index.js
executable file
·401 lines (341 loc) · 12.9 KB
/
index.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
'use strict'
const fs = require('fs')
const path = require('path')
let isWsl = require('is-wsl')
const which = require('which')
const { execSync } = require('child_process')
const { StringDecoder } = require('string_decoder')
const PREFS = [
'user_pref("browser.shell.checkDefaultBrowser", false);',
'user_pref("browser.bookmarks.restore_default_bookmarks", false);',
'user_pref("dom.disable_open_during_load", false);',
'user_pref("dom.max_script_run_time", 0);',
'user_pref("dom.min_background_timeout_value", 10);',
'user_pref("extensions.autoDisableScopes", 0);',
'user_pref("browser.tabs.remote.autostart", false);',
'user_pref("browser.tabs.remote.autostart.2", false);',
'user_pref("extensions.enabledScopes", 15);'
].join('\n')
// NOTE: add 'config.browsers' to get which browsers are started
const $INJECT_LIST = ['baseBrowserDecorator', 'args', 'logger', 'emitter']
// Check if Firefox is installed on the WSL side and use that if it's available
if (isWsl && which.sync('firefox', { nothrow: true })) {
isWsl = false
}
/**
* Takes a string from Windows' tasklist.exe with the following arguments:
* `/FO CSV /NH /SVC` and returns an array of PIDs.
* @param {string} tasklist Expected to be in the form of:
* `'"firefox.exe","14972","Console","1","5.084 K"\r\n"firefox.exe","12204","Console","1","221.656 K"'`
* @returns {string[]} Array of String PIDs. Can be empty.
*/
const extractPids = tasklist => tasklist
.split(',')
.filter(x => /^"\d{3,10}"$/.test(x))
.map(pid => pid.replace(/"/g, ''))
/**
* Curried function version of safeExecSync with reference to logger
* in a closure.
* @param {function} log An instance of logger.create
* @returns {{(command:string):string}} A closure with reference to logger
*/
const createSafeExecSync = log => command => {
let output = ''
try {
output = String(execSync(command))
} catch (err) {
// Something went wrong but we can usually continue.
// For Windows kill.exe, one common error is trying to kill a PID
// that no longer exist, which is fine.
log.debug(String(err))
}
return output
}
// Get all possible Program Files folders even on other drives
// inspect the user's path to find other drives that may contain Program Files folders
const getAllPrefixes = function () {
const drives = []
const paden = process.env.Path.split(';')
const re = /^[A-Z]:\\/i
let pad
for (let p = 0; p < paden.length; p++) {
pad = paden[p]
if (re.test(pad) && drives.indexOf(pad[0]) === -1) {
drives.push(pad[0])
}
}
const result = []
const prefixes = [process.env.PROGRAMFILES, process.env['PROGRAMFILES(X86)']]
let prefix
for (let i = 0; i < prefixes.length; i++) {
if (typeof prefixes[i] !== 'undefined') {
for (let d = 0; d < drives.length; d += 1) {
prefix = drives[d] + prefixes[i].slice(1)
if (result.indexOf(prefix) === -1) {
result.push(prefix)
}
}
}
}
return result
}
// Return location of firefox.exe file for a given Firefox directory
// (available: "Mozilla Firefox", "Aurora", "Nightly").
const getFirefoxExe = function (firefoxDirName) {
if (process.platform !== 'win32' && process.platform !== 'win64') {
return null
}
const firefoxDirNames = Array.prototype.slice.call(arguments)
for (const prefix of getAllPrefixes()) {
for (const dir of firefoxDirNames) {
const candidate = path.join(prefix, dir, 'firefox.exe')
if (fs.existsSync(candidate)) {
return candidate
}
}
}
return path.join('C:\\Program Files', firefoxDirNames[0], 'firefox.exe')
}
const getAllPrefixesWsl = function () {
const drives = []
// Some folks configure their wsl.conf to mount Windows drives without the
// /mnt prefix (e.g. see https://nickjanetakis.com/blog/setting-up-docker-for-windows-and-wsl-to-work-flawlessly)
//
// In fact, they could configure this to be any number of things. So we
// take each path, convert it to a Windows path, check if it looks like
// it starts with a drive and then record that.
const re = /^([A-Z]):\\/i
for (const pathElem of process.env.PATH.split(':')) {
if (fs.existsSync(pathElem)) {
const windowsPath = execSync('wslpath -w "' + pathElem + '"').toString()
const matches = windowsPath.match(re)
if (matches !== null && drives.indexOf(matches[1]) === -1) {
drives.push(matches[1])
}
}
}
const result = []
// We don't have the PROGRAMFILES or PROGRAMFILES(X86) environment variables
// in WSL so we just hard code them.
const prefixes = ['Program Files', 'Program Files (x86)']
for (const prefix of prefixes) {
for (const drive of drives) {
// We only have the drive, and only wslpath knows exactly what they map to
// in Linux, so we convert it back here.
const wslPath =
execSync('wslpath "' + drive + ':\\' + prefix + '"').toString().trim()
result.push(wslPath)
}
}
return result
}
const getFirefoxExeWsl = function (firefoxDirName) {
if (!isWsl) {
return null
}
const firefoxDirNames = Array.prototype.slice.call(arguments)
for (const prefix of getAllPrefixesWsl()) {
for (const dir of firefoxDirNames) {
const candidate = path.join(prefix, dir, 'firefox.exe')
if (fs.existsSync(candidate)) {
return candidate
}
}
}
return path.join('/mnt/c/Program Files/', firefoxDirNames[0], 'firefox.exe')
}
const getFirefoxWithFallbackOnOSX = function () {
if (process.platform !== 'darwin') {
return null
}
const firefoxDirNames = Array.prototype.slice.call(arguments)
const prefix = '/Applications/'
const suffix = '.app/Contents/MacOS/firefox'
let bin
let homeBin
for (let i = 0; i < firefoxDirNames.length; i++) {
bin = prefix + firefoxDirNames[i] + suffix
if ('HOME' in process.env) {
homeBin = path.join(process.env.HOME, bin)
if (fs.existsSync(homeBin)) {
return homeBin
}
}
if (fs.existsSync(bin)) {
return bin
}
}
}
const makeHeadlessVersion = function (Browser) {
const HeadlessBrowser = function () {
Browser.apply(this, arguments)
const execCommand = this._execCommand
this._execCommand = function (command, args) {
// --start-debugger-server ws:6000 can also be used, since remote debugging protocol also speaks WebSockets
// https://hacks.mozilla.org/2017/12/using-headless-mode-in-firefox/
execCommand.call(this, command, args.concat(['-headless', '--start-debugger-server 6000']))
}
}
HeadlessBrowser.prototype = Object.create(Browser.prototype, {
name: { value: Browser.prototype.name + 'Headless' }
})
HeadlessBrowser.$inject = Browser.$inject
return HeadlessBrowser
}
// https://developer.mozilla.org/en-US/docs/Command_Line_Options
const FirefoxBrowser = function (baseBrowserDecorator, args, logger, emitter) {
baseBrowserDecorator(this)
const log = logger.create(this.name + 'Launcher')
const safeExecSync = createSafeExecSync(log)
let browserProcessPid
let browserProcessPidWsl = []
this._getPrefs = function (prefs) {
if (typeof prefs !== 'object') {
return PREFS
}
let result = PREFS
for (const key in prefs) {
result += 'user_pref("' + key + '", ' + JSON.stringify(prefs[key]) + ');\n'
}
return result
}
this._start = function (url) {
const self = this
const command = args.command || this._getCommand()
const profilePath = args.profile || self._tempDir
const flags = args.flags || []
let extensionsDir
if (Array.isArray(args.extensions)) {
extensionsDir = path.resolve(profilePath, 'extensions')
fs.mkdirSync(extensionsDir)
args.extensions.forEach(function (ext) {
const extBuffer = fs.readFileSync(ext)
const copyDestination = path.resolve(extensionsDir, path.basename(ext))
fs.writeFileSync(copyDestination, extBuffer)
})
}
fs.writeFileSync(path.join(profilePath, 'prefs.js'), this._getPrefs(args.prefs))
const translatedProfilePath =
isWsl ? execSync('wslpath -w ' + profilePath).toString().trim() : profilePath
if (isWsl) {
log.warn('WSL environment detected: Please do not open Firefox while running tests as it will be killed after the test!')
log.warn('WSL environment detected: See https://github.com/karma-runner/karma-firefox-launcher/issues/101#issuecomment-891850143')
browserProcessPidWsl = extractPids(safeExecSync('tasklist.exe /FI "IMAGENAME eq firefox.exe" /FO CSV /NH /SVC'))
log.debug('Recorded PIDs not to kill:', browserProcessPidWsl)
}
// If we are using the launcher process, make it print the child process ID
// to stderr so we can capture it. Does not work in WSL.
//
// https://wiki.mozilla.org/Platform/Integration/InjectEject/Launcher_Process/
process.env.MOZ_DEBUG_BROWSER_PAUSE = 0
browserProcessPid = undefined
self._execCommand(
command,
[url, '-profile', translatedProfilePath, '-no-remote', '-wait-for-browser'].concat(flags)
)
self._process.stderr.on('data', errBuff => {
let errString
if (typeof errBuff === 'string') {
errString = errBuff
} else {
const decoder = new StringDecoder('utf8')
errString = decoder.write(errBuff)
}
const matches = errString.match(/BROWSERBROWSERBROWSERBROWSER\s+debug me @ (\d+)/)
if (matches) {
browserProcessPid = parseInt(matches[1], 10)
}
})
}
if (isWsl) {
// exit: will run for each browser when all tests has finished
emitter.on('exit', (done) => {
const tasklist = extractPids(safeExecSync('tasklist.exe /FI "IMAGENAME eq firefox.exe" /FO CSV /NH /SVC'))
.filter(pid => browserProcessPidWsl.indexOf(pid) === -1)
// if this is not the first time 'exit' is called then tasklist is probably empty
if (tasklist.length > 0) {
log.debug('Killing the following PIDs:', tasklist)
const killResult = safeExecSync('taskkill.exe /F ' + tasklist.map(pid => `/PID ${pid}`).join(' ') + ' 2>&1')
log.debug(killResult)
}
return process.nextTick(done)
})
}
this.on('kill', function (done) {
// If we have a separate browser process PID, try killing it.
if (browserProcessPid) {
try {
process.kill(browserProcessPid)
} catch (e) {
// Ignore failure -- the browser process might have already been
// terminated.
}
}
return process.nextTick(done)
})
}
FirefoxBrowser.prototype = {
name: 'Firefox',
DEFAULT_CMD: {
linux: isWsl ? getFirefoxExeWsl('Mozilla Firefox') : 'firefox',
freebsd: 'firefox',
darwin: getFirefoxWithFallbackOnOSX('Firefox'),
win32: getFirefoxExe('Mozilla Firefox')
},
ENV_CMD: 'FIREFOX_BIN'
}
FirefoxBrowser.$inject = $INJECT_LIST
const FirefoxHeadlessBrowser = makeHeadlessVersion(FirefoxBrowser)
const FirefoxDeveloperBrowser = function () {
FirefoxBrowser.apply(this, arguments)
}
FirefoxDeveloperBrowser.prototype = {
name: 'FirefoxDeveloper',
DEFAULT_CMD: {
linux: isWsl ? getFirefoxExeWsl('Firefox Developer Edition') : 'firefox',
darwin: getFirefoxWithFallbackOnOSX('Firefox Developer Edition', 'FirefoxDeveloperEdition', 'FirefoxAurora'),
win32: getFirefoxExe('Firefox Developer Edition')
},
ENV_CMD: 'FIREFOX_DEVELOPER_BIN'
}
FirefoxDeveloperBrowser.$inject = $INJECT_LIST
const FirefoxDeveloperHeadlessBrowser = makeHeadlessVersion(FirefoxDeveloperBrowser)
const FirefoxAuroraBrowser = function () {
FirefoxBrowser.apply(this, arguments)
}
FirefoxAuroraBrowser.prototype = {
name: 'FirefoxAurora',
DEFAULT_CMD: {
linux: isWsl ? getFirefoxExeWsl('Aurora') : 'firefox',
darwin: getFirefoxWithFallbackOnOSX('FirefoxAurora'),
win32: getFirefoxExe('Aurora')
},
ENV_CMD: 'FIREFOX_AURORA_BIN'
}
FirefoxAuroraBrowser.$inject = $INJECT_LIST
const FirefoxAuroraHeadlessBrowser = makeHeadlessVersion(FirefoxAuroraBrowser)
const FirefoxNightlyBrowser = function () {
FirefoxBrowser.apply(this, arguments)
}
FirefoxNightlyBrowser.prototype = {
name: 'FirefoxNightly',
DEFAULT_CMD: {
linux: isWsl ? getFirefoxExeWsl('Nightly', 'Firefox Nightly') : 'firefox',
darwin: getFirefoxWithFallbackOnOSX('FirefoxNightly', 'Firefox Nightly'),
win32: getFirefoxExe('Nightly', 'Firefox Nightly')
},
ENV_CMD: 'FIREFOX_NIGHTLY_BIN'
}
FirefoxNightlyBrowser.$inject = $INJECT_LIST
const FirefoxNightlyHeadlessBrowser = makeHeadlessVersion(FirefoxNightlyBrowser)
// PUBLISH DI MODULE
module.exports = {
'launcher:Firefox': ['type', FirefoxBrowser],
'launcher:FirefoxHeadless': ['type', FirefoxHeadlessBrowser],
'launcher:FirefoxDeveloper': ['type', FirefoxDeveloperBrowser],
'launcher:FirefoxDeveloperHeadless': ['type', FirefoxDeveloperHeadlessBrowser],
'launcher:FirefoxAurora': ['type', FirefoxAuroraBrowser],
'launcher:FirefoxAuroraHeadless': ['type', FirefoxAuroraHeadlessBrowser],
'launcher:FirefoxNightly': ['type', FirefoxNightlyBrowser],
'launcher:FirefoxNightlyHeadless': ['type', FirefoxNightlyHeadlessBrowser]
}