If you are a web application developer and have never heard of HtmlUnit , it is time to give it a look.
HtmlUnit for testing
HtmlUnit was intended to be used for UI testing. For example, let’s say you host an eCommerce Java EE application. In order to perform some testing, you would want to simulate the entire life cycle of a user creating an account, verifying the account, logging in, filling up their shopping cart, and paying for the items.
Normal Java testing methods use JUnit and only test the the back-end Java code that runs on the server. This is fine and dandy, but that is not how the user uses the website. You need to use a UI test in order to truly test. While testing the back-end is extremely valuable and should never be forgotten, you are potentially not testing several key things that can only be tested using the actual browser.
For example, here are some issues that cannot be tested on the back end:
- When attempting to make a payment, the user’s session was invalidated and they were brought to the log in screen.
- The "Check Out" button was disabled when it was not supposed to be.
JUnit and HtmlUnit can be used in combination extremely effectively. You can create assertions on each Html element returned from each page within the web session.
And it also lets you pick the browser you want to simulate. You can tell it to act like IE8, Firefox, Opera, etc. So if the web page you are testing has a browser dependent issue, you should be able to test for this using HtmlUnit.
HtmlUnit for Automation of Tasks
I was working on a team who heavily used Salesforce.com. There were several daily audit procedures (like TPS reports from Office Space) that required manual entry of the audit data into the Salesforce website. It was a tedious step and I hated doing it, so I decided to see if I could write a Java program that could automate those annoying manual entries.
Well, what API do we know of that allows you to simulate a user web browser session through a web application? HtmlUnit of course!
In under an hour, I had created a Swing application that took the Audit inputs and punched the values into Salesforce automatically. HtmlUnit logged into Salesforce, traveled to the page that lists the audits, clicks the “Create audit” button, fills in the fields, and clicks submit.
And then I started to realize the possibilities are endless with this tool!
While HtmlUnit is very cool and extremely powerful, it isn’t perfect. As you can imagine, web browsers are extremely complex and have invested 1000’s of hours of development to create them. The open source developers who made HtmlUnit do not have those sorts of resources and therefore are not going to be able to perfectly simulate each web browser.
So if you find yourself performing mundane website data entry and you get sick of it, or if you need to test the full user experience through your website instead of just the backend Java code… give HtmlUnit a whirl.