Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Added comment about incompatibility of jar included with HtmlUnit

...

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

Code Block

def parser = new org.cyberneko.html.parsers.SAXParser()
parser.setFeature('http://xml.org/sax/features/namespaces', false)
def page = new XmlParser(parser).parse('http://groovy.codehaus.org/')
def data = page.depthFirst().A.'@href'.grep{ it != null && it.endsWith('.html') }
data.each { println it }

...

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

Code Block

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

...

Here is the output in both cases:

Code Block
none
none

http://groovy.codehaus.org/apidocs/index.html
/faq.html
/groovy-jdk.html
http://groovy.codehaus.org/team-list.html
http://groovy.codehaus.org/xref/index.html
http://www.javamagazin.de/itr/ausgaben/psecom,id,317,nodeid,20.html
http://www.weiqigao.com/blog/2006/09/14/gruby_on_grails_tonight_at_630.html
http://www.oreillynet.com/onjava/blog/2006/09/charles_nutter_responds_our_fu.html

...

The following example tests the Google search engine using HtmlUnit:

Code Block

import com.gargoylesoftware.htmlunit.WebClient

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

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:

Code Block

import watij.runtime.ie.IE
import watij.finders.SymbolFactory

def ie = new IE()
ie.start('http://www.google.com')
// 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
ie.link(SymbolFactory.url, 'http://groovy.codehaus.org/').flash()
ie.close()

...

The following example tests the Google search engine using WebTest:

Code Block
xml
xml

<webtest name="google test">
    <steps>
        <invoke url="http://google.com"/>
        <verifyTitle text="Google"/>
        <setInputField name="q" value="Groovy"/>
        <clickButton name="btnG"/>
        <verifyXPath xpath="//a[@href='http://groovy.codehaus.org/']" />
    </steps>
</webtest>

...

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:

Code Block
xml
xml

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

...

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

Code Block

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

ant.taskdef(resource:'webtest.taskdef'){
    classpath(){
           pathelement(location:"$webtest_home/lib")
           fileset(dir:"$webtest_home/lib", includes:"**/*.jar")
    }
}

ant.webtest(name:'Test Google with Groovy, AntBuilder and WebTest'){
    steps(){
        invoke(url:'http://www.google.com')
        verifyTitle(text:'Google')
        setInputField(name:'q', value:'Groovy')
        clickButton(name:'btnG')
        verifyXPath(xpath:"//a[@href='http://groovy.codehaus.org/']")
    }
}

...