Does Selenium support headless browser testing?


Yes, Selenium supports headless browser testing. This can be done with the help of the HTMLUnitDriver. It is the fastest webdriver among other browser drivers and is platform independent.

After Selenium 2.53 version, HTMLUnitDriver jar has to be added explicitly within the project. To add the dependency, follow the steps as listed below −

  • Right-click on the project and choose Build path. Then click on Configure Build Path.

  • Go to Java Build Path, then select Libraries. Click on Add External JARs. Then browse and add the HTMLUnitDriver jar.

  • We have to add org.openqa.selenium.htmlunit.HtmlUnitDriver to the project.

Example

Code Implementation.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.htmlunit.HtmlUnitDriver;
public class HeadlessMode{
   public static void main(String[] args) {
      //HtmlUnitDriver initialization
      HtmlUnitDriver driver = new HtmlUnitDriver();
      driver.get("https://www.tutorialspoint.com/questions/index.php");
      // wait of 5 seconds
      driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
      System.out.println("Page title: " + driver.getTitle());
   }
}

Output

Also, there will be no browser displayed as execution goes on.

Updated on: 28-Dec-2020

238 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements