-
-
Save mallim/5849544 to your computer and use it in GitHub Desktop.
This code has been tested to work with casperjs 1.0.2 and phantomjs 1.9.1. After writing this, then I managed to get mocha-phantomjs (https://github.com/metaskills/mocha-phantomjs) to work and I switch to that. Primarily because mocha-phantomjs allows the usage of mocha's reporter straight without further customization. Additional things feature…
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Check for phantom | |
if ( !phantom.casperLoaded ) { | |
console.log( 'This script must be invoked using the casperjs executable' ); | |
phantom.exit( 1 ); | |
} | |
// get a Casper object. | |
// See http://casperjs.org/ | |
// Instantiate casper | |
var casper = require('casper').create({ | |
exitOnError: false, | |
pageSettings: { | |
loadImages: false, | |
loadPlugins: false | |
}, | |
onLoadError: function (_casper, url) { | |
console.log("[onLoadError]: ", url ); | |
}, | |
onTimeout: function (err) { | |
console.log( "[Timeout]: ", err ); | |
}, | |
logLevel: 'debug', | |
verbose: true | |
}); | |
// Intercept events for 404 and other HTTP errors | |
casper.on('http.status.200', function(resource) { | |
console.log("[HTTP 200]", " <", resource.url, ">" ); | |
}); | |
casper.on('http.status.404', function(resource) { | |
console.log("[HTTP 404]", " <", resource.url, ">" ); | |
}); | |
casper.on('http.status.500', function(resource) { | |
console.log("[HTTP 500]", " <", resource.url, ">" ); | |
}); | |
// | |
// Capture remote log messages | |
// | |
casper.on('remote.message', function(msg) { | |
console.log(">>> ", msg); | |
}); | |
casper.on('page.error', function(msg, trace) { | |
console.log(">>> Remote error: ", msg, trace ); | |
}); | |
// this will be evaluated inside the context of the window. | |
// See http://casperjs.org/api.html#casper.evaluate for notes on | |
// the difference between casper's running environment and the | |
// DOM environment of the loaded page. | |
function testReporter(){ | |
// casper is webkit, so we have good DOM methods. You're | |
// probably unfamiliar with them because of shitty DOMs | |
// in shitty browsers. `test` is the class applied | |
// to passing tests in mocha. If you're using a different | |
// runner, you'll need to figure out how to extract this | |
// this kind of data from the report. | |
var testNodes = document.querySelectorAll('.test'); | |
// testNodes is a NodeList so has no useful array methods, | |
// but we can apply them, I'm returning an object that will | |
// be used later: | |
// { | |
// didPass: true | |
// text: 'The assertion of this test' | |
// } | |
return Array.prototype.map.call(testNodes, function(aTest){ | |
// in mocha, a passed test has a class `pass`. Normally a | |
// match will return an array of matches, which is `!!`ed | |
// into an appropriate boolean. | |
// the assertion of the test is stored in an `h2` | |
return { | |
didPass: !!aTest.getAttribute('class').match(/pass/), | |
text: aTest.querySelector('h2').innerHTML | |
} | |
}) | |
}; | |
// open the casper's browser and load a url. This url should be | |
// where ever your unit tests are being displayed. | |
casper.start('http://localhost:9292/unit.html'); | |
// Might need this code to wait for something to happen | |
// casper.waitForText("passes", function log(){ | |
// console.log( "Yap, got the passes text showing up, continue..." ); | |
// }); | |
// casper.then is a promise. It ensures specific execution order. | |
// give 'javascript promise' a google for some reading. | |
casper.then(function(){ | |
// this.evaluate runs the specified function inside the context | |
// of the page that casper is accessing. Think of it like popping | |
// open the console in Firebug or Webkit Inspector. | |
// the return value of `evaluate` here is array of tests. | |
this.evaluate(testReporter).forEach(function(assertion){ | |
// loop through the test, calling casper's `assert` method | |
// which will print to the console. | |
this.test.assert(assertion.didPass, assertion.text); | |
},this); | |
}); | |
// run all the `then`s and call `test.renderResults` when they're done. | |
casper.run(function(){ | |
if( this.cli.has( "xunit") ) | |
{ | |
// This can help you to render to a file | |
this.test.renderResults( true, 0, this.cli.get('xunit') ); | |
} | |
else | |
{ | |
this.test.renderResults( true ); | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment