Skip to content

Instantly share code, notes, and snippets.

@scotthorn
Forked from nhoizey/screenshots.js
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save scotthorn/5b92b3d90cd176604f9c to your computer and use it in GitHub Desktop.
Save scotthorn/5b92b3d90cd176604f9c to your computer and use it in GitHub Desktop.
/*
* Takes provided URL passed as argument and make full height screenshots of this page
* with several viewport widths.
*
* These viewport widths are taken from common android and iOS devices. Modify as needed.
*
* Takes an optional second argument for the path where screenshots are saved.
*
* Usage:
* $ casperjs screenshots.js http://example.com
* $ casperjs screenshots.js http://example.com directory/to/save
*/
var casper = require("casper").create();
var screenshotUrl = '',
screenshotNow = new Date(),
screenshotDateTime = screenshotNow.getFullYear() + pad(screenshotNow.getMonth() + 1) + pad(screenshotNow.getDate()) + '-' + pad(screenshotNow.getHours()) + pad(screenshotNow.getMinutes()) + pad(screenshotNow.getSeconds()),
save_directory = screenshotDateTime,
viewport_widths = [240, 320, 360, 568, 603, 640, 768, 800, 960, 1024, 1280, 1400, 1600, 1920];
if (casper.cli.args.length < 1) {
casper
.echo("Usage: $ casperjs screenshots.js http://example.com")
.exit(1)
;
} else {
screenshotUrl = casper.cli.args[0];
}
if (casper.cli.args.length >= 2) {
save_directory = casper.cli.args[1];
}
casper.start(screenshotUrl, function() {
this.echo('Current location is ' + this.getCurrentUrl(), 'info');
});
casper.each(viewport_widths, function(casper, width) {
this.then(function() {
this.viewport(width, 100);
});
this.thenOpen(screenshotUrl, function() {
this.wait(5000);
});
this.then(function(){
this.echo('Screenshot for width:' + width, 'info');
this.capture('screenshots/' + save_directory + '/' + width + '.png');
});
});
casper.run();
function pad(number) {
var r = String(number);
if ( r.length === 1 ) {
r = '0' + r;
}
return r;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment