Getting Started with GhostDriver & PhantomJs
RSSS

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.

ghostdriver

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]
phantomjsdriver

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;
import org.openqa.selenium.support.ui.Select;

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

  @Before
  public void setUp() throws Exception {
	  
	  dCaps = new DesiredCapabilities();
	  dCaps.setJavascriptEnabled(true);
	  dCaps.setCapability("takesScreenshot", false);
	  
	  driver = new PhantomJSDriver(dCaps);
	  baseUrl = "http://assertselenium.com/";
	  driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  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"));
    }
  }

  @After
  public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }

}

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

References:

Slides by Ivan De Marino
PhantomJs
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.

7 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. Ravi Prakash
    October 24, 2013 at 4:52 am

    Not able to navigate to any website having domain as “edu”(e.g – http://www.fms.edu). 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.

    Thanks,

  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.
    Thanks.

Leave a Reply

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