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 PhantomJS with a JSON configuration file

In this recipe, we will learn how to store PhantomJS configuration options in a JSON document and load those options using the config command-line argument.

Getting ready

To run this recipe, we will need a JSON-formatted configuration file with our PhantomJS command-line options.

The script in this recipe is available in the downloadable code repository as recipe07.js under chapter01. If we run the provided example script, we must change to the root directory for the book's sample code. An example configuration file is also in this directory as recipe07-config.json.

Lastly, the script in this recipe runs against the demo site that is included with the cookbook's sample code repository. To run that demo site, we must have Node.js installed. In a separate terminal, change to the phantomjs-sandbox directory (in the sample code's directory) and start the app with the following command:

node app.js

How to do it…

Select our command-line configuration options (changing hyphenated property names into their camel-cased equivalents) and apply our values. Save these configuration settings to a JSON-formatted document. For example, the contents of recipe07-config.json under chapter01:

{
  "cookiesFile"     : "cookie-jar.txt",
  "ignoreSslErrors" : true
}

Tip

For more information about JSON, including its formatting rules, visit http://www.json.org.

Given the script from the Running PhantomJS with cookies recipe earlier in this chapter, enter the following at the command line:

phantomjs --config=chapter01/recipe07-config.json chapter01/recipe07.js

How it works…

The configuration file is a JSON document where we can take our preferred command-line arguments and store them on disk. The keys in the JSON object have a one-to-one correspondence with the command-line arguments themselves – the hyphenated command-line argument names are converted to their camel-cased versions (for example, cookies-file becomes cookiesFile). The values in the JSON object follow easy conversion rules based on the most applicable JavaScript primitives: strings are strings, numbers are numbers, and true/false or yes/no become the corresponding true or false Boolean literals. Creating our own JSON-formatted configuration file requires only two things: a text editor and the knowledge of which command-line arguments we wish to capture in it.

Tip

See http://phantomjs.org/api/command-line.html for the complete list of documented command-line options in the PhantomJS API.

Note

The help and version command-line arguments do not have corresponding versions in the JSON configuration file. Also, at the time of writing this book, there is a documented defect wherein the JSON key for the load-images argument is not recognized.

The example script in this recipe (recipe07.js under chapter01) is identical to the one that we used for our demonstration in the Running PhantomJS with cookies recipe; we are reusing it here for convenience. For a more thorough explanation of what it is doing, see the How it works… section under that recipe.

When launching PhantomJS with the config command-line argument, the PhantomJS runtime interprets the argument's value as a path on the filesystem and attempts to load and evaluate that file as a JSON document. If the file cannot be parsed as a JSON document, then PhantomJS prints a warning and ignores it. If the file is correctly parsed, then PhantomJS configures itself as if the arguments in the JSON document had been passed as normal command-line arguments.

This raises an interesting question: given equivalent arguments, which one takes precedence? The one specified in the JSON configuration file? Or the one specified on the command line? The answer is that it depends which one comes last. In other words, given recipe07-config.json, we can run:

phantomjs --cookies-file=jar-of-cookies.txt --config=chapter01/recipe07-config.json chapter01/recipe07.js

That creates cookie-jar.txt, as specified in recipe07-config.json. While the following command creates jar-of-cookies.txt, as specified on the command line:

phantomjs --config=chapter01/recipe07-config.json --cookies-file=jar-of-cookies.txt chapter01/recipe07.js

There's more…

Saving a PhantomJS configuration to a JSON document can help us in a couple of ways. First, by putting it into a file, we can put it under version control and track the changes to that configuration over time. Also, by putting the configuration into a file, it can more easily be shared across teams or jobs in continuous integration.

See also

  • The Running PhantomJS with cookies recipe
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