Headless Browser Testing [ PhantomJS ] – Selenium WebDriver

Headless Browser Testing Using Selenium: As we know that Selenium is popular for the web browser automated testing. Selenium WebDriver supports all the different major browsers but sometimes you don’t want to test on the real browser, that time Headless helps you in testing the application.

What is a Headless Browser Testing?

Some browsers are available which doesn’t have any GUI (Graphical user interface), such browsers are called Headless browser. Some of the famous headless browsers are HtmlUnit and the NodeJs headless browsers. Similarly, there are few other headless browsers are available.

Advantages of Headless Browser

  • if you are using a headless browser then you can able to run your tests at any time because the browser is not displaying over the screen.
  • It allows you to continue your work while the automation script is running.
  • There is less chance of errors because of human intervention.
  • If you test an application using a Headless browser then the test will run faster as compare to GUI testing because the headless browser doesn’t need to wait for the complete page to finish rendering before performing any action.

Disadvantages of Headless Browser

  • When you run your scripts on a headless browser, you are not able to feel the real user experience.
  • Cosmetic bugs (Which bugs are found in the GUI of the application) are can not be identified while you testing through a headless browser for example location of a button and color of a web element.

Selenium support for Headless browser

In Selenium you can test an application in headless testing with the help of using HtmlUnitDriver. This HtmlUnitDriver class uses the HtmlUnit headless browser. To test the application you need to create an HtmlUnitWebDriver. Once you have the HtmlUnitWebDriver, you can use it as a regular WebDriver. Let’s see with an example:

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());
   }
}

Output:

The Title Of the Page is:- Google

When you are using HtmlUnitDriver it uses the Rhino JavaScript engine but when you run your script with other browsers those are using different JavaScript engine. Hence if you compare the results of HtmlUnitDriver with other browser results then the result may vary. In HtmlUnitDriver the javascript is disabled but we can enable the javascript like below1 ways:

Enable JavaScript In Headless Browser

  • We can enable the JavaScript at the time of initializing the HtmlUnitDriver, like below
HtmlUnitDriver driver = new HtmlUnitDriver(true);
  • We can also enable by Setting the ‘setJavascriptEnabled’ to true
HtmlUnitDriver driver = new HtmlUnitDriver();
driver.setJavascriptEnabled(true);

How you can test in Different Browser Versions using HtmlUnitDriver

HtmlUnitDriver allows you to test your script in different browser versions. you can select the different browser with the help of the below codes

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());
   }
}

If you want to know the browser version also then HtmlUnitDriver provider a method getBrowserVersion(), by using this we can also get the browser version on which our automation script is running.

package com.selenium.practice.basic;

import org.openqa.selenium.htmlunit.HtmlUnitDriver;
import com.gargoylesoftware.htmlunit.BrowserVersion;

public class HeadlessBrowser_Testing_example1 
{
   public static void main(String[] args) 
   {
      String title;
      
      HtmlUnitDriver driver=new HtmlUnitDriver(BrowserVersion.FIREFOX_52);
      driver.get("https://www.google.com");
      
      title=driver.getTitle();
      System.out.println("The Title Of the Page is:- "+title);
      System.out.println("The Scripts Executed on Browser Version: "+driver.getBrowserVersion());
   }
}

Output:

The Title Of the Page is:- Google
The Scripts Executed on Browser Version: FF52

How to Run Selenium In PhantomJS Headless Browser

Earlier we are learning how we can run our automation script in the HtmlUnit Headless browser, now we are going to learn another popular headless browser PhantomJS. By using this Headless browser also we can able to test our web application with the help of inbuilt GhostDriver.

How to Setup PhantomJS Driver With Selenium WebDriver

  • We need to download the PhantomJS driver from this URL.
  • After Download and add into the project, we need to create an instance of PhantomJSDriver after that you can write your automation scripts like the Selenium WebDriver.
PhantomJS Driver
PhantomJS Driver
WebDriver driver = new PhantomJSDriver ();

The complete code snippet:

public class HeadlessBrowser 
{
   public void phantomJSDriver() throws Exception
   {
      //Set the path of the phantomjs.exe file in the properties
      System.setProperty("phantomjs.binary.path", "D:\\Selenium\\Drivers\\phantomjs.exe");
      
      // To declare and initialize PhantomJSDriver
      WebDriver driver = new PhantomJSDriver();
      
      
      // Open Google.com
      driver.get("https://www.google.com");
      
      // Get the title of the site and store it in the variable Title
      String Title = driver.getTitle();
      
      // Print the title
      System.out.println("I am at " +Title);
   }
}

Like HtmlUnitDriver this driver also allows you to run your script in different browser versions, for that you need to add only below lines of code in your script.

WebDriver driver = new PhanthomJSDriver(BrowserVersion.FIREFOX_3);

The full code snippet:

public class TestingwBrowserversions 
{
   public static void main(String[] args) 
   {
      WebDriver driver = new PhanthomJSDriver(BrowserVersion.FIREFOX_3);
      driver.get("http://www.google.com");
      System.out.println("Title of the page is" + driver.getTitle());
   }
}

This browser is one of the tester’s great choice because of the lightweight, headless and also support JavaScript. However in April 2017, the maintainer of PhantomJS has stepped down and in March 2018, this PhantomJS project is formally abandoned.

After abandoning the PhantomJS driver, it’s better to move to ChromeDriver.

How to Chrome Headless Browser Driver With Selenium WebDriver

Now Google chrome also implemented a headless option for chrome from versions 59 (you can see here). so you can download and you can use the Google Canary browser from this link.

To Run the chrome browser in headless, we can tell the ChromeDriver with using the ChromeOptions. check the below code

public class ChromeHeadlessTest 
{
   public void testExecution() throws IOException 
   {
      System.setProperty("webdriver.chrome.driver", "<chromedrier_path>");
      // Add options to Google Chrome. The window-size is important for responsive sites
      
      ChromeOptions options = new ChromeOptions();
      options.addArguments("headless");
      options.addArguments("window-size=1200x600");
      WebDriver driver = new ChromeDriver(options);
      driver.get("http://seleniumhq.org");
      driver.quit();
   }
}

The above code tells google chrome to execute in headless mode with the specified window-size. you can find out also some other options here.

How to Run Firefox in Headless with Selenium WebDriver?

Till now we have discussed how to run our selenium automation script in different headless browsers like HtmlUnitDriver, PhanthomJSDriver, ChromeDriver and now we are going to learn how we can automate in Firefox headless browser. Follow the below code to understand the same:

public class FirefoxHeadless 
{
   public static void main(String[] args) 
   {
      System.setProperty("webdriver.gecko.driver", "C:\\selenium_drivers\\geckodriver.exe");

      //Set Firefox Headless mode as TRUE
      FirefoxOptions options = new FirefoxOptions();
      options.setHeadless(true);

      //Instantiate Web Driver
      WebDriver driver = new FirefoxDriver(options);
      driver.get("http://www.google.com");
      System.out.println("Page title is - " + driver.getTitle());

      driver.close();
   }
}

This is all about how you can test your web application in different headless browsers using selenium WebDriver. Please feel free to share your feedback and suggestion about how we can more improve the article in the comment section.

Example:

How to Run Tests on Firefox Headless Browser Using Java Selenium Example?

Firefox should be 56

public class Firefox_Headless
{
   System.setProperty("webdriver.gecko.driver","./driver/geckodriver.exe");
   FirefoxOptions options=new FirefoxOptions();
   File pathToFirefoxBinary=new File("C:\\Program Files\\Mozilla Firefox\\firefox.exe");
   FirefoxBinary path=new FirefoxBinary(pathToFirefoxBinary);
   options.setBinary(path);
   options.addArguments("-headless");
   WebDriver driver = new FirefoxDriver(options);
   driver.get("http://google.com/");
   System.out.println(driver.getTitle());
}

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment