1 var fs = require('fs');
2 var path = require('path');
4 function serializeOption(value) {
5 if (typeof value === 'function') {
6 return value.toString();
8 return JSON.stringify(value);
11 var phantomJSExePath = function () {
12 // If the path we're given by phantomjs is to a .cmd, it is pointing to a global copy.
13 // Using the cmd as the process to execute causes problems cleaning up the processes
14 // so we walk from the cmd to the phantomjs.exe and use that instead.
16 var phantomSource = require('phantomjs').path;
18 if (path.extname(phantomSource).toLowerCase() === '.cmd') {
19 return path.join(path.dirname( phantomSource ), '//node_modules//phantomjs//lib//phantom//phantomjs.exe');
25 var PhantomJSBrowser = function(baseBrowserDecorator, config, args) {
26 baseBrowserDecorator(this);
28 var options = args && args.options || config && config.options || {};
29 var flags = args && args.flags || config && config.flags || [];
31 this._start = function(url) {
32 // create the js file that will open karma
33 var captureFile = this._tempDir + '/capture.js';
34 var optionsCode = Object.keys(options).map(function (key) {
35 if (key !== 'settings') { // settings cannot be overriden, it should be extended!
36 return 'page.' + key + ' = ' + serializeOption(options[key]) + ';';
40 if (options.settings) {
41 optionsCode = optionsCode.concat(Object.keys(options.settings).map(function (key) {
42 return 'page.settings.' + key + ' = ' + serializeOption(options.settings[key]) + ';';
46 var captureCode = 'var page = require("webpage").create();\n' +
47 optionsCode.join('\n') + '\npage.open("' + url + '");\n';
48 fs.writeFileSync(captureFile, captureCode);
50 flags = flags.concat(captureFile);
52 // and start phantomjs
53 this._execCommand(this._getCommand(), flags);
57 PhantomJSBrowser.prototype = {
61 linux: require('phantomjs').path,
62 darwin: require('phantomjs').path,
63 win32: phantomJSExePath()
65 ENV_CMD: 'PHANTOMJS_BIN'
68 PhantomJSBrowser.$inject = ['baseBrowserDecorator', 'config.phantomjsLauncher', 'args'];
73 'launcher:PhantomJS': ['type', PhantomJSBrowser]