官术网_书友最值得收藏!

  • PhantomJS Cookbook
  • Rob Friesel
  • 650字
  • 2021-07-16 11:37:57

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 clean exit, our output should look like the following:

Running the PhantomJS exit demo...
Exiting cleanly from PhantomJS!

If the script exits with an error, our output should look like the following:

Running the PhantomJS exit demo...
Exiting with an error status.

We can also verify this from the command line on Linux and OS X as follows:

echo $?

On Windows, we can verify it as follows:

echo %ERRORLEVEL%

We will see a 0 or a 1, depending on whether the script exited successfully or with an error, respectively.

How it works…

Though a trivial example, our example script works as follows:

  1. We print our introductory message and then test whether a random number is even or odd using a % 2 calculation.
  2. If we have an even number, we make a clean exit from PhantomJS by calling phantom.exit.
  3. If we have an odd number, we exit from PhantomJS with an error by calling phantom.exit and passing it a non-zero integer.

The phantom.exit method is our only way to gracefully exit a script in PhantomJS. It takes an optional integer as its only parameter, and this integer is the exit status that will be returned to the shell session that initiated PhantomJS. If we do not pass any argument to phantom.exit, then it will assume we are exiting successfully and will return a 0.

There's more…

Controlling the PhantomJS exit status is an important component of integrating the application into many workflows. By exposing the ability to control the overall program exit status through the phantom.exit API, our JavaScript scripts become first-class citizens on the command line.

Type coercion with phantom.exit

Another interesting point to note about phantom.exit is that although its sole parameter expects an integer, it exhibits some "typical JavaScript" coercive behavior with non-integer arguments. For example, it effectively performs Math.round on floats, as follows:

phantom.exit(1.1);
// exits as 1
phantom.exit(1.9);
// exits as 2

Passing a string to phantom.exit will effectively cast the value to a number using the Number constructor on that argument before falling back to its previously stated rounding rules, as shown in the following code snippet:

phantom.exit('1');
// exits as 1
phantom.exit('1.5');
// exits as 2
phantom.exit('one');
// exits as 0

Note that strings that cannot be parsed into numbers are discarded, and the call to phantom.exit is treated as though no arguments were passed.

As a final curiosity, the casting behavior with phantom.exit extends to Boolean values as well. Consistent with JavaScript's rules for "truthy" and "falsy" values, Number casts true and false to 1 and 0, respectively. Though this makes sense in JavaScript's larger context, it may also seem somewhat counterintuitive when used with phantom.exit as follows:

phantom.exit(true);
// exits as 1 -- interpreted as an error
phantom.exit(false);
// exits as 0 -- interpreted as a success

Generally speaking, although we can pass these non-integer values to phantom.exit, we should only pass integers or call the method with no arguments.

主站蜘蛛池模板: 罗城| 靖边县| 沂水县| 行唐县| 准格尔旗| 苏尼特右旗| 曲阜市| 乐陵市| 永年县| 民丰县| 大关县| 淮南市| 杨浦区| 奉贤区| 施秉县| 修水县| 天津市| 德阳市| 阿克苏市| 高阳县| 靖江市| 诸城市| 和田县| 固始县| 安岳县| 玉田县| 勃利县| 双牌县| 鄂伦春自治旗| 财经| 临澧县| 巴楚县| 康马县| 伊通| 乌兰察布市| 建宁县| 会同县| 灵川县| 兴义市| 南澳县| 安福县|