Skip to content
This repository has been archived by the owner on Sep 7, 2022. It is now read-only.

Commit

Permalink
feat: Support option for phantom to exit on ResourceError
Browse files Browse the repository at this point in the history
If karma or grunt exit before being able to kill phantomjs, the
phantomjs process will live on as a zombie. This change allows phantomjs
to exit when it hits a resource error (like karma disappearing).

It is enabled by a config option exitOnResourceError.
  • Loading branch information
nmalaguti committed May 29, 2015
1 parent 1983a3c commit 2b90c6b
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 25 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ module.exports = function(config) {
flags: ['--load-images=true'],
debug: true
}
},

phantomjsLauncher: {
// Have phantomjs exit if a ResourceError is encountered (useful if karma exits without killing phantom)
exitOnResourceError: true
}
});
};
Expand Down
24 changes: 19 additions & 5 deletions capture.template.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
var system = require('system'),
fs = require('fs'),
webpage = require('webpage');

(function (phantom) {
var page = webpage.create();
var page = require('webpage').create();

<% if (exitOnResourceError) { %>
page.onResourceError = function() {
phantom.exit(1);
};
<% } %>

<% _.forOwn(pageOptions, function(value, key) { %>
page.<%= key %> = <%= value %>;
<% }); %>

<% _.forOwn(pageSettingsOptions, function(value, key) { %>
page.settings.<%= key %> = <%= value %>;
<% }); %>

<% if (debug) { %>
function debugPage() {
console.log('Launch the debugger page at http://localhost:9000/webkit/inspector/inspector.html?page=2');

Expand All @@ -19,4 +30,7 @@ webpage = require('webpage');
setTimeout(launchPage, 15000);
}
debugPage();
<% } else { %>
page.open('<%= url %>');
<% } %>
}(phantom));
43 changes: 23 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ function serializeOption(value) {
}

var phantomJSExePath = function () {
// If the path we're given by phantomjs is to a .cmd, it is pointing to a global copy.
// Using the cmd as the process to execute causes problems cleaning up the processes
// If the path we're given by phantomjs is to a .cmd, it is pointing to a global copy.
// Using the cmd as the process to execute causes problems cleaning up the processes
// so we walk from the cmd to the phantomjs.exe and use that instead.

 var phantomSource = require('phantomjs').path;
Expand All @@ -34,32 +34,35 @@ var PhantomJSBrowser = function(baseBrowserDecorator, config, args, logger) {
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]) + ';';
var pageOptions = {};
var pageSettingsOptions = {};

_.forOwn(options, function(optionsValue, optionsKey) {
if (optionsKey !== 'settings') { // settings cannot be overriden, it should be extended!
pageOptions[optionsKey] = serializeOption(optionsValue);
} else {
// key === settings
_.forOwn(optionsValue, function(settingsValue, settingsKey) {
pageSettingsOptions[settingsKey] = serializeOption(settingsValue);
});
}
});

if (options.settings) {
optionsCode = optionsCode.concat(Object.keys(options.settings).map(function (key) {
return 'page.settings.' + key + ' = ' + serializeOption(options.settings[key]) + ';';
}));
}

var captureCode;
if (args.debug) {
flags = flags.concat('--remote-debugger-port=9000');
flags = flags.concat('--remote-debugger-autorun=yes');
}

var file = fs.readFileSync(path.join(__dirname, 'capture.template.js'));

var compiled = _.template(file.toString());
captureCode = compiled({url: url});
var file = fs.readFileSync(path.join(__dirname, 'capture.template.js'));

} else {
captureCode = 'var page = require("webpage").create();\n' +
optionsCode.join('\n') + '\npage.open("' + url + '");\n';
}
var compiled = _.template(file.toString());
var captureCode = compiled({
debug: args.debug,
exitOnResourceError: config && config.exitOnResourceError,
pageOptions: pageOptions,
pageSettingsOptions: pageSettingsOptions,
url: url
});

fs.writeFileSync(captureFile, captureCode);

Expand Down

0 comments on commit 2b90c6b

Please sign in to comment.