Running tests in headless mode with PhantomJS

PhantomJS is a headless Webkit-based browser. We can use PhantomJS in conjunction with Selenium WebDriver to run basic functional tests.

In this recipe, we will set up and use PhantomJS to run a simple test.

Getting ready

You will need PhantomJS binary for this example. You can download the appropriate binary from http://phantomjs.org/.

In this example, the Windows version of PhantomJS is used.

You will also need to add a dependency in the maven pom.xml file, as follows:

  <dependency>
      <groupId>com.github.detro</groupId>
      <artifactId>phantomjsdriver</artifactId>
      <version>1.2.0</version>
      <scope>test</scope>
    </dependency>

How to do it...

Let's create a new test PhantomjsTest that uses PhantomJS to run a test in headless mode, as shown in the following code example:

package com.secookbook.examples.chapter13;

import static org.junit.Assert.*;
import static org.testng.Assert.assertEquals;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;

public class PhantomjsTest {

    WebDriver driver;

    @Before
    public void setUp() throws Exception {
        DesiredCapabilities caps = DesiredCapabilities.phantomjs();
        caps.setJavascriptEnabled(true);
        caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,
                "./src/test/resources/drivers/phantomjs.exe");
        driver = new PhantomJSDriver(caps);
        driver.get("http://bit.ly/1zdNrFZ");
    }
    @After
    public void tearDown() throws Exception {
        driver.quit();
    }

    @Test
    public void testBmiCalc() {
        WebElement height = driver.findElement(By.name("heightCMS"));
        height.sendKeys("181");

        WebElement weight = driver.findElement(By.name("weightKg"));
        weight.sendKeys("80");

        WebElement calculateButton = driver.findElement(By.id("Calculate"));
        calculateButton.click();

        WebElement bmi = driver.findElement(By.name("bmi"));
        assertEquals(bmi.getAttribute("value"), "24.4");

        WebElement bmiCategory = driver.findElement(By.name("bmiCategory"));
        assertEquals(bmi_category.getAttribute("value"), "Normal");
    }
}

How it works...

When tests are run with PhantomJS, you will not see graphical browser opening and actions being performed. You will see the execution log on the console, that looks as shown in the following screenshot:

How it works...

Using PhantomJS requires a new instance of the PhantomJSDriver class, as shown in the following code snippet:

driver = new PhantomJSDriver(caps);

We need to pass the path of PhantomJS executable using DesiredCapabilities through following way:

DesiredCapabilities caps = DesiredCapabilities.phantomjs();
caps.setJavascriptEnabled(true);
caps.setCapability(PhantomJSDriverServ-ice.PHANTOMJS_EXECUTABLE_PATH_PROPERTY,"./src/test/resources/drivers/phantomjs.exe");

There's more...

We can also run PhantomJS as a Selenium node and connect to the Hub using PhantomJS binary, using following command line:

phantomjs --webdriver=9090 --webdriver-selenium-grid-hub=http://localhost:4444

This will register a new node to Selenium Grid. You can run tests with the following capabilities using RemoteWebDriver:

DesiredCapabilities caps = DesiredCapabilities.phantomjs();
caps.setJavascriptEnabled(true);
driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), caps);
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.183.210