Skip to content

Instantly share code, notes, and snippets.

@clochix
Created October 8, 2013 09:37
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • 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");
}
@mickaelandrieu
Copy link

Maybe we can add this in the Casper API ? It would be useful !

https://code.google.com/p/phantomjs/source/browse/examples/child_process-examples.js?r=f52044cd3106266aa8efc1fae05e8c923315a5e5

What's the difference between spawn & execFile ?

@n1k0
Copy link

n1k0 commented Oct 11, 2013

@mickaelandrieu what kind of API would you like to add to CasperJS? I'm finding the PhantomJS one good enough.

@mickaelandrieu
Copy link

https://github.com/ariya/phantomjs/blob/master/src/modules/child_process.js she's not documented :/ But you're right we don't need more thant Phantomjs'one

@hiteshranaut
Copy link

thanks for the script . my casperjs file is located in c://wamp/www what i want is to execute
"myexefile.exe -l username -p password -c myfile.txt [-t 10000]"
but there is no doc available or example . i just want to execute the above command and store the responce in var myvalue in casperjs plese help

@jeff-h
Copy link

jeff-h commented Aug 5, 2014

I don't really understand how to incorporate the provided code into a Jasper test (using Jasper's promises, I guess). Would you mind providing an example wrapped inside a casper.then()?

@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