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

$ phantomjs --webdriver=8910


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.

package com.example.tests;

import java.util.regex.Pattern;
import java.util.concurrent.TimeUnit;
import org.junit.*;
import static org.junit.Assert.*;
import static org.hamcrest.CoreMatchers.*;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class SampleHeadlessTest {
  private WebDriver driver;
  private String baseUrl;
  private StringBuffer verificationErrors = new StringBuffer();
  protected static DesiredCapabilities dCaps;

  public void setUp() throws Exception {
	  dCaps = new DesiredCapabilities();
	  dCaps.setCapability("takesScreenshot", false);
	  driver = new PhantomJSDriver(dCaps);
	  baseUrl = "";
	  driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

  public void getLinksOfAssertSelenium() throws Exception {
    driver.get(baseUrl + "/");
    //Getting all the links present in the page by a HTML tag.
    java.util.List  links = driver.findElements(By.tagName("a"));
    //Printing the size, will print the no of links present in the page.
    System.out.println("Total Links present is "+links.size());
    //Printing the links in the page, we get through the href attribute.
    for(int i = 0; i<links.size();i++){
    	System.out.println("Links are listed "+links.get(i).getAttribute("href"));

  public void tearDown() throws Exception {
    String verificationErrorString = verificationErrors.toString();
    if (!&quot;&quot;.equals(verificationErrorString)) {


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.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: