Incorporating Decision-Making Statements - WinRunner

You can incorporate decision-making into your test scripts using if/else or switch statements.

If/Else Statement

An if/else statement executes a statement if a condition is true; otherwise, it executes another statement. It has the following syntax:

if ( expression )
statement1;
[ else
statement2; ]

expression is evaluated. If expression is true, statement1 is executed. If expression is false, statement2 is executed.

For example, the if/else statement below checks that the Flights button in the Flight Reservation window is enabled. It then sends the appropriate message to the report.


if (value != ON)
report_msg ("The Flights button was successfully enabled");
else
report_msg ("Flights button was not enabled. Check that values for
Fly From and Fly To are valid");

Switch Statement

A switch statement enables WinRunner to make a decision based on an expression that can have more than two values. It has the following syntax:

switch (expression )
{
case case_1:
statements
case case_2:
statements
case case_n:
statements
default: statement(s)
}

The switch statement consecutively evaluates each case expression until one is found that equals the initial expression. If no case is equal to the expression, then the default statements are executed. The default statements are optional.

Note that the first time a case expression is found to be equal to the specified initial expression, no further case expressions are evaluated. However, all subsequent statements enumerated by these cases are executed, unless you use a break statement to pass execution to the first statement immediately following the switch statement.

The following test uses the Flight Reservation application. It randomly clicks either the First, Business or Economy Class button. Then it uses the appropriate GUI checkpoint to verify that the correct ticket price is displayed in the Price text box.

arr[1]="First";arr[2]="Business";arr[3]="Economy";
while(1)
{
num=int(rand()*3)+1; # Click class button
set_window ("Flight Reservation");
button_set (arr[num], ON);

# Check the ticket price for the selected button
switch (num)
{
case 1: #First
obj_check_gui("Price:", "list1.ckl", "gui1", 1);
break;
case 2: #Business
obj_check_gui("Price:", "list2.ckl", "gui2", 1);
break;
case 3: #Economy
obj_check_gui("Price:", "list3.ckl", "gui3", 1);
}
}


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

WinRunner Topics