Can't launch Selenium + PhantomJS/GhostDriver as Child processes

1k Views Asked by At

I've got a Node script that I want to use the child_process module with to get a Selenium server running with PhantomJS's GhostDriver.

I've required the module: Child = require "child_process"

And here's how I'm trying to start the server and attach GD to it (in Coffeescript):

@Selenium = new Child.exec "java -jar selenium/selenium-server-standalone-2.44.0.jar -role hub -port 4444", (error, stdout, stderr) =>
    console.log stdout
    console.log error if error
@PhantomJS = new Child.exec "phantomjs --webdriver=8080 --webdriver-selenium-grid-hub=http://127.0.0.1:4444", (error, stdout, stderr) =>
    console.log stdout
    console.log error if error

The stdout for @PhantomJS is this:

PhantomJS is launching GhostDriver...
[ERROR - 2014-12-10T18:51:27.587Z] GhostDriver - main.fail - {"message":"Could not start Ghost Driver","line":82,"sourceId":4469911104,"sourceURL":":/ghostdriver/main.js","stack":"Error: Could not start Ghost Driver\n    at :/ghostdriver/main.js:82","stackArray":[{"sourceURL":":/ghostdriver/main.js","line":82}]}

Additionally, I get this error from that command: {"killed": false, "code": 1, "signal": null}

Some notes:

  • The Selenium jar file is in fact located at selenium/selenium-server-standalone-2.44.0.jar
  • I've tried npm updateing just to see if that would make a difference
  • It occurred to me that something else might be running on port 4444, so I went ahead and ran "PORT_NUMBER=4444 | lsof -i tcp:${PORT_NUMBER} | awk 'NR!=1 {print $2}' | xargs kill" to no avail
  • I've tried installing PhantomJS from the source as per this suggestion to the same error
  • If I run these commands separately, outside of the script, it all works fine
1

There are 1 best solutions below

0
nelsonic On

In case anyone else has this issue, we solved it using daemon to run the child process in the background so that the terminal is free to run other commands/scripts.

Note: you will need to install the daemon module from NPM: npm install daemon --save-dev
(it has tests + decent usage stats and does what you need/expect)

Create a file called selenium_child_process.js and paste the following code:

console.log('Starting Selenium ...');
require('daemon')(); // this will run everything after this line in a daemon:
const exec = require('child_process').exec;
// note: your path to the selenium.jar may be different!
exec('java -jar ./bin/selenium.jar', (error, stdout, stderr) => {
  if (error) {
    console.error(`exec error: ${error}`);
    return;
  }
  if (stdout) {
    console.log(`> ${stdout}`); 
  }
  if (stderr) {
    console.log(`>> ${stderr}`); // handle errors in your preferred way.
  }
});

Then run the file with node selenium_child_process.js (in your terminal)

You now have selenium running as a child (background) process on TCP Port 4444.


If you want to shut down the Selenium server, you will need to kill the process. We use the following command:

lsof -n -iTCP:4444 -sTCP:LISTEN -n -l -P | grep 'LISTEN' | awk '{print $2}' | xargs kill -9

If you get stuck we're happy to help!