How to take screenshots at different screen sizes using CasperJS?
Today, I am going to show you guys how to take screenshots at different screen sizes using CasperJS. To make it more fun, we are going to take a screenshots on different device with variety screen sizes. For example, iPhone 4s, iPhone 5, iPhone 6, iPad mini 4, Desktop 720p, and Desktop 1080p. Also, to make this script more useful, the urls that we will be getting the screenshots are from a file that may contains as many urls as you want. I believe this script will be useful for QA to test their site to see what will their site looks like on different device. If you haven’t already, install CasperJS or PhantomJS before we continue on. Now, let’s dive right in.
CasperJS code (screenshots.js) to start with
The code will simply tried to see if user provide an input file, otherwise, it will simply throws error and abort. Next things is, we need to get all the urls from the input file.
CasperJS code (screenshots.js) to get the contents from the input file
Now, the @variable should have the contents of the file when you execute like this, $ casperjs screenshots.js urls.json. Of course, the urls.json should look something like this.
Now, what we are having in the @variable contents is a string. But, it is not so much helpful for us because it is a string and we need to get the urls from it. As a result, we need to use the JSON.parse to convert the string to JSON, so that JavaScript can read it.
CasperJS code (screenshots.js) to use JSON.parse to read the urls from the @variable contents
Now the @variable urls should have all the urls from the input file. Now, we need to open each url. This is how we are going to do.
CasperJS code (screenshots.js) to open each url
A sample of the output of this would look something like this.
Now that each urls has been open, we need to set the height and width for different screen size. So that we can take a screenshot for different size of screen later. Notice that we will be including device like iPhone 4s, iPhone 5, iPhone 6, iPad mini 4, Desktop 720p, and Desktop 1080p. And, the code will look something like this.
CasperJS code (screenshots.js) for different device at different screen size
This will be the list of width and height for different device at different screen size. And, now we have this. We can try to take a screenshot on different device for each url that we opened.
CasperJS code (screenshots.js) for taking screenshot for different device for each url that we opened
A sample of the output of this would look something like this.
Now, when you run the code like this, $ casperjs screenshots.js urls.json, you should see screenshots has been taken for each url that we opened on different device. To be more clear, the full code for screenshots.js is as follows.
CasperJS code (screenshots.js) for taking screenshot for different device for each url that we opened
Hopefully this guide has given you the confidence to use CasperJS to open urls and take a screenshot. I hope that this tutorial has helped you and thank you for reading!
Resources
I’ll try to keep this list current and up to date. If you know of a great resource you’d like to share or notice a broken link, please let us know.
Comments