Skip to content

Instantly share code, notes, and snippets.

@Gerg
Forked from sheelc/README.md
Last active August 29, 2015 13:56
Show Gist options
  • Save Gerg/8926514 to your computer and use it in GitHub Desktop.
Save Gerg/8926514 to your computer and use it in GitHub Desktop.

Jasmine 2 Phantom Runner

Use to run your Jasmine 2 specs using phantom.js. Useful for running your tests in your console or CI.

Setup:

  1. Download phantom_runner.js and put it in your project
  2. Include the phantom_callback.html snippet in your SpecRunner.html after other Jasmine files are included

To use:

  • Call phantomjs phantom_runner.js #{path to SpecRunner.html}
  • If no color is desired in the output, then add the --no-color flag either before or after the location of the SpecRunner.html, e.g. phantomjs phantom_runner.js #{path to SpecRunner.html} --no-color
<script type="text/javascript" src="lib/jasmine-2.0.0/console.js"></script>
<script type="text/javascript">
if (typeof window.callPhantom === "function") {
window.callPhantom("parse time");
}
</script>
var system = require("system"),
fs = require("fs"),
page = require("webpage").create(),
showColor = true,
url = system.args[1];
if (system.args[1] == "--no-color" || system.args[2] == "--no-color") {
showColor = false;
if (system.args[1] == "--no-color") {
url = system.args[2];
}
}
page.onConsoleMessage = function(msg) {
fs.write("/dev/stdout", msg, "w");
};
page.onError = function() {
console.log.apply(console, arguments);
};
page.onAlert = function() {
console.log.apply(console, arguments);
};
if (!url) {
console.log("argument is required: location of jasmine tests");
phantom.exit(1);
}
var dateStarted = Date.now();
page.onCallback = function(message) {
if (message === "parse time") {
page.evaluate(function(showColor) {
jasmineRequire.console(jasmineRequire, jasmine);
var consoleReporter = jasmine.ConsoleReporter({
print: function() { console.log.apply(console, arguments); },
showColors: showColor,
timer: new jasmine.Timer()
});
jasmine.getEnv().addReporter(consoleReporter);
jasmine.getEnv().addReporter({
jasmineDone: function() { window.callPhantom('jasmine done'); },
specDone: function(result) { window.specsFailed = window.specsFailed || (result.status === "failed") }
});
}, showColor);
} else if (message === "jasmine done") {
var timeElapsed = Date.now() - dateStarted,
minimumExecutionTime = 1500;
var exitCode = page.evaluate(function() {
return window.specsFailed ? 1 : 0;
});
var exitFn = function() { phantom.exit(exitCode); };
if (timeElapsed < minimumExecutionTime) {
setTimeout(exitFn, minimumExecutionTime - timeElapsed);
} else {
exitFn();
}
}
};
page.open(url, function(status) {
if (status !== "success") {
console.log("could not successfully open up page at: " + url);
phantom.exit(1);
}
});
@sheelc
Copy link

sheelc commented Feb 16, 2014

Cool, "merged" the updates. Thanks for the cleanup!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment