my thoughts on test automation

Selenium WebDriver, Grid 2, PhantomJS and Ghost Driver – tutorial

Selenium WebDriver lets you execute your tests in large number of browsers. With PhantomJS and Ghost Driver you can even execute them in headless mode! PhantomJS is a headless WebKit based browser, which you can interact with using JavaScript. Ghost Driver is a PhantomJS-backed JavaScript implemetation of WebDriver. Ghost Driver has been integrated with PhantomJS since PhantomJS 1.8 release. In this post I will show you how to switch from running your Selenium WebDriver tests in a standard browser to headless mode.

Selenium WebDriver

We will use test and SBT project from previous post and modify it to use PhantomJSDriver.

  • Download PhantomJS binary and make sure it is in your PATH
  • Assuming you use SBT as a build tool, add Ghost Driver library to your build.sbt file:
    "com.github.detro.ghostdriver" % "phantomjsdriver" % "1.1.0"
  • Create sample test class:
    package com.wordpress.automatictester.examples;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.phantomjs.PhantomJSDriver;
    import org.testng.annotations.AfterClass;
    import org.testng.annotations.BeforeClass;
    import org.testng.annotations.Test;
    import static;
    import static org.hamcrest.MatcherAssert.assertThat;
    public class PhantomJSTest {
        private WebDriver driver;
        private static final By ABOUT_TAB = By.linkText("About");
        private static final By ROADMAP_LINK = By.xpath("//a[text() = 'Roadmap']");
        private static final By HELP_LINK = By.xpath("//a[text() = 'Help']");
        private void initialize() {
            driver = new PhantomJSDriver();
        public void verifySomething() {
            assertThat(driver.findElement(ROADMAP_LINK).isDisplayed(), is(true));
            assertThat(driver.findElements(HELP_LINK).isEmpty(), is(true));
        private void quit() {

It’s done!

Selenium Grid 2

For this demo, we will use Selenium Grid 2 with both hub and PhantomJS node running on local machine. You can read more about Selenium Grid 2 in that post.

  • Modify above @BeforeClass method to:
       private void initialize() throws MalformedURLException {
          URL hubUrl = new URL("http://localhost:4444/wd/hub");
          DesiredCapabilities capabilities = new DesiredCapabilities();
          driver = new RemoteWebDriver(hubUrl, capabilities);
  • Download Selenium standalone JAR if you don’t have one
  • Start hub:
    java -jar selenium-server-standalone-2.42.2.jar -role hub
  • Start node:
    phantomjs --webdriver=8080 --webdriver-selenium-grid-hub=

Test is ready to be executed now.


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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: