Canoo Webtest WebTest Canoo

Extension Step ifStep

Description

Step which allows conditional execution of inner steps.

Provides the ability for conditional testing. The step takes either one of the two attributes test and unless, or a single nested element condition. Exactly one of these must have a value.

If the test attribute is set and evaluates to true, the encapsulated steps will be executed. If the unless attribute is set and evaluates to false, the intermediate steps will be executed.

If there is a nested element condition, the intermediate steps will be executed if all steps nested inside the condition are successful.

NOTE: using this step too frequently can lead to overly complicated and difficult to understand test scripts.

Parameters

description
Required? no
The description of this test step.
test
Required? yes/no
Expression which if true should cause inner steps to be executed. One of test or unless should be set unless nested condition is used.
unless
Required? yes/no
Expression which if true should cause inner steps NOT to be executed. One of test or unless should be set unless nested condition is used.

Nested Parameters

step
Required? yes
A webtest step.
condition
Required? yes/no
Group of steps which if successful should cause inner steps to be executed. Use instead of test or unless for more complex conditions.
else
Required? no
Group of steps which execute if the 'ifStep' condition fails. If neither 'then' or 'else' are explicitly set, any inner steps are treated as if they were in an implicit 'then' step.
otherwise
Required? no
Alias for else step.
then
Required? no
Group of steps which execute if the 'ifStep' condition passes. If neither 'then' or 'else' are explicitly set, any inner steps are treated as if they were in an implicit 'then' step.

Details

Examples

Place the <ifStep> around other steps, e.g.:

ifStep example 1
<target name="testNewUserOnedepends="init">
  <echo message="Testing Creation of New Users (don't run on prod)" />
  <property name="isOnProductionvalue="true" />
  <webtest name="testNewUser">
    <config .../>
    <steps>
      <invoke .../>
      <ifStep description="production teststest="${isOnProduction}">
        <not description="check DEBUG message not on prod">
          <verifyText description="Find Debug statementstext="DEBUG:" />
        </not>
      </ifStep>
      <ifStep description="non-production testsunless="${isOnProduction}">
        <verifyText description="Find Debug statementstext="DEBUG:" />
        <-- ... fill out form ... -->
        <clickButton description="register new username="createButton" />
      </ifStep>
    </steps>
  </webtest>
</target>

If you are using XSLT reporting, you should see something like this:

In this next scenario, we simply look for span statements within the html of class 'debug' and if we find any we skip creating new users. This example shows how we can use a nested condition, in addition to ant or dynamic variables, to use more sophisticated tests.

ifStep example 2
<target name="testNewUserTwodepends="init">
  <echo message="Testing Creation of New Users (don't run on prod)" />
  <webtest name="testNewUser">
    <config .../>
    <steps>
      <invoke .../>
      <ifStep description="production tests">
        <condition>
          <verifyXPath
            description="Check for debug statements"
            xpath="count(//span[@class='debug']) &gt; 0"
            />
        </condition>
        <then>
          <-- ... fill out form ... -->
          <clickButton description="register new username="createButton" />
        </then>
        <else>
          <-- do something else instead -->
        </else>
      </ifStep>
    </steps>
  </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