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

Running a PhantomJS script with arguments

In this recipe, we will learn how to run a PhantomJS script with additional arguments that are passed into the script for evaluation. Note that these are arguments passed into the execution context and are not command-line arguments for the PhantomJS runtime itself.

Getting ready

To run this recipe, we will need a script to run with PhantomJS; the script in this recipe is available in the downloadable code repository as recipe04.js under chapter01. If we run the provided example script, we must change to the root directory for the book's sample code. Lastly, we will need the arguments we wish to pass into the script.

How to do it…

Given the following script:

if (phantom.args.length === 0) {
  console.log('No arguments were passed in.');
} else {
  phantom.args.forEach(function(arg, index) {
    console.log('[' + index + '] ' + arg);
  });
}

phantom.exit();

Enter the following command at the command line:

phantomjs chapter01/recipe04.js foo bar "boo baa"

We will see the following results printed in the terminal:

[0] foo
[1] bar
[2] boo baa

How it works…

Our preceding example script performs the following actions:

  1. It checks the length of the phantom.args array and prints a message if that array is empty.
  2. If the phantom.args array is not empty, we iterate over the items in the array, printing their index followed by the value of the argument itself.
  3. Lastly, we exit from the PhantomJS runtime using phantom.exit.

As we discussed in the Running a PhantomJS script recipe, PhantomJS will attempt to evaluate and execute the first unrecognized argument as though it were a valid JavaScript file. But what does PhantomJS do with all of the arguments after that?

The answer is that they are collected into the phantom.args array as string values. Each argument after the script name goes into this array. Note that phantom.args does not include the script name itself. Instead, PhantomJS records that in the read-only phantom.scriptName property.

There's more…

It is worth noting that both phantom.args and phantom.scriptName are both marked as deprecated in the API documentation. As such, usage of both of these properties is discouraged. Although using them for quick one-off or exploratory scripts is fine, neither of these properties should go into any library that we intend to maintain or distribute.

Wherever possible, we should use the system.args array (from the system module) instead of phantom.args and phantom.scriptName.

Tip

When in doubt, check the PhantomJS project website and its documentation at http://phantomjs.org/api/. It is actively maintained, and as such will contain up-to-date information about the preferred APIs.

See also

  • The Inspecting command-line arguments recipe in Chapter 2, PhantomJS Core Modules
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