Skip to content

Getting Started with PhantomJS – A New Journey Begins! [Book Review]


Being in IT for almost two decades now I am perhaps lucky to have witnessed the growth of this industry.  More so, I am glad to see the “rebirth” of Java Scripting for front-end development in the recent years.  But ask any front-end developer and one thing they dreaded most was testing these web pages and Java Scripts.  PhantomJS – however is a game changer!
//Say Hello with PhantomJS 
console.log(‘Hello, world!’);
phantom.exit();
PhantomJS provides “headless” testing of web applications. Wait, what did I mean with headless? When you are typing a URL from the browser, it essentially creates a request and the response is reflected on the page.  PhantomJS actually does the same thing, EXCEPT we don’t need to wait for it to be rendered before our eyes! Probably this is how it got its name 🙂
PhantomJS also can dynamically capture/render pages as images, allow manipulation of page content/event,  gain access to network-level information, and ability to save important infos into files for later processing.
  
//This is how to load a page in PhantomJS:
var page = require(‘webpage’).create();
page.open(“http://www.amazon.com”, function (status) {
     if (status === “success”) {
         console.log(page.title);
     } else {
         console.log(“Page load failed.”);
     }
});

I find this book easy to read with only 121 pages, you can actually get done with it in one sitting (but would be good to do it in front of your laptop trying out the sample codes yourself).   However, it is advised you already have concrete knowledge of Java Scripts, HMTL, and CSS since the book heavily used later technologies such as DOM, JSON, and HTML5.
An improvement I guess would be since the goal of the book was to jumpstart IT practitioners into PhantomJS, it would have been better to have laid out the different syntaxes of PhantomJS itself.  In Chapter 2, I was surprised to see “===” as the conditional operator.  Having used to “==” in Java, I had to check this if it was a typo. 
//Example using HTTP POST operation
var page = require(‘webpage’).create(),
    data = ‘universe=expanding&answer=42’;

page.open(server, ‘post’, data, function (status) {
    if (status !== ‘success’) {
        console.log(‘Unable to post!’);
    } else {
        console.log(page.content);
    }
    phantom.exit();
});

The book has more to give than what I expected.  All throughout, the book used actual web pages (e.g. Pinterest, Instagram) as examples for us to run our tests, which challenges our creativity and encourages us to further try it out with other pages.  Aside from PhantomJS, I liked how in a subtle way, the author introduced other technologies such as Yahoo Local Search and Google Directions API (which made me realize how easy it was to use them!).  In the last chapter, the book introduced CasperJS – a spin-off extension and further simplified PhantomJS. I find this really exciting!
The book is concise and straightforward, I highly encourage everyone doing front-end development to read this and for Technical Architects to consider PhantomJS at production work.  

On a side note, I noticed that the author used his family member’s names all throughout the book, so I guess this was in some way a personal book.  Anyway, I am excited for PhantomJS and is looking forward to further playing with it!

You can purchase the book via http://www.packtpub.com/getting-started-with-phantomjs/book or through Amazon.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: