• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Programming Languages
    • Java Tutorials
    • Python Tutorials
    • JavaScript Tutorials
  • Automation Tools and Different Tools
    • Web Automation
      • Selenium with Java
        • Selenium Basic
        • Selenium Advance
        • Selenium Realtime
        • Framework
        • Selenium Interview
        • Selenium Videos
        • Selenium with Docker
      • Selenium with Python
      • WebdriverIO
        • Selenium Webdriver C# Tutorial
      • Cypress
      • Playwright
    • TestNG
    • Cucumber
    • Mobile Automation
      • Appium
    • API Testing
      • Postman
      • Rest Assured
      • SOAPUI
    • testRigor
    • Katalon
    • TestProject
    • Serenity BDD
    • Gradle- Build Tool
    • RPA-UiPath
    • Protractor
    • Windows Automation
  • Automation For Manual Testers
  • Services
  • Online Training
  • Contact us
  • About me
  • Follow us
    • Linkedin
    • Facebook Group
    • Facebook Page
    • Instagram

Automation

Selenium WebDriver tutorial Step by Step

You are here: Home / Advance Selenium / Headless browser Testing using Selenium HtmlUnitDriver

Headless browser Testing using Selenium HtmlUnitDriver

March 21, 2015 by Mukesh Otwani 38 Comments

Headless browser Testing using Selenium

Hello Welcome to Selenium tutorial, today we will see how to perform Headless browser Testing using Selenium HtmlUnitDriver.

Headless refer which we can’t see on UI.

Headless browser Testing using Selenium

If you have ever worked on Firefox, Chrome, IE browser in Selenium you might have faced so many issues like xpath is changing everytime when a new feature is getting added.Using these browsers, some common issue which I faced during my daily automation like Sync issues and speed as well. Some browser takes the time to load in my experience I faced performance issues with IE browser.

To  increase the speed of your test script i.e. performance of your script we can try some testcases using Headless browser Testing using Selenium.

What is Headless testing/Headless browser in Automation?

Advantage and Disadvantage of headless browsers or Why should I use this?

How to perform Headless testing in Selenium Webdriver using HTMLUnitDriver.

What are the common issues or limitation while working with Headless browsers?

So let us discuss each topic separately.

What is Headless testing/Headless browser in Automation?

Ans-A browser, which does not have any GUI it means which runs in the background. If you run your programs in Firefox, Chrome, IE and the different browser then you can see how the browser is behaving but in headless browsers, you cannot .

Advantage and Disadvantage of headless browsers or Why should I use this?

Ans-One of the most Important advantage is Performance.

1-When we run your test case using headless browsers then you will get the result just in seconds, you will see the result in below program.

2-When we have to run a test case to create some sample test data or just you have to verify some messages and functionality then you can try headless browsers.

3- When we have to run the test case on the remote machine or server, which does not have any browser, but still you have to execute test case then you can try with headless browsers again.

I hope you get the clear picture of this so let us start with some program and output as well.

There is so many headless browsers available in the market, which does the same like Nodejs etc.

When you build your test case using Jenkins then also it runs in Headless mode.

 

Here is the quick video for Headless testing using HTMLUNITDriver

 

 

Headless browser Testing using Selenium

To implement Headless testing selenium have inbuilt class known as

HtmlUnitDriver like other browsers like FirefoxDriver, ChromeDriver etc.

You can find the official https://code.google.com/p/selenium/wiki/HtmlUnitDriver

Before moving to the program, you should have the setup ready. If you have not done setup then no worry use download and Install

Java, Eclipse, Selenium Webdriver Jars, Latest Release

Program-Headless browser Testing using Selenium

package com.bog.htmldemo;

import org.openqa.selenium.By;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.htmlunit.HtmlUnitDriver;

public class HtmlDemoProgram1 {

public static void main(String[] args) throws InterruptedException {

// Declaring and initialize  HtmlUnitWebDriver
WebDriver driver = new HtmlUnitDriver();

// open facebook webpage
driver.get("http://www.facebook.com");

// Print the title
System.out.println("Title of the page "+ driver.getTitle());

// find the username field
WebElement username = driver.findElement(By.id("email"));

// enter username
username.sendKeys("mukeshotwani.50@gmail.com");

// find the password field
WebElement password = driver.findElement(By.id("pass"));

// Click the loginbutton
password.sendKeys("pjs@903998");

// find the Sign up button
WebElement Signup_button = driver.findElement(By.id("loginbutton"));

// Click the loginbutton
Signup_button.click();

// wait for 5 second to login
Thread.sleep(5000);

// You will get new title after login
System.out.println("After login title is = " + driver.getTitle());

}}

Output

Title of the page Welcome to Facebook – Log In, Sign Up or Learn More

After login, title is = Facebook

In above program, you will notice it will take only 3-4 seconds to give you the result but if try the same in other browsers it will take 15-20 seconds.

Javascript supports in the HtmlUnitDriver

All the browser which  we are using they use separate javascript engine, but HTMLUnitDriver use Rhino engine so if you will test some javascript applications then chances are high you will get diff results.

By default, JavaScript is disabled in HTMLUnitDriver so you have to enable it while writing the script.

Two ways to enable javascript in HTMLUnitDriver

First- setJavascriptEnabled method to true

WebDriver driver = new HtmlUnitDriver();

driver.setJavascriptEnabled(true);

 Second- While initializing browser itself you can enable. 

HtmlUnitDriver driver = new HtmlUnitDriver(true);

Thanks for visiting my blog. Keep in touch.

Have a nice day 🙂

Filed Under: Advance Selenium Tagged With: Headless browser, How to perform Headless browser Testing using Selenium Webdriver

Reader Interactions

Comments

  1. Kavya Chitra says

    February 21, 2017 at 5:35 PM

    Hi Mukesh,

    I am using HtmlUnit for headless browser, it is not detecting some Elements like Button. Whereas with WebDriver the test is executing. Plz help me on this.

    Reply
    • Mukesh Otwani says

      February 21, 2017 at 5:50 PM

      Can you try Phantom JS ?

      Reply
  2. Hari says

    February 20, 2017 at 4:41 PM

    Hi,
    Nice article and it is very useful for all selenium testers.
    I executed my code as per your suggetions with HtmlUnitDriver. Thank you very much

    Reply
    • Mukesh Otwani says

      February 20, 2017 at 7:28 PM

      Hi Hari,

      Great…:)

      Reply
  3. Sue says

    January 12, 2017 at 12:11 AM

    Selenium3.01 does not have the htmlunitdriver.jar , where can i find the jar?

    Reply
    • Mukesh Otwani says

      January 13, 2017 at 10:24 AM

      Hi Sue,

      HtmlUnitDriver was a part of Selenium main distribution package prior to Selenium version 2.53. From 2.53 version, you need to add it separately.

      Reply
  4. Lae says

    December 15, 2016 at 7:58 PM

    Hi Mukesh,

    On you script everything appears fine. But, on my package Test;. It’s seem to have an error. It wanted for me to import The type org.openqa.selenium.HasInputDevices cannot be resolved. It is indirectly referenced from required .class .

    Now, what is that?

    Reply
    • Mukesh Otwani says

      December 17, 2016 at 12:00 PM

      Hi Lae,

      I have not used HasInputDevices in my code.

      Reply
  5. Suman says

    October 28, 2016 at 3:15 PM

    Hi Mukesh,

    I am using Angular JS application and I am trying to fetch the data from table then it is showing error message saying that “Element not visible”.
    Can you please help me on this.

    Reply
    • Mukesh Otwani says

      November 5, 2016 at 10:49 PM

      Hi Suman,

      Below post will guide you http://learn-automation.com/solve-elementnotvisibleexception-in-selenium-webdriver/

      Reply
  6. Naresh says

    October 20, 2016 at 2:14 PM

    Hi Mukesh,
    I am using phantomjs for Headless browser testing.

    Q: How handle the alert message in headless browser ?

    Reply
    • Mukesh Otwani says

      October 20, 2016 at 2:20 PM

      Hi Naresh,

      You can handle using normal way http://learn-automation.com/handle-alert-in-selenium-webdriver/

      Reply
  7. Rajkumar S says

    October 7, 2016 at 5:24 PM

    Nice Article Mukesh, it is to the point and simple and straight.

    Reply
    • Mukesh Otwani says

      October 7, 2016 at 7:19 PM

      Thanks Rajkumar, For headless kindly visit phantomjS as well.

      Reply
  8. Naveen says

    August 26, 2016 at 8:34 PM

    Hi Mukesh,
    When I tried to print result page in google using following code.it is not printing any value. please suggest any changes it current code
    DesiredCapabilities capabilities = DesiredCapabilities.htmlUnit();
    capabilities.setBrowserName(BrowserType.FIREFOX);
    HtmlUnitDriver driver = new HtmlUnitDriver(capabilities);
    driver = new HtmlUnitDriver(capabilities);
    driver.setJavascriptEnabled(false);
    driver.get(“http://www.google.com”);
    WebElement element = driver.findElement(By.name(“q”));
    element.sendKeys(“uber”);
    element.submit();
    WebElement webElement = driver.findElement(By.xpath(“/html”));
    // WebElement webElement = driver.findElement(By.xpath(“/html”));
    String str=webElement.getAttribute(“outerHTML”);
    // String str= webElement.getAttribute(“outerHTML”);
    System.out.println(str);

    output:
    null

    Thanks
    Naveen

    Reply
    • Mukesh Otwani says

      August 27, 2016 at 10:27 PM

      Hey Naveen, its working from my side. Which Selenium version u r using?

      Reply
      • Pradeep says

        September 28, 2016 at 6:42 PM

        I am getting the same results. basically returns null.

        I am using following dependencies:

        org.seleniumhq.selenium
        selenium-server
        3.0.0-beta3

        org.seleniumhq.selenium
        selenium-java
        3.0.0-beta3

        org.seleniumhq.selenium
        selenium-support
        3.0.0-beta2

        org.seleniumhq.selenium
        selenium-htmlunit-driver
        2.52.0

        Please suggest….

        Reply
        • Mukesh Otwani says

          September 29, 2016 at 10:36 AM

          Hi Pradeep,

          Remove all and use only

          org.seleniumhq.selenium
          selenium-java
          2.53.1

          Reply
          • Cyril says

            October 10, 2016 at 1:56 PM

            Hey, I’ve Selenium-Java-2.53.1 but HtmlUnitDriver is not recognized. Can you help me ?

          • Mukesh Otwani says

            October 10, 2016 at 2:55 PM

            Hey Cycil,

            Kindly use selenium server standalone 2.53.1 and then try the same program http://selenium-release.storage.googleapis.com/index.html?path=2.53/

            Kindly check and let me know.

          • Cyril says

            October 10, 2016 at 6:04 PM

            http://image.noelshack.com/fichiers/2016/41/1476102828-bugunitdriver.png

            On this link you will see the rendering my program

          • Mukesh Otwani says

            October 12, 2016 at 11:45 AM

            Hey Cyril,

            You can download below jar to make your program work https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-htmlunit-driver/2.52.0

  9. Arpitha says

    May 19, 2016 at 11:38 AM

    hi Mukesh,

    I am facing problem in headless browser testing. The application is built using Angular JS, when i do the headless testing for the application – the script will launch the browser but it fails to load the page, as i can see in the logs “unable to locate web element “. Could you please help me in resolving this.

    Reply
    • Mukesh Otwani says

      May 21, 2016 at 3:02 AM

      Hi Arpitha,

      Use protractor for Angular JS application

      Reply
  10. Swatik Panda says

    March 28, 2016 at 11:45 AM

    Hi MUkesh,
    Thanks for this fine article on Headless browser .
    I am getting following error
    driver = new HtmlUnitDriver(BrowserVersion.FIREFOX_38);
    ((HtmlUnitDriver)driver).setJavascriptEnabled(true);

    Output:
    ———-
    Mar 28, 2016 11:38:38 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
    WARNING: Invalid base url: “/”, ignoring it
    Mar 28, 2016 11:38:39 AM com.gargoylesoftware.htmlunit.javascript.StrictErrorReporter runtimeError
    SEVERE: runtimeError: message=[An invalid or illegal selector was specified (selector: ‘*,:x’ error: Invalid selector: *:x).] sourceName=[https://stage.xyz.net/sf/js/jquery/jquery.js] line=[4] lineSource=[null] lineOffset=[0]
    Mar 28, 2016 11:38:39 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
    WARNING: Invalid base url: “/”, ignoring it
    Mar 28, 2016 11:38:39 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
    WARNING: Invalid base url: “/”, ignoring it
    Mar 28, 2016 11:38:40 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
    WARNING: Invalid base url: “/”, ignoring it
    Mar 28, 2016 11:38:40 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
    WARNING: Invalid base url: “/”, ignoring it
    Mar 28, 2016 11:38:40 AM com.gargoylesoftware.htmlunit.IncorrectnessListenerImpl notify
    WARNING: Invalid base url: “/”, ignoring it

    Note : I have disabled all the listners in xml .

    Reply
    • Mukesh Otwani says

      March 28, 2016 at 5:07 PM

      Hi Swatik,

      It seems some dependency is missing.

      Please add all jars and from seleniumhq and run test again.

      Reply
  11. Tony says

    February 29, 2016 at 4:51 PM

    any limitation of HTMLunitTest Driver it may be? i.e. support the same behavior of IE, Firefox or Chrome? thanks the sharing.

    Reply
    • Mukesh Otwani says

      March 2, 2016 at 12:01 AM

      Hi Tony, it works for simple scenario. Try Phantom JS that will work like other browsers.

      Reply
  12. Poojitha says

    February 23, 2016 at 2:16 PM

    Hi Mukesh,
    Great work and good explanation looking forward for any additional information

    Reply
    • Mukesh Otwani says

      March 3, 2016 at 2:24 PM

      Hi Poojitha,

      Thank you 🙂 Keep visiting

      Reply
  13. surekha says

    February 19, 2016 at 3:45 PM

    Please Help me how to use htmlunitdriver using python language.

    Reply
    • Mukesh Otwani says

      February 22, 2016 at 9:04 AM

      Hi Surekha,

      I have not worked on python so not sure on this 🙁

      Reply
  14. Nitin says

    December 29, 2015 at 1:50 PM

    Yo Man! You Rock My World!~! Am Ain’t MJ!! 😉 🙂

    Reply
    • Mukesh Otwani says

      December 29, 2015 at 10:02 PM

      Thanks Nitin

      Reply
  15. Madiraju Krishna Chaitanya says

    December 17, 2015 at 8:07 AM

    Hi Mukesh Ji,Nice Program.I want to highlight a small correction in the comments section in the above program that you have written.In the Line Number: 33 you have given it as “// Click the loginbutton” for the password being entered code, written below(Line Number: 34).Please correct the comment something as “enter password”. The rest of the is working fine, as designed.Thank You for your valuable time and for sharing your knowledge and experience.

    Reply
    • Mukesh Otwani says

      December 17, 2015 at 8:41 PM

      Thanks Chaitanya ji I will make the changes 🙂

      Reply
  16. Madiraju Krishna Chaitanya says

    December 17, 2015 at 7:14 AM

    Hi Mukesh Ji, Nice Article.It was very informative.I could grasp the concept very easily by following the video and also the code provided.Thank You.Please keep up the Good Work.

    Reply
    • Mukesh Otwani says

      December 17, 2015 at 8:40 PM

      Hi Chaitanya,

      Thank you your comments always keep motivating me 🙂 Please share with others too 🙂

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Primary Sidebar

Free Selenium Videos

https://www.youtube.com/watch?v=w_iPCT1ETO4

Search topic

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?

Stay connected via Facebook

Stay connected via Facebook

Archives

Footer

Categories

Recent Post

  • API Testing Using Postman And RestAssured
  • Disable Personalise Your Web Experience Microsoft Edge Prompt In Selenium
  • How To Fix Error: No tests found In Playwright
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Best and easy way to Group test cases in selenium

Top Posts & Pages

  • Selenium Webdriver tutorial for beginners
  • How To Fix Eclipse Autocomplete Or Code Suggestion In Eclipse
  • Selenium Webdriver C# Tutorial
  • WHAT ARE YOUR EXPECTATIONS FROM US?