This page discusses

how to use NekoHTML, HtmlUnit, Watij and WebTest to test web applications

There are many ways to test Web Applications with Groovy:

We examine a few approaches below.

Groovy with CyberNeko HTML Parser

NekoHTML is a library which allows you to parse HTML documents (which may not be well-formed) and treat them as XML documents (i.e. XHTML). NekoHTML automatically inserts missing closing tags and does various other things to clean up the HTML if required - just as browsers do - and then makes the result available for use by normal XML parsing techniques.

Here is an example of using NekoHTML with XmlParser to find '.html' hyperlinks on the groovy homepage:

def parser = new org.cyberneko.html.parsers.SAXParser()
parser.setFeature('', false)
def page = new XmlParser(parser).parse('')
def data = page.depthFirst().A.'@href'.grep{ it != null && it.endsWith('.html') }
data.each { println it }

We turned off namespace processing which lets us select nodes using '.A' with no namespace.

Here is one way to do the same example with XmlSlurper:

def page = new XmlSlurper(new org.cyberneko.html.parsers.SAXParser()).parse('')
def data = page.depthFirst().grep{ == 'A' && it.@href.toString().endsWith('.html') }.'@href'
data.each { println it }

We didn't turn off namespace processing but do the selection using just the local name, i.e. '.name()'.

Here is the output in both cases:

Now that we have the links we could do various kinds of assertions, e.g. check the number of links, check that a particular link was always on the page, or check that there are no broken links.

Groovy and HtmlUnit

The following example tests the Google search engine using HtmlUnit:

import com.gargoylesoftware.htmlunit.WebClient

def webClient = new WebClient()
def page = webClient.getPage('')
// check page title
assert 'Google' == page.titleText
// fill in form and submit it
def form = page.getFormByName('f')
def field = form.getInputByName('q')
def button = form.getInputByName('btnG')
def result =
// check groovy home page appears in list (assumes it's on page 1)
assert result.anchors.any{ a -> a.hrefAttribute == '' }

Note that to use HtmlUnit with Groovy you should not include the xml-apis-*.jar included with HtmlUnit in your CLASSPATH.

Groovy and Watij

The following example tests the Google search engine using Watij:

import watij.finders.SymbolFactory

def ie = new IE()
// check page title
assert ie.title() == 'Google'
// fill in query form and submit it
ie.textField(SymbolFactory.@name, 'q').set('Groovy')
ie.button(SymbolFactory.@name, 'btnG').click()
// check groovy home page appears in list by trying to flash() it, '').flash()

You can use Watij from within groovysh or groovyconsole if you want to have an interactive (irb-like in ruby terms) experience.

Groovy and WebTest

The following example tests the Google search engine using WebTest:

<webtest name="google test">
        <invoke url=""/>
        <verifyTitle text="Google"/>
        <setInputField name="q" value="Groovy"/>
        <clickButton name="btnG"/>
        <verifyXPath xpath="//a[@href='']" />

The above fragment can be inserted into any Ant build script where we have defined the WebTest tasks.

The above example didn't use any Groovy but we could have just as easily used some Groovy for the last line if we didn't like the XPath expression, for example:

<webtest name="google test">
        <invoke url=""/>
        <verifyTitle text="Google"/>
        <setInputField name="q" value="Groovy"/>
        <clickButton name="btnG"/>
        assert step.context.currentResponse.anchors.any{ a -> a.hrefAttribute == '' }

Depending on your setup, you could produce the following report for this test:

Alternatively, we could have written the whole test in Groovy using AntBuilder as follows:

def webtest_home ='webtest.home'
def ant = new AntBuilder()

           fileset(dir:"$webtest_home/lib", includes:"**/*.jar")

ant.webtest(name:'Test Google with Groovy, AntBuilder and WebTest'){
        setInputField(name:'q', value:'Groovy')

Grails can automatically create this style of test for your generated CRUD applications, see Grails Functional Testing for more details.