Book Image

PhantomJS Cookbook

By : Rob Friesel
Book Image

PhantomJS Cookbook

By: Rob Friesel

Overview of this book

Table of Contents (15 chapters)
PhantomJS Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
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.