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.
Syntax
if (condition1)
statement1
elseif (condition2)
statement2
else
statementN
endif
Parameters
| Parameter | Description |
|---|---|
condition | Any expression that evaluates to true or false. Build it with a value-returning API — see the rules below. |
statement | The step or steps executed when that branch is taken. A branch may hold several statements; group them in a block { ... }. |
else | Optional. Runs when none of the previous conditions was true. |
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.
Syntax
while (condition)
statement
endWhile
Parameters
| Parameter | Description |
|---|---|
condition | Evaluated before each pass. While it is true the body runs; when it becomes false, execution continues with the statement after endWhile. |
statement | Executed on every pass. To run several statements inside the loop, group them in a block { ... }. |
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 API | Returns |
|---|---|
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 |
elseifis one word — there is no space and no secondif.- A
whileloop 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.