Skip to content

Instantly share code, notes, and snippets.

@clochix
Created October 8, 2013 09:37
Show Gist options
  • Save clochix/6882184 to your computer and use it in GitHub Desktop.
Save clochix/6882184 to your computer and use it in GitHub Desktop.
Sample code to call a shell script from CasperJS
// (…)
var childProcess;
try {
childProcess = require("child_process");
} catch (e) {
this.log(e, "error");
}
if (childProcess) {
childProcess.execFile("/bin/bash", ["mycommand.sh", args1, args2, args3], null, function (err, stdout, stderr) {
this.log("execFileSTDOUT:", JSON.stringify(stdout), 'debug');
this.log("execFileSTDERR:", JSON.stringify(stderr), 'debug');
});
this.log("Done", "debug");
} else {
this.log("Unable to require child process", "warning");
}
@ag10
Copy link

ag10 commented Jun 17, 2015

What is the equivalent for slimerjs? I get the following error when I run the same script that passes with phantomjs under slimerjs.

FAIL Error: Module: Can not resolve "child_process" module required by main located at file:///usr/local/lib/node_modules/casperjs/bin/bootstrap.js
type: uncaughtError
file: functionalTestsWIP.js
error: {}
stack: not provided

@anklos
Copy link

anklos commented Oct 13, 2015

any chance you know how to get the standout in casper.then?

Sample code:

casper.then(function(){
   childProcess.execFile("/bin/bash", ["mycommand.sh", args1, args2, args3], null, function (err, stdout, stderr)       {
      this.log("execFileSTDOUT:", JSON.stringify(stdout), 'debug');
      this.log("execFileSTDERR:", JSON.stringify(stderr), 'debug');
    });
});

casper.then(function(){});

I cannot get any stdout or stderr from the callback by running a bash script. but if I do simple commands like ls -l /usr, i can get the output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment