Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying PhantomJS Cookbook
  • Table Of Contents Toc
  • Feedback & Rating feedback
PhantomJS Cookbook

PhantomJS Cookbook

By : Friesel
4.2 (5)
close
close
PhantomJS Cookbook

PhantomJS Cookbook

4.2 (5)
By: Friesel

Overview of this book

A task-based guide that provides solutions to real-world test automation problems. 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 (10 chapters)
close
close
9
Index

Installing PhantomJS

Let's begin the PhantomJS Cookbook with the recipe that is the prerequisite for all of the other recipes—downloading and installing PhantomJS so that it is available on our computers.

Prebuilt binaries of PhantomJS are available for most major platforms, and in the interest of expedience and simplicity, that is how we proceed. PhantomJS is designed to be a stand-alone application, and in most situations, no external dependencies are required.

Getting ready

To install PhantomJS, we will need access to the Internet and permission to install applications.

How to do it…

Perform the following steps to download and install PhantomJS:

  1. Navigate to the PhantomJS download page at http://phantomjs.org/download.
  2. Locate and download the prebuilt binary that is appropriate for our system. Prebuilt binaries exist for the following operating systems:
    • Windows (XP or later).
    • Mac OS X (10.6 or later).
    • Linux (for 32-bit or 64-bit systems). Current binaries are built on CentOS 5.8, and should run successfully on Ubuntu 10.04.4 (Lucid Lynx) or more modern systems.
  3. Extract the prebuilt binary. For Windows and OS X systems, this will be a .zip archive; for Linux systems, this will be a .tar.bz2 archive. For Windows machines, the binary should be phantomjs.exe; for OS X and Linux machines, the binary should be bin/phantomjs.

    Note

    We should place the binary somewhere on your system that makes sense to us.

  4. Once extracted, make sure to add PhantomJS to the system's PATH.

    Tip

    The PATH or search path is a variable on the command line that contains a list of directories searched by the shell to find an executable file when it is called. On POSIX-compatible systems (Linux and OS X), this list is delimited by colons (:), and on Windows, it is delimited by semicolons (;). For more information about the PATH variable, visit http://en.wikipedia.org/wiki/PATH_(variable).

    For a tutorial that focuses on POSIX-compatible systems, visit http://quickleft.com/blog/command-line-tutorials-path.

    For documentation on the Windows PATH, visit http://msdn.microsoft.com/en-us/library/w0yaz275(v=vs.80).aspx.

  5. After placing the PhantomJS binary on our PATH, we can verify that it was installed by typing the following in the command line:
    phantomjs –v
    

The version of PhantomJS that we just installed should print out to the console.

Tip

If we have trouble here, we should check out the troubleshooting guide on the PhantomJS project site at http://phantomjs.org/troubleshooting.html.

How it works…

In an effort to lower the barrier to entry and help drive adoption, the prebuilt binaries of PhantomJS are made available by community volunteers. This is, in part, an acknowledgment that building PhantomJS from the source code can be a complex and time-consuming task. To quote the build page on the PhantomJS site: "With 4 parallel compile jobs on a modern machine, the entire process takes roughly 30 minutes." It is easy to imagine that this might scare off many developers who just want to try it out.

These prebuilt binaries should therefore make it easy to drop PhantomJS onto any system and have it running in minutes. These binaries are intended to be fully independent applications, with no external library dependencies such as Qt or WebKit. On some Linux systems, however, a little extra work may be required to ensure that the libraries necessary for proper font rendering (for example, FreeType and Fontconfig) are in place, along with the basic font files.

Note

Throughout this book, our code will assume that we are using Version 1.9 or higher of PhantomJS.

There's more…

In addition to the prebuilt binaries, Mac OS X users may also install PhantomJS using Homebrew. To do this, enter the following as the command line:

brew update && brew install phantomjs

Note that installing PhantomJS with Homebrew also means that we will be compiling it from source.

Tip

Homebrew is an open source, community-run package manager for OS X built on top of Git and Ruby. To find out more information about Homebrew, check out its website at http://brew.sh.

As a bonus, Homebrew also automatically adds PhantomJS to your PATH.

Installing from Source

In the event that one of the prebuilt binaries is not suitable for your specific situation, you may need to consider building PhantomJS from the source code. If this is the case, you will want to check out the build instructions that are listed at http://phantomjs.org/build.html; note that you will need the developer tools specific to your system (for example, Xcode on OS X and Microsoft Visual C++ on Windows) to be installed before you begin.

Visually different images
CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
PhantomJS Cookbook
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon