Ghosts & Ghouls / CasperJS & PhantomJS

Kill everyday mundane tasks with these two beautiful pieces of JS.



PhantomJS is a headless WebKit browser which is scriptable with the JavaScript API. It has fast and native support for various web standards: DOM handling, CSS selector, JSON, Canvas, and SVG.

So what is headless I hear you say? Well basically headless means it doesn't draw on your screen (It should really be called GUIless). It can do all the same things as your favourite browsers, but faster; which makes it perfect for automating and testing applications programmatically.

Not all is lost though if you want to see something, you have the power of using PhantomJS w/ CasperJS to produce automated responsive screenshots.

To get the party started, install using Homebrew:

brew update && brew install phantomjs

Once the music is playing and your hips are shaking, lets go ahead and write a simple application. The following application will open the website of your choice and takes a screenshot.

The first line of our application creates a new instance of a webpage. Just like opening chrome for the first time. The second line sets our homePage variable which we pass to our webpage to load. Once the page has finished loading the onLoadFinished() callback function is executed. The callback receives a single argument, status, which indicates whether the page loaded successfully or not.

Once all of that is done we have access to all the pages parameters and some worth mentioning are:

page.cookie — Get or set Cookies visible to the current URL
page.settings — This property stores various settings of the web page such as if Javascript is enabled, the userAgent and username/password set for HTTP Auth

We will be using page.url parameter which gets the current URL of the loaded webpage. This property can be particularly useful when pages contain redirects, and you want to know exactly where you landed.

Lets finish off by taking a screenshot using the page.render() method, which can create PNG, GIF, JPEG, and PDF files.

All of that in 10 lines of code and faster than the time it took to read this sentence.

var page = require("webpage").create();
var homePage = "";;
page.onLoadFinished = function(status) {
var url = page.url
    console.log("Status: " + status);
console.log("Loaded: " + url);


CasperJS is a navigation scripting and testing utility for PhantomJS. Think of it like the JS version of PHPUnit. I use it for a wide variety of tasks such as CMS input, webscraping, testing, rigging voting systems and my last and most fun automated task; login to coworkers gmails to see who hadn’t changed their work email password (haX0r). Another nice little feature is that is supports CoffeeScript, if you’re that way inclined.

To get this party started, install using Homebrew:

brew updated && brew install casperjs

Its now time for another simple application to wet our huge appetites. Instead of running you through a hello world example I’ll link you to my haX0r gmail project.

NOTE: I am not responsible for any trouble you get into or cause using this script. Please use it responsibly and at your own risk. I used it in a work environment when it was important for people to change their passwords to protect our work.

There is a readme inside the repo, this should explain everything you need to know:

Gmail Haxor
Author: @jjkilpatrick /
This script will run through your defined list of emails (input/test.csv) and attempt to login using the password set in the gmail.js app ( If the script is successful it will place the email in the ‘naughty’ list and save this in output/results.json.
NOTE: I am not responsible for any trouble you get into or cause using this script. Please use it responsibly and at your own risk.
## Instructions for use
1. Ensure you have PhantomJS installed on your machine. I suggest using Homebrew:
2. Ensure you have CasperJS installed on your machine I suggest using Homebrew:
3. Clone the repo: ``
4. Make sure that input/data.csv is populated with your emails. List should be comma seperated
5. Then run “casperjs gmail.js”
6. When the script has finished, your results will be found in the output directory ‘output’

Updated Link — You can download / fork the gmail hax0r script here.

If you liked this, please hit me up with a “Recommend”. Thank you.

Show your support

Clapping shows how much you appreciated John Kilpatrick’s story.