-
-
Save NicholasModesto/9204d6eb8e808891e0a0 to your computer and use it in GitHub Desktop.
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
/*! | |
* | |
* Automatic Responsive screenshots creation with PhantomJS and CasperJS. | |
* Adapted from the Responsive Design Workflow book by Stephen Hay (http://responsivedesignworkflow.com/). | |
* | |
* Usage instructions: | |
- Install PhantomJS (http://phantomjs.org/) and CasperJS (http://casperjs.org/) | |
- Save this script as `screenshots-multipages.js` in a folder somewhere in your filesystem | |
- In the same folder, create a subfolder called `screenshots` (defined in `screenshotFolder` variable) | |
- Define the URLs you want to process in `baseUrl` (string) and `links` (array) variables | |
- Define your breakpoints in `breakpoints` (array) variable | |
- In your terminal go to the folder where `screenshots-multipages.js` lives, and run | |
$ casperjs screenshots-multipages.js | |
* Your screenshots are generated in `screenshots` folder. | |
* | |
*/ | |
var casper = require('casper').create(), | |
baseUrl = "http://www.bbc.co.uk/", | |
screenshotFolder = 'screenshots', | |
links = [ | |
'', // an empty string means the home page | |
'news/business-23643700' // if you have more levels it would be something like /chapter1/errata/ | |
], | |
breakpoints = [ | |
400, | |
600, | |
900, | |
1200 | |
]; | |
casper.start(); | |
// casper.userAgent(''); // assign a UA String if the site does UA sniffing | |
/*! | |
* USER AGENT STRINGS | |
* | |
* iPhone 6 w/ios 8 - Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/600.1.3 (KHTML, like Gecko) Version/8.0 Mobile/12A4345d Safari/600.1.4 | |
* iPhone 5 w/ios 7 - Mozilla/5.0 (iPhone; CPU iPhone OS 7_0 like Mac OS X; en-us) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53 | |
* iPad - Mozilla/5.0 (iPad; CPU OS 7_0 like Mac OS X) AppleWebKit/537.51.1 (KHTML, like Gecko) Version/7.0 Mobile/11A465 Safari/9537.53 | |
* Nexus 5 - Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 5 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19 | |
* Nexus 7 - Mozilla/5.0 (Linux; Android 4.3; Nexus 7 Build/JSS15Q) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/29.0.1547.72 Safari/537.36 | |
* | |
*/ | |
function nameFile(link, breakpoint) { | |
var name; | |
if (link === '') { | |
name = 'home'; | |
} else { | |
name = link; | |
} | |
return screenshotFolder + '/' + name.replace(/\//g, '_') + breakpoint + '.png'; | |
} | |
links.forEach(function (link) { | |
casper.thenOpen(baseUrl + link, function () { | |
breakpoints.forEach(function (breakpoint) { | |
casper.viewport(breakpoint, 800).capture(nameFile(link, breakpoint), { | |
top: 0, | |
left: 0, | |
width: breakpoint, | |
height: casper.evaluate(function() { | |
return document.body.scrollHeight; | |
}) | |
}); | |
}); | |
}); | |
}); | |
casper.run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment