/
mocha-phantomjs
executable file
·164 lines (145 loc) · 4.59 KB
/
mocha-phantomjs
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
#!/usr/bin/env node
var program = require('commander'),
spawn = require('child_process').spawn,
print = require('util').print,
fs = require('fs'),
path = require('path'),
exists = fs.existsSync || path.existsSync,
cwd = process.cwd(),
which = require('which'),
cookies = [],
headers = {},
settings = {};
function keyValue(val, store) {
val = val.split('=');
if (val[1] === 'true') {
val[1] = true;
} else if (val[1] === 'false') {
val[1] = false;
}
store[val[0]] = val[1];
return val;
}
function cookiesParser(val) {
val = JSON.parse(val);
cookies.push(val);
return val;
}
function header(val) {
return keyValue(val, headers);
}
function setting(val) {
return keyValue(val, settings);
}
function viewport(val) {
val = val.split('x');
return {
width: parseFloat(val[0]),
height: parseFloat(val[1])
};
}
program
.version(JSON.parse(fs.readFileSync(__dirname + '/../package.json', 'utf8')).version)
.usage('[options] page')
.option('-R, --reporter <name>', 'specify the reporter to use', 'spec')
.option('-t, --timeout <timeout>', 'specify the test startup timeout to use', parseInt, 6000)
.option('-A, --agent <userAgent>', 'specify the user agent to use')
.option('-c, --cookies <Object>', 'phantomjs cookie object http://git.io/RmPxgA', cookiesParser) // http://git.io/RmPxgA
.option('-h, --header <name>=<value>', 'specify custom header', header)
.option('-s, --setting <key>=<value>', 'specify specific phantom settings', setting)
.option('-v, --view <width>x<height>', 'specify phantom viewport size', viewport)
.option('-C, --no-color', 'disable color escape codes')
.option('-p, --path <path>', 'path to PhantomJS binary');
program.on('--help', function(){
console.log(' Examples:');
console.log('');
console.log(' $ mocha-phantomjs -R dot /test/file.html');
console.log(' $ mocha-phantomjs http://testserver.com/file.html');
console.log(' $ mocha-phantomjs -p ~/bin/phantomjs /test/file.html');
console.log('');
});
program.parse(process.argv);
if (!program.args.length) { program.outputHelp(); process.exit(1); };
if (program.agent) { settings.userAgent = program.agent; }
var script = fs.realpathSync(__dirname + '/../lib/mocha-phantomjs.coffee');
var reporter = program.reporter;
var page = function(){
var arg = program.args[0];
if (arg.match(/file:\/\//)) { return arg; };
if (arg.match(/http:\/\//)) { return arg; };
if (arg.match(/https:\/\//)) { return arg; };
if (exists(arg)) { return arg; };
if (exists(cwd+'/'+arg)) { return fs.realpathSync(cwd+'/'+arg); };
return arg;
}();
var config = JSON.stringify({
timeout: program.timeout,
cookies: cookies,
headers: headers,
settings: settings,
viewportSize: program.view,
useColors: program.color
});
function startPhantomJS(cmdPath, args, callback) {
var cmd = getCmdFromPath(cmdPath)
if (cmd != null) {
return spawnPhantomJS(cmd, args)
}
getCmdByEnvironmentPath(function (err, cmdPath) {
if(err || !cmdPath) {
getCmdByModulePath(function(err, cmdPath) {
if (err) {
console.error('PhantomJS was not found.');
process.exit(1);
}
spawnPhantomJS(cmdPath, args)
});
} else {
spawnPhantomJS(cmdPath, args);
}
})
}
function spawnPhantomJS(cmdPath, args) {
var phantomjs = spawn(cmdPath, args);
phantomjs.stdout.pipe(process.stdout);
phantomjs.stderr.pipe(process.stdout);
phantomjs.on('exit', function(code){
if (code === 127) { print("Perhaps phantomjs is not installed?\n"); }
process.exit(code);
});
}
function getCmdFromPath(cmdPath, callback) {
if (cmdPath === undefined) { return };
var result = null,
errorMessage = null,
tempPath = path.resolve(cmdPath);
if (exists(tempPath)) {
result = tempPath;
} else {
errorMessage = "PhantomJS does not exist at '" + cmdPath + "'. Looking for PhantomJS in the PATH."
if (!callback) {
console.error(errorMessage);
}
}
if (callback) {
callback(errorMessage, result);
} else {
return result;
}
}
function getCmdByEnvironmentPath(callback) {
which('phantomjs', callback);
}
function getCmdByModulePath(callback) {
for (var i=0; i < module.paths.length; i++) {
var bin = path.join(module.paths[i], '.bin/phantomjs');
if (process.platform === 'win32') {
bin += '.cmd';
}
if (exists(bin)) {
return callback(null, bin);
}
}
return callback('PhantomJS not found.', null)
}
startPhantomJS(program.path, [script, page, reporter, config]);