forked from karma-runner/karma-phantomjs-launcher
-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
56 lines (41 loc) · 1.57 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
var fs = require('fs');
function serializeOption(value) {
if (typeof value === 'function') {
return value.toString();
}
return JSON.stringify(value);
}
var PhantomJSBrowser = function(baseBrowserDecorator, config, args) {
baseBrowserDecorator(this);
this.DEFAULT_CMD = config.cmd;
var options = args && args.options || config && config.options || {};
var flags = args && args.flags || config && config.flags || [];
this._start = function(url) {
// create the js file that will open karma
var captureFile = this._tempDir + '/capture.js';
var optionsCode = Object.keys(options).map(function (key) {
if (key !== 'settings') { // settings cannot be overriden, it should be extended!
return 'page.' + key + ' = ' + serializeOption(options[key]) + ';';
}
});
if (options.settings) {
optionsCode = optionsCode.concat(Object.keys(options.settings).map(function (key) {
return 'page.settings.' + key + ' = ' + serializeOption(options.settings[key]) + ';';
}));
}
var captureCode = 'var page = require("webpage").create();\n' +
optionsCode.join('\n') + '\npage.open("' + url + '");\n';
fs.writeFileSync(captureFile, captureCode);
// and start phantomjs
this._execCommand(this._getCommand(), flags.concat(captureFile));
};
};
PhantomJSBrowser.prototype = {
name: 'PhantomJS',
ENV_CMD: 'PHANTOMJS_BIN'
};
PhantomJSBrowser.$inject = ['baseBrowserDecorator', 'config.phantomjsLauncher', 'args'];
// PUBLISH DI MODULE
module.exports = {
'launcher:PhantomJS': ['type', PhantomJSBrowser]
};