You are previewing PhantomJS Cookbook.
O'Reilly logo
PhantomJS Cookbook

Book Description

Over 70 recipes to help boost the productivity of your applications using real-world testing with PhantomJS

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.

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
  • Downloading the example code for this book. 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.

    Table of Contents

    1. PhantomJS Cookbook
      1. Table of Contents
      2. PhantomJS Cookbook
      3. Credits
      4. About the Author
      5. About the Reviewers
      6. www.PacktPub.com
        1. Support files, eBooks, discount offers, and more
          1. Why Subscribe?
          2. Free Access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Getting Started with PhantomJS
        1. Introduction
        2. Installing PhantomJS
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Installing from Source
        3. Launching the PhantomJS REPL
          1. Getting ready
          2. How to do it…
          3. How it works…
        4. Running a PhantomJS script
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        5. Running a PhantomJS script with arguments
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        6. Running PhantomJS with cookies
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        7. Running PhantomJS with a disk cache
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. disk-cache
            2. max-disk-cache-size
            3. Cache locations
          5. See also
        8. Running PhantomJS with a JSON configuration file
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        9. Debugging a PhantomJS script
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. remote-debugger-port
            2. remote-debugger-autorun
      9. 2. PhantomJS Core Modules
        1. Introduction
        2. Inspecting the version at runtime
          1. Getting ready
          2. How to do it…
          3. How it works…
        3. Managing cookies with the phantom object
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        4. Specifying a path for external scripts
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. phantom.libraryPath
            2. phantom.injectJs
            3. phantom.injectJs versus require
          5. See also
        5. Setting up a global PhantomJS error handler
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. onError parameters
          5. See also
        6. Controlling the exit status of a PhantomJS script
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Type coercion with phantom.exit
        7. Inspecting command-line arguments
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Establishing a command-line convention
          5. See also
        8. Inspecting system environment variables
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        9. Saving a file from a PhantomJS script
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. exists
            2. makeDirectory
            3. isWritable
            4. write
          5. See also
        10. Reading a file from PhantomJS
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. changeWorkingDirectory
            2. open
            3. stream objects
              1. atEnd
              2. readLine
              3. close
              4. Other stream methods
          5. See also
        11. Creating a custom module for PhantomJS
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        12. Loading custom modules in PhantomJS
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
      10. 3. Working with webpage Objects
        1. Introduction
        2. Creating a web page instance in PhantomJS with the webpage module
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. The WebPage constructor
        3. Opening a URL within PhantomJS
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        4. Generating a POST request from PhantomJS
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        5. Inspecting page content from a PhantomJS script
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        6. Including external JavaScript on the page
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. includeJs
            2. injectJs
          5. See also
        7. Recording debugger messages
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        8. Simulating mouse clicks in PhantomJS
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        9. Simulating keyboard input in PhantomJS
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        10. Simulating scrolling in PhantomJS
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        11. Simulating mouse hovers in PhantomJS
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        12. Blocking CSS from downloading
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. onResourceRequested
            2. onResourceReceived
          5. See also
        13. Causing images to fail randomly
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. onResourceError
          5. See also
        14. Submitting Ajax requests from PhantomJS
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        15. Working with WebSockets in PhantomJS
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
      11. 4. Unit Testing with PhantomJS
        1. Introduction
        2. Running Jasmine unit tests with PhantomJS
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        3. Using TerminalReporter for unit testing in PhantomJS
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        4. Creating a Jasmine test runner for PhantomJS and every other browser
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        5. Running Jasmine unit tests with Grunt
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        6. Watching your tests during development with Grunt
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        7. Running Jasmine unit tests with the Karma test runner
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        8. Generating code coverage reports with Istanbul and the Karma test runner
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        9. Running Jasmine unit tests with Karma and PhantomJS from WebStorm
          1. Getting ready
          2. How to do it…
          3. How it works…
        10. Running QUnit tests with PhantomJS
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        11. Running Mocha unit tests with PhantomJS
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
      12. 5. Functional and End-to-end Testing with PhantomJS
        1. Introduction
        2. Running Selenium tests with PhantomJS and GhostDriver
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        3. Using WebdriverJS as a Selenium client for PhantomJS
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        4. Adding Poltergeist to a Capybara suite
          1. Getting ready
          2. How to do it…
          3. How it works…
        5. Taking screenshots during tests with Poltergeist
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        6. Simulating precise mouse clicks with Poltergeist
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        7. Installing CasperJS
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        8. Interacting with web pages using CasperJS
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        9. End-to-end testing with CasperJS
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        10. Exporting test results from CasperJS in the XUnit format
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        11. Detecting visual regressions using PhantomCSS
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
      13. 6. Network Monitoring and Performance Analysis
        1. Introduction
        2. Generating HAR files from PhantomJS
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
        3. Listing CSS properties
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        4. Generating an appcache manifest
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Configuration options
          5. See also
        5. Executing a simple performance analysis
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Configuration options
          5. See also
        6. Executing a detailed performance analysis
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Information
            2. Format
            3. Ruleset
            4. User agent
            5. Viewport
            6. Headers
            7. CDNs
          5. See also
        7. Executing a YSlow performance analysis with a custom ruleset
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        8. Automating performance analysis with YSlow and PhantomJS
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. Using TAP format
          5. See also
      14. 7. Generating Images and Documents with PhantomJS
        1. Introduction
        2. Rendering images from PhantomJS
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        3. Saving images as Base64 from PhantomJS
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        4. Rendering and rasterizing SVGs from PhantomJS
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        5. Generating clipped screenshots from PhantomJS
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. CasperJS
          5. See also
        6. Saving a web page from PhantomJS as a PDF
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. webpage.paperSize
              1. width and height
              2. format
              3. orientation
              4. border
          5. See also
        7. Applying custom headers and footers to PDFs generated from PhantomJS
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
            1. height
            2. contents
          5. See also
        8. Testing responsive designs with PhantomJS
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
      15. 8. Continuous Integration with PhantomJS
        1. Introduction
        2. Setting up PhantomJS in a CI environment
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        3. Generating JUnit reports
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. There's more…
          5. See also
        4. Generating TAP reports
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
        5. Setting up a fully covered project in CI with PhantomJS
          1. Getting ready
          2. How to do it…
          3. How it works…
          4. See also
      16. Index