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

Launching the PhantomJS REPL

In this recipe, we will learn how to use the PhantomJS REPL. The PhantomJS REPL is an excellent tool for getting familiar with the runtime environment and for quickly hacking out an idea without needing to write a fully qualified script.

Getting ready

To run this recipe, we will need to have PhantomJS installed on our PATH. We will also need an open terminal window.

How to do it…

Perform the following steps to invoke and work in the PhantomJS REPL:

  1. At the command-line prompt, type the following:
    phantomjs
    
  2. When the PhantomJS REPL starts up, we should see its default command-line prompt:
    phantomjs>
    
  3. At the PhantomJS prompt, we can enter any command from the PhantomJS API or any other valid JavaScript expressions and statements. The REPL will print the return value from the expression we entered, although we may need to wrap the expression in a console.log statement for a readable response, for example:
    phantomjs> 1 + 1
    {}
    phantomjs> console.log(1 + 1)
    2
    undefined
    phantomjs> for (var prop in window) console.log(prop)
    document
    window
    // 475 more...
    undefined
    
  4. When we are finished in the REPL, type the following command to exit the REPL:
    phantom.exit()
    

How it works…

The PhantomJS REPL, also called interactive mode, was introduced to PhantomJS starting with Version 1.5. The REPL is the default mode for PhantomJS when the application is invoked without any arguments.

REPL stands for Read-Evaluate-Print Loop. The commands we enter at the prompt are read by the interpreter, which evaluates them and prints the results, before finally looping back to the prompt for us to continue. Many programming environments feature REPLs (for example, Node.js provides another popular JavaScript REPL), and the debugger consoles in tools such as the Chrome Developer Tools and Firebug would also qualify as REPLs. REPLs are useful for quickly trying out ideas in the runtime environment.

In our example, we enter the PhantomJS REPL by invoking phantomjs from the command line without any arguments. Once we are in the REPL, we can type in whatever commands we need to explore our ideas, hitting Enter after each command. Note that we must enter a full and syntactically valid expression or statement before hitting Enter; if we do not, PhantomJS will report an error (for example, Can't find variable: foo or Parse error).

The PhantomJS REPL also features auto-completion. Hitting the Tab key in the REPL will autoexpand our options. We can even hit Tab multiple times to cycle through our available options; for example, try typing p and then hit Tab to see what options the REPL presents.

Finally, when we are finished, we use phantom.exit() to leave the REPL; we can also use the Ctrl + C or Ctrl + D key commands to exit the REPL.

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 €18.99/month. Cancel anytime