Skip to main content

Conditional Functions (if / elseif / else / while)

Conditional and loop functions let a Test Designer script make a decision at playback time instead of running a fixed line-by-line sequence. Use them to branch on what the application actually rendered, and to repeat a block of steps until a condition is met.

if / elseif / else / endif

Runs one of several statement blocks depending on which condition evaluates to true. Conditions are evaluated top to bottom and the first true branch wins.

if syntax

if (condition1)
    statement1
elseif (condition2)
    statement2
else
    statementN
endif

if parameters

ParameterDescription
conditionAny expression that evaluates to true or false. Build it with a value-returning API — see the rules below.
statementThe step or steps executed when that branch is taken. A branch may hold several statements; group them in a block { ... }.
elseOptional. Runs when none of the previous conditions was true.

if example

navigateTo("http://demosite.appvance.com/");
wait(1000);

var items = getText(_span("cart-items"));

if (items == "0")
    click(link("Ruby on Rails Tote"));
elseif (items == "1")
    click(link("View cart"));
else
    click(link("Checkout"));
endif

while / endWhile

Creates a loop that repeats a statement as long as the test condition evaluates to true. The condition is evaluated before each pass, so the body never runs if the condition is already false.

while syntax

while (condition)
    statement
endWhile

while parameters

ParameterDescription
conditionEvaluated before each pass. While it is true the body runs; when it becomes false, execution continues with the statement after endWhile.
statementExecuted on every pass. To run several statements inside the loop, group them in a block { ... }.

while example

navigateTo("http://demosite.appvance.com/");
wait(1000);

var page = 1;

while (page <= 3)
{
    click(link("Page " + page));
    wait(1000);
    page = page + 1;
}
endWhile

Rules and pitfalls

Never put an assert or verify API inside a condition. Those APIs return no value and throw an exception when the assertion fails, so an if built on one aborts the script instead of branching. To decide a branch, use an API that returns a value and compare it.

Value-returning APIReturns
getText()Visible text of an element
getValue()Current value of an input
getAttribute()Value of an element attribute
getBrowserURL()Current page URL
isVisible()true / false
  • elseif is one word — there is no space and no second if.
  • A while loop re-evaluates its condition every pass, so make sure something inside the loop changes, or it will never end.
  • Add a wait() inside loops that click through pages, otherwise the next pass reads the previous page.

Related: Assert Functions, Verify Functions.