I'm currently digging into CasperJS and really enjoying it. However, maybe it's something I missed in the documentation, I seem to be having trouble with casper.capture(). I've currently rigged mine up to capture whenever a test fails, and put it in a separate setup module as below
function captureFailure(filename){
casper.test.on("fail", function(failure){
casper.viewport(1280, 1024);
casper.capture("failedScreenshots/Failure-"+filename+".jpg", {
top: 0,
left: 0,
width: 1280,
height: 1024
});
});
}
exports.captureFailure = captureFailure;
This is then put into my tests like so:
. . .
// setup
setup.login();
// test
casper.test.begin("Complete new social campaign flow with image as a signed in user.", 16, function suite(test) {
// setup captureFailure
setup.captureFailure("SocialFlowImage");
casper.start(data.baseURL+'/campaigns/', function(){
console.log("Campaign page loaded");
this.click(campaignCreate);
casper.waitForSelector(socialCampaignCreateModal, function(){
test.assertExists(socialCampaignCreateModal, 'Modal pops up');
test.assertTextExists('Deal', 'Deal button exists');
test.assertTextExists('Marketing Email', 'Marketing Email button exists');
test.assertTextExists('Facebook', 'Facebook button exists');
});
});
. . .
For the most part this was working on its own, but when I ran all of my tests in tandem to test multiple failures, screenshots were overwritten as they went along. In chronological order it looked something like this:
test 1 -> test 1 failure -> Capture screenshot 1 -> test 2 -> test 2 failure -> Capture screenshot 2 and subsequently overwrite screenshot 1
And this resulted in 2 of the same screenshots but of different naming convention.
Any ideas?
Figured out that I had overlapping event handlers on the "fail" event. To remedy this, changed my casperjs test style to include the test object with setUp and tearDown functions, removed the event handles by doing
casper.test.removeListener("fail", casper.test.listeners("fail")[0]);
Which is a bit hacky, but my custom event handler has a filename
arg passed in for unique screenshot names and easier times debugging/identifying which test ran what. As a result I have to jimmy rig it since my setUp actually has an anonymous function that attaches it.