Webtest WebTest GitHub Home

Extension Step groovy

Description

Executes the provided Groovy script (the binding is the same for all groovy steps within a webtest). Deprecated in favor of using the apache script task.

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/> ).

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>