Selenium

Selenium Headless Browser Testing

Photo of Sarad DhungelSarad DhungelJanuary 13th, 2017Last Updated: April 9th, 2019
2 206 3 minutes read

Headless browser refers to running tests in browser like simulation without having to invoke a browser or without a GUI. In this example set, we will be using Headless browser to run test cases.

We will be also discussing about the Headless browser in detail, about its importance as well as the caveat to keep in mind while using it.
 
 
 
 
 
 
 

1.Introduction

Headless browser can be achieved my importing HtmlUnitDriver class in Selenium. Headless browser is used to perform load test, functional test as well as regression test as it is most light weight and fastest implementation of WebDriver API. These programs behave just like a browser but don’t show any GUI. Some of the headless browsers are NodeJS, PhantonJS,HtmlUnit etc.PhantomJS can also be implemented in Selenium by importingPhantomJS jar.

Advantages of HtmlUnitDriver :

  1. Lightweight and quickest to implement.
  2. Ideal to perform different tests such as load test, functional test, sanity test as well as regression test in server without having to install browsers.
  3. To run test cases on simulated multiple browser versions.
  4. To access different content of the web pages quickly without having to load them

Disadvantages of HtmlUnitDriver :

    1. Although they support common browser features like (HTML parsing, cookies) however they do not render DOM elements of JavaScript. It uses Rhino JavaScript engine.
    2. Since Headless Browser uses Java Script engine, which has Java Script configured slightly different than the W3C standard. However, there is an option to run test cases with JavaScript or without the Java Script option.

Testing.java

package javacodegeeks.seleniumHeadlessBrowser;import org.openqa.selenium.By;import org.openqa.selenium.WebElement;import org.openqa.selenium.htmlunit.HtmlUnitDriver;public class TestingwJS {public static void main (String args[]){HtmlUnitDriver driver = new HtmlUnitDriver();driver.setJavascriptEnabled(true);driver.get("http://www.google.com");System.out.println("Title of the page is" + driver.getTitle());WebElement java = driver.findElement(By.name("q"));java.sendKeys("Java Code Geeks");java.submit();System.out.println("Title of the page now is " + driver.getTitle());}}

2. Enabling JavaScript

Java Script can be enabled in two ways :

1. As a constructor by passing JavaScript enable flag toHtmlUnitDriver class

HtmlUnitDriver driver = new HtmlUnitDriver(true);

2. By usingsetJavaScriptEnabled method

HtmlUnitDriver driver = new HtmlUnitDriver(); Driver.setJavaScriptEnabled(true);

TestingwJS.java

package javacodegeeks.seleniumHeadlessBrowser;import org.openqa.selenium.By;import org.openqa.selenium.WebElement;import org.openqa.selenium.htmlunit.HtmlUnitDriver;public class TestingwJS {public static void main (String args[]){HtmlUnitDriver driver = new HtmlUnitDriver();driver.setJavascriptEnabled(true);driver.get("http://www.google.com");System.out.println("Title of the page is" + driver.getTitle());WebElement java = driver.findElement(By.name("q"));java.sendKeys("Java Code Geeks");java.submit();System.out.println("Title of the page now is " + driver.getTitle());}}

Enabling different types of Browsers and versions.

Types of Browsers can be added by passingBrowserVersion as constructor

HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_45);

TestingwBrowserversions.java

package javacodegeeks.seleniumHeadlessBrowser;import org.openqa.selenium.By;import org.openqa.selenium.WebElement;import org.openqa.selenium.htmlunit.HtmlUnitDriver;import com.gargoylesoftware.htmlunit.BrowserVersion;public class TestingwBrowserversions {public static void main(String[] args) {HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_45);driver.get("http://www.google.com");System.out.println("Title of the page is" + driver.getTitle());WebElement java = driver.findElement(By.name("q"));java.sendKeys("Java Code Geeks");java.submit();System.out.println("Title of the page now is " + driver.getTitle());}}

3.PhantomJS

PhantomJS is also a headless browser type which has JavaScript API features available. It provides an optimal solution for headless website testing as it comes with the standard DOM API.
In order to access PhantomJS with Selenium, we need to add PhantomJS in project directory like for chrome driver.

Want to master Selenium?
Subscribe to our newsletter and download theSeleniumProgramming Cookbookright now!
In order to get you prepared for your Selenium development needs, we have compiled numerous recipes to help you kick-start your projects. Besides reading them online you may download the eBook in PDF format!

Thank you!

We will contact you soon.

PhantonJs.java

package javacodegeeks.seleniumHeadlessBrowser;import java.io.File;import org.openqa.selenium.By;import org.openqa.selenium.WebDriver;import org.openqa.selenium.WebElement;import org.openqa.selenium.phantomjs.PhantomJSDriver;public class PhantomJs {public static void main(String [] args){File file = new File("/Users/saraddhungel/Downloads/phantomjs");System.setProperty("phantomjs.binary.path", file.getAbsolutePath());WebDriver driver = new PhantomJSDriver();driver.get("http://www.google.com");WebElement wbelement = driver.findElement(By.name("q"));wbelement.sendKeys("Java Code Geeks");wbelement.submit();System.out.println("Title of the page now is " + driver.getTitle());driver.quit();}}

4.Conclusion

We explore the concept of the headless browser in this tutorial. We also discussed about different headless browsers like HtmlUnitDrive , PhantomJs. The advantages and disadvantages of using them.

5. Download the Eclipse Project

This was an example of Selenium Headless Browser Testing

Download
You can download the source code of this example here:SeleniumHeadless Browser
Do you want to know how to develop your skillset to become aJava Rockstar?
Subscribe to our newsletter to start Rockingright now!
To get you started we give you our best selling eBooks forFREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to theTerms andPrivacy Policy

Thank you!

We will contact you soon.

Photo of Sarad DhungelSarad DhungelJanuary 13th, 2017Last Updated: April 9th, 2019
2 206 3 minutes read
Photo of Sarad Dhungel

Sarad Dhungel

I am a graduate in Computer Engineering from Howard University. Third place award winner in Intel-Cornell Cup. Passionate about new emerging software and technology. During free time, I enjoy reading about politics, business, spirituality, technology and traveling.
Subscribe
Notify of
guest
I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.

I agree to theTerms andPrivacy Policy
The comment form collects your name, email and content to allow us keep track of the comments placed on the website. Please read and accept our website Terms and Privacy Policy to post a comment.