Getting Started with GhostDriver & PhantomJs

Headless Testing with GhostDriver and PhantomJs

Headless Testing

One major use case of PhantomJS is headless testing of web applications. It is suitable for general command-line based testing, as well as with the CI system.

Bringing you here the WebDriver Implementation of PhantomJs Headless Testing.

PhantomJS itself is not a test framework, it is only used to launch the tests via a suitable test runner. Here is a list of different test framework and test runner.

We will see a PhantomJs and GhostDriver combination. GhostDriver is a pure JavaScript implementation of the WebDriver Wire Protocol for PhantomJS. It’s a Remote WebDriver that uses PhantomJS as back-end. So, Ghostdriver is the bridge between the Selenium WebDriver with Phantom JS.

WebDriver Implementation for PhantomJs == GhostDriver

WebDriver bindings == PhantomJsDriver

Architecture Implementation of GhostDriver

Below is a diagrammatic representation of GhostDriver implementation.


Ghost Driver is a pure JavaScript implementation of the WebDriver Wire Protocol for PhantomJS. It’s a Remote WebDriver that uses PhantomJS as back-end.

How to use it ?

Step 1:

    Download PhantomJs as per your OS.

I am using Mac and hence this blog post will follow Setting up PhantomJs with Selenium WebDriver

Download and extract the package. or windows its an exe file and for Mac its just a zip package you will need to extract and SET the phantomjs in the PATH.

You will need to setup the Phantomjs executable that is in the bin folder to some PATH.

e.g: /usr/bin

Step 2:

Starting the PhantomJs in a Remote WebDriver mode

[sourcecode lanuguage=”unix”]$ phantomjs –webdriver=8910[/sourcecode]

PhantomJs will launch the GhostDriver…

We are done with the Setup, now we can use any RemoteWebDriver implementation to send commands to it.

PhantomJSDriver implementation.

Step 3: Setup the Selenium Project in Eclipse as documented here or via Maven as documented here

PhantomJsDriver Implementations expects a value to be passed and we will pass the Desired capabilities Instance.

Below is the Sample Code, which shows us the PhantomJsDriver implementation.

Here is the output for the following code. Shows the output after running the Script Headless mode.


Slides by Ivan De Marino
JavaDocs of PhantomJsDriver

Hope this blog post helps people who wants to get started with the GhostDriver and phantomJs Headless Testing using WebDriver.

A special Thanks to Ivan De Marino and Ariya Hidayat for creating this wonderful projects.

2 thoughts on “Getting Started with GhostDriver & PhantomJs

  1. Hi,

    Can I know how to run it on Windows. Below is my code written but still It is throwing error
    System.setProperty( “phantomjs.binary.path”, System.getProperty(“user.dir”)+”\\phantomjs-2.1.1-windows\\bin\\phantomjs.exe” );
    DesiredCapabilities capabilities = DesiredCapabilities.phantomjs();
    driver = new PhantomJSDriver(capabilities);

  2. Hi,
    you can run with the next code:
    File path=new File(“.//src/test/resources/phantomjs/bin/phantomjs.exe”);

    String[] cli_args = new String[] {“–ignore-ssl-errors=true”, “–debug=false”};

    DesiredCapabilities capabilities = new DesiredCapabilities();
    capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, System.getProperty(“user.dir”) + “.//src/test/resources/phantomjs/bin/phantomjs.exe”);
    capabilities.setCapability(“Platform”, Platform.ANY);
    capabilities.setCapability(PhantomJSDriverService.PHANTOMJS_CLI_ARGS, cli_args);
    driver = new PhantomJSDriver(capabilities);

    ((JavascriptExecutor) driver).executeAsyncScript(
    “window.setTimeout(arguments[arguments.length – 1], 30000);”);
    driver.manage().window().setSize(new Dimension(1280, 1024));

Leave a Comment