Assertions in HtmlUnit

I’ve been going back through the pros and cons of JWebUnit as part of my research for the HtmlUnit vs Foo series of articles I’m writing. One of JWebUnit’s big draws is the set of easy-to-use assertion methods provided by its base test case class, WebTestCase. HtmlUnit doesn’t provide such a thing, because it doesn’t provide a base test case class.

There has always been something of a trade-off here: use JWebUnit and tie yourself to a specific unit testing framework (JUnit) while benefiting from a more domain-specific set of assertions (assertCookiePresent, assertFormPresent, assertLinkPresent, etc), or fly free with HtmlUnit but perform assertions using only the primitive utility methods provided by your unit testing framework (assertNull, assertNotNull, assertEquals, etc).

However, I’ve long though that it would be nice for HtmlUnit to have the best of both worlds by using an assertion utility class, similar to TestNG’s Assert class. Experiencing the convenience of JWebUnit’s API again has given me the final kick in the pants, and the first version of HtmlUnit’s new WebAssert class is now in SVN. It will be included as part of HtmlUnit 1.14, or you can always grab the latest build here. I’m sure the set of available assertions will grow, but here is the initial list:

  • assertTitleEquals(HtmlPage, String)
  • assertTitleContains(HtmlPage, String)
  • assertTitleMatches(HtmlPage, String)
  • assertElementPresent(HtmlPage, String)
  • assertElementPresentByXPath(HtmlPage, String)
  • assertElementNotPresent(HtmlPage, String)
  • assertElementNotPresentByXPath(HtmlPage, String)
  • assertTextPresent(HtmlPage, String)
  • assertTextPresentInElement(HtmlPage, String, String)
  • assertTextNotPresent(HtmlPage, String)
  • assertTextNotPresentInElement(HtmlPage, String, String)
  • assertLinkPresent(HtmlPage, String)
  • assertLinkNotPresent(HtmlPage, String)
  • assertLinkPresentWithText(HtmlPage, String)
  • assertLinkNotPresentWithText(HtmlPage, String)
  • assertFormPresent(HtmlPage, String)
  • assertFormNotPresent(HtmlPage, String)
  • assertInputPresent(HtmlPage, String)
  • assertInputNotPresent(HtmlPage, String)
  • assertInputContainsValue(HtmlPage, String, String)
  • assertInputDoesNotContainValue(HtmlPage, String, String)

Post a Comment