Book Image

PhantomJS Cookbook

By : Rob Friesel
Book Image

PhantomJS Cookbook

By: Rob Friesel

Overview of this book

Table of Contents (15 chapters)
PhantomJS Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Preface

With all the exciting things going on in the browser space, this is a fantastic time to be a front-end developer. The family of technologies that we call HTML5 is giving us new opportunities that were difficult or even impossible just a few years ago, and JavaScript has flourished alongside it as rich web applications have become the norm. Throughout this time, the WebKit project has emerged as the leader of this innovative streak. If you are unfamiliar with WebKit, it is an open source web browser engine with contributors from companies such as Apple, Google, and Nokia, to name a few. WebKit powers Safari, versions of Chrome, and PhantomJS.

The reason you are reading this book is because you have discovered PhantomJS and want to harness its full potential.

PhantomJS is one of the most important innovations in the front-end development tool chain in the last several years. It has proven to be the ideal environment for lightning-fast tests, both manual and automated. Since it is simply a specialized build of WebKit, front-end developers can have confidence that their tests are being executed in a real browser, not a simulated environment. As it is truly headless, it can be deployed anywhere without the hassle of configuring Xvfb. Perhaps best of all, PhantomJS is fully scriptable using JavaScript, a tool that every front-end developer already knows. All these elements combined have uniquely positioned PhantomJS as the preferred testing environment among front-end developers for quick feedback and continuous integration.

The PhantomJS Cookbook focuses on using PhantomJS as the preferred testing environment. This book provides practical recipes that demonstrate the fundamentals of this headless browser and also help you take advantage of it for a variety of testing tasks. In this book, you will learn how to integrate PhantomJS into your development workflow at all stages. You will learn how you can receive immediate feedback from your unit tests. You will learn how to create a functional test suite that is both fast and automatic. Also, you will learn how to add PhantomJS to your continuous integration system so that you can make end-to-end and front-end performance tests first-class citizens of your build.

What this book covers

Chapter 1, Getting Started with PhantomJS, introduces the PhantomJS browser and how to work with it from the shell. It covers installing PhantomJS and how to run it with different command-line arguments.

Chapter 2, PhantomJS Core Modules, discusses the core modules in PhantomJS, such as phantom and system, and covers how to use the fs module to work with the filesystem. The chapter also explains how to create your own modules and load them into your PhantomJS scripts.

Chapter 3, Working with webpage Objects, introduces webpage objects and includes sophisticated strategies for dealing with web page content. You will learn how to interact with the page, simulate events, and capture those interactions for successful tests.

Chapter 4, Unit Testing with PhantomJS, explores how to use PhantomJS as an environment for JavaScript unit tests. This chapter focuses on the Jasmine BDD testing framework, but will also introduce two other popular frameworks, Mocha and QUnit.

Chapter 5, Functional and End-to-end Testing with PhantomJS, demonstrates functional and end-to-end testing strategies with PhantomJS. The chapter surveys several different functional testing tools, including Selenium, Poltergeist (a driver for Capybara), and CasperJS.

Chapter 6, Network Monitoring and Performance Analysis, illustrates how to perform automated performance analysis with PhantomJS. The chapter explores topics such as how to generate a HAR file for waterfall analysis, and how to use libraries such as confess.js and YSlow for automated performance analysis.

Chapter 7, Generating Images and Documents with PhantomJS, shows how to generate images and PDFs with PhantomJS. The chapter provides an overview of PhantomJS' built-in image rendering features and explains how to apply them.

Chapter 8, Continuous Integration with PhantomJS, demonstrates PhantomJS as part of a continuous integration (CI) strategy. The chapter surveys CI, using Jenkins as its specimen, and shows how to fail builds on that system, concluding with a recipe for comprehensive CI example.

What you need for this book

By and large, the only things that you will need for the recipes in this cookbook are your normal web development toolkit and PhantomJS. For most recipes, you will not need anything more than a terminal and a text editor or IDE. Some recipes, such as those that discuss functional testing or continuous integration, will require other specific pieces of software to be installed (for example, Selenium, Capybara, Jenkins, and so on), but those requirements will be discussed in context with those recipes.

Also, many of the recipes in this book illustrate their principles by executing against a Node.js-based demonstration application. If you wish to follow along with the recipes exactly as is, you will need to have Node.js version 0.10.2 or greater installed on your system.

Who this book is for

The PhantomJS Cookbook is targeted at experienced web developers who are interested in using PhantomJS to add a comprehensive testing strategy to their development workflows. This book assumes that you already have knowledge of the foundational front-end development skills (such as JavaScript, HTML, and CSS) and some experience with testing fundamentals. Some familiarity with PhantomJS is beneficial but not strictly required. Lastly, some recipes may involve some other programming languages (for example, Java or Ruby) and these will be called out where necessary.

Conventions

In this book, you will find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "We can launch the PhantomJS REPL from the command line using the phantomjs command."

A block of code is set as follows:

var system = require('system');
system.args.forEach(function(arg, i) {
  console.log(i + ' = ' + arg);
});
phantom.exit();

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

phantom.onError = function onErrorFn(msg, trace) {
  console.error('[PHANTOMJS ERROR] ' + msg);
  phantom.exit(1);
};

Any command-line input or output is written as follows:

phantomjs --cookies-file=cookie-jar access-secure-site.js

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "Lastly, click on Save to persist the changes to this job."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to , and mention the book title via the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you. The example code is also available on GitHub at https://github.com/founddrama/phantomjs-cookbook.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at if you are having a problem with any aspect of the book, and we will do our best to address it.