Canoo Webtest WebTest Canoo

Extension Step groovy

Description

Executes the provided Groovy script (the binding is the same for all groovy steps within a webtest).

Parameters

description
Required? no
The description of this test step.
file
Required? yes/no
The name of the file containing the script code. You may omit this parameter if you have embedded script code.
replaceProperties
Required? no, default is false
Indicates if properties (${...} and #{...}) present in the script should be replaced by their value. Use carefully as ${...} is although the syntax for GString.

Inline Text

The inline text is all the text between the start tag ( <groovy> ) and the end tag ( </groovy> ), including blanks, tabs or newlines. Using a pair of start/end tags ( <groovy> </groovy> ) has not the same behavior than the seemingly equivalent empty element tag ( <groovy/> )! See this issue for an example.

Required? yes/no
The script to execute. You may omit this if you use the attribute file.

Details

When you are supplying the scripting code you can define methods and variables as needed. In addition, you can call any of the Java methods which would be available to the <groovy> step. See the API documentation for WebTest to determine what method calls are available.

As an example, the GroovyStep class extends the Step class which has a getContext() method which returns the current Context which has a method getCurrentResponse() which returns the current response which has a method getDocumentElement(). In groovy you can just use step.context.currentResponse.documentElement to return the current HTML document.

In addition to the functionality available to the GroovyStep class, there are additional convenience variables step and out available to let the script access itself or its log respectively.

step
The groovy step itself.
out
Mapped to step's log (at the debug level).

Note that the groovy step does not provide all the convenience variables provided by scriptStep. The value of the missing variables are easily computed as follows:

document, for an html page
step.context.currentResponse.documentElement
document, for an xml page
step.context.currentResponse.xmlDocumentElement
response
step.context.currentResponse.webResponse

The examples reuse the scenario of the scriptStep .

Order Example

OrderTest
<invoke description="Load Order Page"
    url="order.html"/>
<groovy description="check qty and price" >
    def document = step.context.currentResponse.documentElement
    def calc_qty = 0
    def calc_price = 0
    document.getHtmlElementsByAttribute('tr', 'class', 'lineitem').each{
        def table_cells = it.getHtmlElementsByTagName('td')
        def qty = table_cells.get(1).asText().toInteger()
        def unit_price = table_cells.get(2).asText().toInteger()
        def total_line_price = table_cells.get(3).asText().toInteger()
        calc_qty += qty
        calc_price += total_line_price
        assert qty * unit_price == total_line_price
    }
    def root = new XmlSlurper().parseText(document.asXml())
    def totalCols = root.'**'.findAll{ it.name() == "tr" }.find{ it['@class'] == 'total' }.td
    // slightly shorter alternative to above if you don't mind explicitly specifying tr
    // totalCols = root.body.table.tbody.tr.find { it['@class'] == 'total' }.td
    assert calc_qty == totalCols[1].text().trim().toInteger()
    assert calc_price == totalCols[3].text().trim().toInteger()
</groovy>

Traffic Light Example

GroovyTrafficLightTest
<target name="testInlineGroovy">
  <property name="image_idvalue="traffic_light"/>
  <webtest name="groovy: test groovy with inlined script">
    <invoke description="Load Initial Page"
      url="trafficlight.html"/>
    <storeXPath description="extract src attribute from image with id=${image_id}"
      xpath="//img[@id='${image_id}']/@src"
      property="imagesrc"/>
    <groovy description="calculate expected alt text for src=#{imagesrc}" >
      def src2alt = ['red.gif':'stop', 'orange.gif':'wait', 'green.gif':'go']
      step.setWebtestProperty('image_alt', src2alt[step.webtestProperties.imagesrc])
    </groovy>
    <verifyXPath description="check alt value"
      xpath="//img[@id='${image_id}']/@alt"
      text="#{image_alt}"/>
  </webtest>
</target>

Rather than having the scripting code inline, you can place it in a file and reference that file as follows:

GroovyFileTrafficLightTest
<target name="testFileGroovy">
  <property name="image_idvalue="traffic_light"/>
  <webtest name="groovy: test Groovy code from file">
    <groovy description="use from file like a macro"
      src="${basedir}/GMacroSteps.groovy"/>
  </webtest>
</target>

news

Latest build: development
Posted: 19-Jul-2016 17:36

WebTest 3.0 released, featuring upgrades to Java 5, Groovy 1.6, and HtmlUnit 2.4.
The release includes support for maven integration, IDE-integration like for unit tests, capturing of background JavaScript errors, new steps for mouseOver and mouseOut events, better parallel execution of tests and - as usual - lots of handling improvements.
Posted: 5 March 2009

WebTest @ JavaOne
Dierk König presented "Functional testing of web applications: scaling with Java" on Wed May 7, 13:30 at JavaOne 2008 in the Tools and Scripting Languages track.
Posted: 8 May 2008

New WebTest screencast available:
Data Driven WebTest
Posted: 13 November 2007

First WebTest screencast available:
Creating a first Webtest Project

Extend WebTest with Groovy! Groovy in Action is available in every good bookstore.
Groovy in Action
Posted: 29 January 2007