public
Last active

Casper.js change of file name to show url and saved to root screenshot directory ref: http://net.tutsplus.com/tutorials/javascript-ajax/responsive-screenshots-with-casper/

  • Download Gist
casperscreens.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
var casper = require("casper").create(),
viewportSizes = [
[320,480],
[320,568],
[600,1024],
[1024,768],
[1280,800],
[1440,900]
],
url = casper.cli.args[0],
saveDir = ('');
saveName = url.replace(/[^a-zA-Z0-9]/gi, '-').replace(/^https?-+/, '');
 
casper.start();
 
casper.each(viewportSizes, function(self, viewportSize, i) {
// set two vars for the viewport height and width as we loop through each item in the viewport array
var width = viewportSize[0],
height = viewportSize[1];
//give some time for the page to load
casper.wait(5000, function() {
//set the viewport to the desired height and width
this.viewport(width, height);
casper.thenOpen(url, function() {
this.echo('Opening at ' + width + ' x ' + height);
//Set up two vars, one for the fullpage save, one for the actual viewport save
var FPfilename = saveDir + saveName + '-fullpage-' + width + ".png";
var ACfilename = saveDir + saveName + '-viewport-' + width + '-' + height + ".png";
//Capture selector captures the whole body
this.captureSelector(FPfilename, 'body');
//capture snaps a defined selection of the page
this.capture(ACfilename,{top: 0,left: 0,width: width, height: height});
this.echo('snapshot taken');
});
});
});
 
casper.run(function() {
this.echo('Finished captures for ' + url).exit();
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.