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

Controlling the exit status of a PhantomJS script


Although we have seen and used phantom.exit in all of our previous examples, we will now discuss it explicitly and learn in detail how it is used. In this recipe, we will learn how to control the exit status of the PhantomJS application.

Getting ready

To run this recipe, we require a script where we need to control the exit status.

The script in this recipe is available in the downloadable code repository as recipe05.js under chapter02. If we run the provided example script, we must change to the root directory for the book's sample code.

How to do it…

Consider the following script:

console.log('Running the PhantomJS exit demo...');

if (Math.floor(Math.random() * 10) % 2 === 0) {
  console.log('Exiting cleanly from PhantomJS!');
  phantom.exit();
} else {
  console.log('Exiting with an error status.');
  phantom.exit(1);
}

Given the preceding script, enter the following at the command line:

phantomjs chapter02/recipe05.js

If the script makes a...