Webtest WebTest GitHub Home

Core 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>