All IT Courses 50% Off
Selenium Tutorials

Selenium Headless Browser Testing

HTMLUnitDriver, Chrome, & Firefox

What is Headless Browser?

A headless browser testing is a browser simulation program that does not have a graphic user interface (UI less). The programs of the Headless browser will operate like any other browser but do not display any UI. Selenium executes its tests in the background.

There are several headless browsers available in the current market the following are the most popular ones.

  • Ghost
  • HtmlUnit
  • Phantom JS
  • Watir-webdriver
  • ZombieJS

What is headless testing?

Executing the web applications UI tests without opening a browsers user interface is called headless browser testing. A Headless browser will similarly act like a normal web browser. Testers will have full control over the web pages loaded into the headless browsers the only difference is you will not see a graphical user interface.

When to use headless browser testing?

We use headless testing once the cross-browser testing is completed and want to run regression test cases in subsequent releases and with continuous integration. It is recommended to use a headless browser when tests are executed in parallel as User Interface based browsers and consumes a lot of memory or resources. 

HTMLUnitDriver

By importing the HtmlUnitDriver class in selenium the Headless browser can be achieved. A Headless browser is used to perform a functional test, load test, and regression test as it is the fastest and lightweight of WebDriver API. These programs just behave like a browser but do not show any GUI.

Advantages of HtmlUnitDriver

  1. It is Lightweight and fastest to implement
  2. It is used to perform tests such as functional tests, load tests, regression tests, and sanity tests in the server without installing the browsers.
  3. It can easily run test cases on multiple browser versions.
  4. It can access the content of the web pages quickly without having to load them.

 Disadvantages of HtmlUnitDriver

  1. Even though they support browser features like (cookies, HTML parsing) however they do not provide DOM elements of JavaScript. It uses the Rhino JavaScript engine.
  2. Since the Headless browser uses JavaScript engine, which Java Script slightly configured is different than the W3C standard. However, we can run the test cases with JavaScript or without JavaScript with an option.

Steps to Use HTMLUnit Driver with Selenium

Step 1: Launch the Eclipse and copy the below code and add the standard selenium library files to the project.

package htmlunitdriver;

import org.openqa.selenium.By;
//import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;


public class htmlUnitTest {

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 element = driver.findElement(By.name("q"));
    element.sendKeys("Selenium Framework");
    element.submit();
 
    System.out.println("Title of the web page  is " + driver.getTitle());
}

}

Step 2: Run the above code. You will observe that no browser is launched and results are shown in the console.

 Enabling JavaScript

Java Script can be enabled in two ways:

1. By passing JavaScript enable flag to HtmlUnitDriver class as a constructor

HtmlUnitDriver driver = new HtmlUnitDriver(true);

2. By using setJavaScriptEnabled method

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

import org.openqa.selenium.By;
//import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;


public class htmlUnitTest {

public static void main(String[] args) {
    HtmlUnitDriver driver = new HtmlUnitDriver();
    driver.setJavascriptEnabled(true);
    driver.get("http://www.google.com");
     
    System.out.println("The Title of the web page is" + driver.getTitle());
     
    WebElement java = driver.findElement(By.name("q"));
    java.sendKeys("Selenium Automation");
    java.submit();
 
    System.out.println("The Title of the web page is " + driver.getTitle());
}


}

Enabling different types of Browsers and versions.

Types of Browsers can be added by passing BrowserVersion as constructor HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_45);

package htmlunitdriver;

import org.openqa.selenium.By;
//import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import com.gargoylesoftware.htmlunit.BrowserVersion;



public class htmlUnitTest {

public static void main(String[] args) {
        HtmlUnitDriver driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_45);
        driver.get("http://www.google.com");
         
        System.out.println("The Title of the page is" + driver.getTitle());
         
        WebElement java = driver.findElement(By.name("q"));
        java.sendKeys("Selenium Automation Framework");
        java.submit();
     
        System.out.println("The Title of the web page is " + driver.getTitle());
 
}



}

Headless Chrome in Selenium

Step1: Create a Package (headless)


Step2: Create a Class(HeadLessChrome)

package headless;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class HeadLessChrome {
public static void main(String[] args) {

System.setProperty(“webdriver.chrome.driver”,”C:/Drivers/chromedriver_win32/
chromedriver.exe”);
ChromeOptions options = new ChromeOptions();
options.setHeadless(true);
//options.addArguments(“—headless”);
WebDriver driver = new ChromeDriver(options);
driver.get(“https://demo.nopcommerce.com/”);
System.out.println(“Title of the page:”+driver.getTitle());

}
}

When you run the above program, it will print the Title of the webpage.

Headless Firefox in Selenium

Step1: Create a Package (headless)

Step2: Create a Class(HeadLessFirefox)

package headless;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.FirefoxDriver;
import org.openqa.selenium.chrome.FirefoxOptions;

public class HeadLessFirefox {
public static void main(String[] args) {

System.setProperty(“webdriver.gecko.driver”,”C:/Drivers/geckodriver-v0.23.0-win64/
geckodriver.exe”);
FirefoxOptions options = new FirefoxOptions();
options.setHeadless(true);
WebDriver driver = new FirefoxDriver(options);
driver.get(“https://demo.nopcommerce.com/”);
System.out.println(“Title of the page:”+driver.getTitle());

}
}

When you run the above program, it will print the Title of the webpage.

Conclusion:

To rapidly test the application in various browsers and without any interruption, headless browser testing is used. HTML unit driver, Chrome, and Firefox are popular for headless browser testing due to its speed, accuracy, y and easy to access features.

Facebook Comments
Tags

Related Articles

Back to top button
Close
Close

Get Selenium Training Online
Course Now at 50%OFF!

Offer valid for 1st 20 seats only, Hurry up!!

You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

H2kinfosys will use this information to be in touch with you for providing updates.