Free Sample
+ Collection

PhantomJS Cookbook

Rob Friesel

Over 70 recipes to help boost the productivity of your applications using real-world testing with PhantomJS
RRP $25.99
RRP $41.99
Print + eBook

Want this title & more?

$12.99 p/month

Subscribe to PacktLib

Enjoy full and instant access to over 2000 books and videos – you’ll find everything you need to stay ahead of the curve and make sure you can always get the job done.

Book Details

ISBN 139781783981922
Paperback304 pages

About This Book

  • Learn the fundamentals of working with the headless PhantomJS browser
  • Create unit and functional test suites backed by PhantomJS
  • Integrate PhantomJS into development and testing workflows for efficient unit testing

Who This Book Is For

This book is intended for web development professionals who want to integrate PhantomJS into their development and testing workflows. If you are a web developer looking to run automated unit tests while you work, or perhaps you are a QA engineer looking for a fast test automation utility, then this book is perfect for you. Some prior knowledge of JavaScript would be helpful.

Table of Contents

Chapter 1: Getting Started with PhantomJS
Installing PhantomJS
Launching the PhantomJS REPL
Running a PhantomJS script
Running a PhantomJS script with arguments
Running PhantomJS with cookies
Running PhantomJS with a disk cache
Running PhantomJS with a JSON configuration file
Debugging a PhantomJS script
Chapter 2: PhantomJS Core Modules
Inspecting the version at runtime
Managing cookies with the phantom object
Specifying a path for external scripts
Setting up a global PhantomJS error handler
Controlling the exit status of a PhantomJS script
Inspecting command-line arguments
Inspecting system environment variables
Saving a file from a PhantomJS script
Reading a file from PhantomJS
Creating a custom module for PhantomJS
Loading custom modules in PhantomJS
Chapter 3: Working with webpage Objects
Creating a web page instance in PhantomJS with the webpage module
Opening a URL within PhantomJS
Generating a POST request from PhantomJS
Inspecting page content from a PhantomJS script
Including external JavaScript on the page
Recording debugger messages
Simulating mouse clicks in PhantomJS
Simulating keyboard input in PhantomJS
Simulating scrolling in PhantomJS
Simulating mouse hovers in PhantomJS
Blocking CSS from downloading
Causing images to fail randomly
Submitting Ajax requests from PhantomJS
Working with WebSockets in PhantomJS
Chapter 4: Unit Testing with PhantomJS
Running Jasmine unit tests with PhantomJS
Using TerminalReporter for unit testing in PhantomJS
Creating a Jasmine test runner for PhantomJS and every other browser
Running Jasmine unit tests with Grunt
Watching your tests during development with Grunt
Running Jasmine unit tests with the Karma test runner
Generating code coverage reports with Istanbul and the Karma test runner
Running Jasmine unit tests with Karma and PhantomJS from WebStorm
Running QUnit tests with PhantomJS
Running Mocha unit tests with PhantomJS
Chapter 5: Functional and End-to-end Testing with PhantomJS
Running Selenium tests with PhantomJS and GhostDriver
Using WebdriverJS as a Selenium client for PhantomJS
Adding Poltergeist to a Capybara suite
Taking screenshots during tests with Poltergeist
Simulating precise mouse clicks with Poltergeist
Installing CasperJS
Interacting with web pages using CasperJS
End-to-end testing with CasperJS
Exporting test results from CasperJS in the XUnit format
Detecting visual regressions using PhantomCSS
Chapter 6: Network Monitoring and Performance Analysis
Generating HAR files from PhantomJS
Listing CSS properties
Generating an appcache manifest
Executing a simple performance analysis
Executing a detailed performance analysis
Executing a YSlow performance analysis with a custom ruleset
Automating performance analysis with YSlow and PhantomJS
Chapter 7: Generating Images and Documents with PhantomJS
Rendering images from PhantomJS
Saving images as Base64 from PhantomJS
Rendering and rasterizing SVGs from PhantomJS
Generating clipped screenshots from PhantomJS
Saving a web page from PhantomJS as a PDF
Applying custom headers and footers to PDFs generated from PhantomJS
Testing responsive designs with PhantomJS
Chapter 8: Continuous Integration with PhantomJS
Setting up PhantomJS in a CI environment
Generating JUnit reports
Generating TAP reports
Setting up a fully covered project in CI with PhantomJS

What You Will Learn

  • Develop your own PhantomJS core modules
  • Learn sophisticated strategies for interacting with web page contents and how to capture those interactions
  • Create a front-end continuous integration (CI) strategy with PhantomJS
  • Perform unit testing with frameworks such as Jasmine, Mocha, and QUnit
  • Implement functional and end-to-end testing with PhantomJS using tools such as Selenium, Capybara, and CasperJS
  • Automate performance analysis with libraries such as Confess.js and YSlow
  • Automate the generation of images and PDFs of your web content

In Detail

Beginning with the basics of PhantomJS, this book will dive into its core modules and guide you through how to solve real-world testing problems. This book explores a variety of test automation tasks, including executing JavaScript unit tests with the Jasmine, QUnit, and Mocha frameworks; functional tests with tools such as Selenium, Capybara, and CasperJS; performance analysis with tools such as YSlow; and much more.

If you are a web developer looking to run automated unit tests while you work, or perhaps you are a QA engineer looking for a fast test automation utility, then this book is perfect for you. Some prior knowledge of JavaScript would be helpful.


Read More