automatictester

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 org.hamcrest.CoreMatchers.is;
    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']");
    
        @BeforeClass
        private void initialize() {
            driver = new PhantomJSDriver();
            driver.navigate().to("http://www.seleniumhq.org/");
        }
    
        @Test
        public void verifySomething() {
            driver.findElement(ABOUT_TAB).click();
            assertThat(driver.findElement(ROADMAP_LINK).isDisplayed(), is(true));
            assertThat(driver.findElements(HELP_LINK).isEmpty(), is(true));
        }
    
        @AfterClass
        private void quit() {
            driver.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();
          capabilities.setBrowserName("phantomjs");
    
          driver = new RemoteWebDriver(hubUrl, capabilities);
          driver.navigate().to("http://www.seleniumhq.org/");
       }
  • 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=http://127.0.0.1:4444

Test is ready to be executed now.

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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: