Book Image

Getting Started with PhantomJS

By : Aries beltran
Book Image

Getting Started with PhantomJS

By: Aries beltran

Overview of this book

PhantomJS is a headless WebKit browser with JavaScript API that allows you to create new ways to automate web testing. PhantomJS is currently being used by a large number of users to help them integrate headless web testing into their development processes. It also gives you developers a new framework to create web-based applications, from simple web manipulation to performance measurement and monitoring.A step step-by by-step guide that will help you develop new tools for solving web and testing problems in an effective and quick way. The book will teach you how to use and maximize PhantomJS to develop new tools for web scrapping, web performance measurement and monitoring, and headless web testing. This book will help you understand PhantomJS’ scripting API capabilities and strengths.This book starts by looking at PhantomJS’ JavaScript API, features, and basic execution of scripts. Throughout the book, you will learn details to help you write scripts to manipulate web documents and fully create a web scrapping tool.Through its practical approach, this book strives to teach you by example, where each chapter focuses on the common and practical usage of PhantomJS, and how to extract meaningful information from the web and other services.By the end of the book, you will have acquired the skills to enable you to use PhantomJS for web testing, as well as learning the basics of Jasmine, and how it can be used with PhantomJS.
Table of Contents (13 chapters)
12
Index

Writing PhantomJS scripts

We know how to write JavaScript, and now we know that there are several PhantomJS JavaScript APIs and objects. We also have learned the basics of the PhantomJS command-line arguments. We are now ready to create our own scripts.

We will create a simple script to load a site and then display the title of the page when loaded successfully. Finally, we will exit. If the page fails to load, we will log some message to the console.

var page = require('webpage').create();
page.open("http://www.packtpub.com", function(status) {
   if ( status === "success" ) {
      console.log(page.title); 
   } else {
      console.log("Page failed to load."); 
   }
   phantom.exit(0);
});

The preceding PhantomJS script is very simple. First, we import the webpage module, create an instance of the webpage object, and assign it to a variable named page.

The page variable now holds an instance of the webpage module where an open function is available. Next, we instructed PhantomJS through the webpage instance to open and load the URL. The second parameter of the open function is a function callback definition that will be executed upon completion of the opening of the URL. Inside the definition, we check if the status is "success", and if it is, the page is loaded, and then we will display the title of the page. Then, we call the exit function to terminate the script. Let's save this code snippet as helloweb.js and execute it by passing the filename as our first argument to the phantomjs binary.

Writing PhantomJS scripts