Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
PhantomJS Cookbook

You're reading from   PhantomJS Cookbook Over 70 recipes to help boost the productivity of your applications using real-world testing with PhantomJS

Arrow left icon
Product type Paperback
Published in Jun 2014
Publisher
ISBN-13 9781783981922
Length 304 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Rob Friesel Rob Friesel
Author Profile Icon Rob Friesel
Rob Friesel
Arrow right icon
View More author details
Toc

Table of Contents (10) Chapters Close

Preface 1. Getting Started with PhantomJS 2. PhantomJS Core Modules FREE CHAPTER 3. Working with webpage Objects 4. Unit Testing with PhantomJS 5. Functional and End-to-end Testing with PhantomJS 6. Network Monitoring and Performance Analysis 7. Generating Images and Documents with PhantomJS 8. Continuous Integration with PhantomJS 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.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at AU $24.99/month. Cancel anytime