SAM SAM - 2 months ago 24
Linux Question

CasperJs doesn't execute completely from PHP

Sample CasperJs test:

casper.test.begin('Hello, Test!', 1, function(test) {
test.assert(true);
test.done();
});


Result of Linux command execution:

# casperjs test /bin/casper-test.js
Test file: /bin/casper-test.js
# Hello, Test!
PASS Subject is strictly true
PASS Hello, Test! (1 test)
PASS 1 test executed in 0.026s, 1 passed, 0 failed, 0 dubious, 0 skipped.


Result of PHP:

<?php
echo exec('casperjs test /path/to/casper-test.js');
?>
[37;42;1mPASS 1 test executed in 0.024s, 1 passed, 0 failed, 0 dubious, 0 skipped. [0m


As you see it only shows the last line of Linux command (correct) result.

Any ideas?

Answer

Check out the documentation for exec:

http://php.net/manual/en/function.exec.php

string exec ( string $command [, array &$output [, int &$return_var ]] )

To capture all the output, you should be able to do something like this:

exec('casperjs test /path/to/casper-test.js 2>&1', $output, $return_var);
var_dump($output);
var_dump($return_var);

Appending 2>&1 to your command will redirect the STDERR and STDOUT and capture it in the $output variable.

Also, I noticed the output appears to be trying to colorize it for output on the shell, that's what the [37;42; string is. According to the docs, it looks like you can add --no-colors to your command to disable the colorized output:

casperjs test --no-colors  /path/to/casper-test.js 2>&1