Skip to content

Commit

Permalink
PhantomJS plugin: Added optional timeout.
Browse files Browse the repository at this point in the history
Closes #415.
  • Loading branch information
milimetric authored and JamesMGreene committed Mar 19, 2013
1 parent b4c5235 commit a34d1f7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion addons/phantomjs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ A PhantomJS-powered headless test runner, providing basic console output for QUn

### Usage ###
```bash
phantomjs runner.js [url-of-your-qunit-testsuite]
phantomjs runner.js [url-of-your-qunit-testsuite] [timeout-in-seconds]
```

### Example ###
Expand Down
15 changes: 12 additions & 3 deletions addons/phantomjs/runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@
(function() {
'use strict';

var url, page,
var url, page, timeout,
args = require('system').args;

// arg[0]: scriptName, args[1...]: arguments
if (args.length !== 2) {
console.error('Usage:\n phantomjs runner.js [url-of-your-qunit-testsuite]');
if (args.length < 2 || args.length > 3) {
console.error('Usage:\n phantomjs runner.js [url-of-your-qunit-testsuite] [timeout-in-seconds]');
phantom.exit(1);
}

url = args[1];
page = require('webpage').create();
timeout = args[2] != null ? parseInt(args[2], 10) : null;

// Route `console.log()` calls from within the Page context to the main Phantom context (i.e. current `this`)
page.onConsoleMessage = function(msg) {
Expand Down Expand Up @@ -65,6 +66,14 @@
phantom.exit(1);
}

// Set a timeout on the test running, otherwise tests with async problems will hang forever
if (typeof timeout === 'number') {
setTimeout(function() {
console.error('The specified timeout of ' + timeout + ' seconds has expired. Aborting...');
phantom.exit(1);
}, timeout * 1000);
}

// Do nothing... the callback mechanism will handle everything!
}
});
Expand Down

0 comments on commit a34d1f7

Please sign in to comment.