Skip to content

PhantomJS Headless Browser Automation

Daniel Gempesaw edited this page Jan 23, 2014 · 5 revisions

Setup

Assuming you already have PhantomJS, the driver for PhantomJS, GhostDriver, comes built in: if you have PhantomJS, you have GhostDriver.

To run your existing Selenium::Remote::Driver test suite against PhantomJS, you have a few options:

  1. If you want to use the binary path as an option to the standalone jar, use phantomjs.binary.path. This is analogous to the steps for Chromedriver and IEDriver; you can even include all three options at once to have a server with access to all of the browsers.

    $ java -jar selenium-server-standalone-2.35.0.jar -Dphantomjs.binary.path=/usr/local/bin/phantomjs
  2. You can fire up PhantomJS in webdriver mode:

    $ phantomjs --webdriver=4444

    The Selenium standalone server and the phantomjs binary cannot both listen on the same port; if you want to run them simultaneously, change one of them to run on a different port and use the port option accordingly when instantiating your S::R::D object.

Performing either of these will give you access to PhantomJS from your existing automation suite. When specifying your capabilities, use "phantomjs":

 my $driver =  Selenium::Remote::Driver->new("browser_name" => "phantomjs");

Caveats

Among other things, PhantomJS has known issues with some HTTPS/SSL pages, and it chokes on javascript alerts/prompts/confirmation dialogue boxes. These and other sticking points may arise when trying to run a test that was not intended for a headless browser.

As the option accept_ssl_certs from the Webdriver Protocol is not supported, you can achieve the same behaviour, by setting an option on the commandline.

$ phantomjs --ignore-ssl-errors=true --webdriver=4444