• Jobs
  • About
  • Introduction to HtmlUnit October 26, 2008

    What is HtmlUnit

    HtmlUnit is an opensource tool that simulates browser behavior. HtmlUnit is written in Java and provides an API that allows you to do all browser actions from invoking web pages to driving web actions like filling out form fields, clicking links etc.

    HtmlUnit is also very good at parsing HTML documents to represent a DOM even when the source may not be well formed. This provides a lot of control to a user to read specific parts of the page´s content Рwhich is extremely helpful for say the purposes of validating it (aka check points).

    What should it be used for

    Even though HtmlUnit has ¨Unit¨ in its name, its not meant only for unit testing. It can be used for component or functional testing as well. For a developer, this tool works very well in a JUnit or TestNG testing framework. Considering HtmlUnit has a fairly large API exposed, good control on the HTML DOM, and supports javascript and AJAX, this tool should be able to do everything you need.

    From a QA Engineer´s mindset, its not that black and white. The first question that you might be asking yourself could be Рif this is a browser simulator, how much can I trust it? It is true that this is a simulator, but its pretty darn close to the real thing. If the purpose of your test cases is to test the core functionality of your AUT (Application Under Test) by navigating through it, validating content on the pages, filling and submitting forms, etc, this tool will most likely suit all your needs.

    If the purpose of the tests were to validate cross browser/ cross platform behavior, then this may not be the best tool for you. Even though HtmlUnit can simulate FireFox and Internet Explorer, I do not use it for my cross browser test cases i.e. test cases that I run on say IE, FF, Safari etc.

    This however does not mean that whenever you have to test your web application with different browsers, you stop considering this tool! Even though this varies by companies goals, type of software products that are tested etc, my experience always has been where we had a large set of test cases that tested the core functionality of the app and a much smaller set of test cases for features that were more prone to differences in behavior across browsers. In this case, I would choose to have my core functionality tested in HtmlUnit and cross browser test cases tested using a different tool like Selenium, QTP etc that ran tests on all the supported real browser instances.

    One other aspect of HtmlUnit to keep in mind is that the ¨browser¨ is in memory. So, you cannot see what htmlunit is working on (unless you view the page content through say a debugger). This makes htmlunit faster for static pages. However, if the website is very dynamic, using AJAX for instance, HtmlUnit can be slower than other real browser driving test frameworks.

    In my opinion, one of the best things going for HtmlUnit is its active community. There have been frequent upgrades over the past couple of years and when I file bugs against them, they are usually fixed within days!


    HtmlUnit is an in-memory browser simulator providing a fairly large API to control all the browser actions. It works well with javascript and AJAX and is constantly improving. It can be used for unit, component or functional testing as long as cross browser testing is not the objective.

    More Information and Resources

    Posted by Rahul Poonekar in : HtmlUnit

    Leave a Reply

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