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.

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 (!"".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.

10 thoughts on “Getting Started with GhostDriver & PhantomJs

  1. Anonymous
    September 6, 2013 at 10:09 pm

    Cannot resolve links.get(i).getAttribute(“href”)

    1. linkfy
      February 9, 2014 at 5:05 pm

      Ups, part of the code disappears, add this after “List”


      Without “”

    2. November 7, 2014 at 8:22 am

      change line 39 to
      List links = driver.findElements(By.tagName(“a”));
      then it‘ll be ok

  2. Ravi Prakash
    October 24, 2013 at 4:52 am

    Not able to navigate to any website having domain as “edu”(e.g – Can someone please provide the reason why it is not navigating to educational domain website.

  3. adam d
    December 10, 2013 at 7:24 pm

    Im trying to get a screenshot on failure to work. Ive added dCaps.setCapability(“takesScreenshot”, true); to my scripts but Im not sure if i have all of the code that I need. Is there something im missing? I dont see a screenshot in any of my directories.


  4. mitul
    May 16, 2014 at 6:20 am

    I am getting
    May 15, 2014 11:15:50 PM org.openqa.selenium.phantomjs.PhantomJSDriverService
    INFO: executable: /usr/local/bin/phantomjs
    May 15, 2014 11:15:50 PM org.openqa.selenium.phantomjs.PhantomJSDriverService
    INFO: port: 13455
    May 15, 2014 11:15:50 PM org.openqa.selenium.phantomjs.PhantomJSDriverService
    INFO: arguments: [--webdriver=13455, --webdriver-logfile=/Users/#####/Desktop/Old Files/cfl-automation/phantomjsdriver.log]
    May 15, 2014 11:15:50 PM org.openqa.selenium.phantomjs.PhantomJSDriverService
    INFO: environment: {}
    env: node: No such file or directory
    Exception in thread “main” org.openqa.selenium.remote.UnreachableBrowserException: Could not start a new session. Possible causes are invalid address of the remote server or browser start-up failure.

    Any help will be appreciated.

  5. November 11, 2014 at 8:30 pm

    The error Cannot resolve links.get(i).getAttribute(“href”) will be resolved by below code
    driver.get(baseUrl + “/”);
    List elementList = driver.findElements(By.tagName(“a”));
    System.out.println(“Total Links present is ” + elementList.size());
    for (int i = 0; i < elementList.size(); i++) {
    System.out.println("Links are listed"
    + elementList.get(i).getAttribute("href"));


  6. December 21, 2014 at 9:40 pm

    It’s actually a nice aand helpful piece off info. I am happy that you simply shared this helpful informtion with us.
    Please stay us uup to date like this. Thanks for sharing.

Leave a Reply

Your email address will not be published. Required fields are marked *