Selenium headless browser testing

A headless browser is a web browser without Graphical User Interface (GUI). It accesses and renders web pages but doesn't show them to any human being. A headless browser should be able to parse JavaScript. Currently, most of the systems encourage tests against headless browsers due to their efficiency and time-saving properties. PhantomJS and HTMLUnit are the most commonly-used headless browsers. Capybara-webkit is another efficient headless WebKit for rails-based applications.

PhantomJS

PhantomJS is a headless WebKit scriptable with the JavaScript API. It is generally used for the headless testing of web applications that come with GhostDriver built-in. Tests on PhantomJS are obviously fast since it has fast and native support for various web standards, such as DOM handling, CSS selector, JSON, canvas, and SVG. In general, WebKit is a layout engine that allows web browsers to render web pages. Some browsers, such as Safari and Chrome, use WebKit.

Apparently, PhantomJS is not a test framework, it is a headless browser that is used only to launch tests via a suitable test runner called GhostDriver. GhostDriver is a JS implementation of the WebDriver Wire Protocol for PhantomJS; WebDriver Wire Protocol is a standard API that communicates with the browser. By default, GhostDriver is embedded with PhantomJS.

Tip

To download PhantomJS, refer to http://phantomjs.org/download.html.

Download PhantomJS, extract the zipped file (for example, phantomjs-1.x.x-windows.zip for Windows) and locate the phantomjs.exe folder. Add the following imports to your test code:

import org.openqa.selenium.phantomjs.PhantomJSDriver;
import org.openqa.selenium.phantomjs.PhantomJSDriverService;
import org.openqa.selenium.remote.DesiredCapabilities;

Introduce PhantomJSDriver using capabilities to enable or disable JavaScript or to locate the phantomjs executable file path:

DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability("takesScreenshot", true);
caps.setJavascriptEnabled(true); // not really needed; JS is enabled by default
caps.setCapability(PhantomJSDriverService.PHANTOMJS_EXECUTABLE_PATH_PROPERTY, "C:/phantomjs.exe");
WebDriver driver = new PhantomJSDriver(caps);

Alternatively, PhantomJSDriver can also be initialized, as follows:

System.setProperty("phantomjs.binary.path", "/phantomjs.exe");
WebDriver driver = new PhantomJSDriver();

PhantomJS supports screen capture as well. Since PhantomJS is a WebKit and a real layout and rendering engine, it is feasible to capture a web page as a screenshot. It can be set as follows:

caps.setCapability("takesScreenshot", true);

The following is the test snippet to capture a screenshot on a test run:

File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("c:\sample.jpeg"),true);

For example, check the following test program for more details:

package packagename;

import java.io.File;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.*;
import org.openqa.selenium.phantomjs.PhantomJSDriver;

public class phantomjs {
  private WebDriver driver;
  private String baseUrl;

  @BeforeTest
  public void setUp() throws Exception {
    System.setProperty("phantomjs.binary.path", "/phantomjs.exe");
    driver = new PhantomJSDriver();
    baseUrl = "https://www.google.co.in";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void headlesstest() throws Exception {
  driver.get(baseUrl + "/");
  driver.findElement(By.name("q")).sendKeys("selenium essentials");
  File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
  FileUtils.copyFile(scrFile, new File("c:\screen_shot.jpeg"), true);
  }

  @AfterTest
  public void tearDown() throws Exception {
    driver.quit();
  }
}

HTMLUnitDriver

HTMLUnit is a headless (GUI-less) browser written in Java and is typically used for testing. HTMLUnitDriver, which is based on HTMLUnit, is the fastest and most lightweight implementation of WebDriver. It runs tests using a plain HTTP request, which is quicker than launching a browser and executes tests way faster than other drivers. The HTMLUnitDriver is added to the latest Selenium servers (2.35 or above).

The JavaScript engine used by HTMLUnit (Rhino) is unique and different from any other popular browsers available on the market. HTMLUnitDriver supports JavaScript and is platform independent. By default, JavaScript support for HTMLUnitDriver is disabled. Enabling JavaScript in HTMLUnitDriver slows down test execution, but it is advised to enable JavaScript support because most modern sites are Ajax-based web apps. Enabling JavaScript also throws a number of JavaScript warning messages in the console during test execution. The following snippet lets you enable JavaScript for HTMLUnitDriver:

HtmlUnitDriver driver = new HtmlUnitDriver();
driver.setJavascriptEnabled(true); // enable JavaScript

The following line of code is an alternate way to enable JavaScript:

HtmlUnitDriver driver = new HtmlUnitDriver(true);

The following piece of code lets you handle a transparent proxy using HTMLUnitDriver:

HtmlUnitDriver driver = new HtmlUnitDriver();
driver.setProxy("xxx.xxx.xxx.xxx", port);  // set proxy for handling Transparent Proxy
driver.setJavascriptEnabled(true);  // enable JavaScript [this emulate IE's js by default]

HTMLUnitDriver can emulate a popular browser's JavaScript in a better way. By default, HTMLUnitDriver emulates IE's JavaScript. To handle the Firefox web browser with version 17, use the following snippet:

HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_17);
driver.setJavascriptEnabled(true);
Here is the snippet to emulate a specific browser's JavaScript using capabilities:
DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();
driver = new HtmlUnitDriver(capabilities);
DesiredCapabilities capabilities = DesiredCapabilities.firefox();
capabilities.setBrowserName("Mozilla/5.0 (X11; Linux x86_64; rv:24.0) Gecko/20100101 Firefox/24.0");
capabilities.setVersion("24.0");
driver = new HtmlUnitDriver(capabilities);
..................Content has been hidden....................

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