WinRunner测试设计(6)
1、使用常量(Constants)和变量(Variables)
常量和变量在TSL中用于处理收据。
常量的值不能改变,它可以是数字、字符和字符串。
变量则相反,在每次运行时它都可以改变其值。
常量和变量名可以包括字母、数字和下划线,其第一个字符必须是字母或下划线。
TSL是大小写敏感的。
在函数体外部,如果你不能决定变量的类型,你可以不必声明它。如果你没有声明变量而使用它,WinRunner会自动认为它的类型是(auto,static,public,extern)。
例如:下面的语句使用了一个变量来存储出现在文本框内的字符串:
-----------------------
edit_get_text("Name:"text);
report_msg("The Customer Name is" & text);
-----------------------
第一句WinRunner读出现在Name文本框的内容,保存到text变量中;第二句把text变量的值显示到report中。
2、执行计算
+ 加, - 减,- 负号, × 乘, / 除, % 求模, ^或** 求幂, ++ 增1, -- 减1。
TSL支持五种运算类型:级联运算,关系运算,逻辑运算,条件运算,赋值运算。它还支持复杂的数学运算,如:sin和exp等。
下面的例子用于航班订票应用程序。WinRunner读取经济仓的票价和商务仓的票价,然后检查是否两者的价差超过100美元。
-----------------------
#Select Economy button
set_window("Flight Reservation");
button_set("Economy",ON);
#Get Economy Class ticket price from price text box
edit_get_text("Price:",economy_price);
#Click Business.
button_set("Business",ON);
#Get Business Class ticket price from price box
edit_get_text("Price:",business_price);
#Check whether price difference exceeds $100
if((business_price-economy_price)>100)
tl_step("Price_check",1,"Price difference is too large.");
-----------------------
3、创建Stress Conditions
你可以创建Stress Conditions到测试脚本中,来决定程序的界限;也可以通过loop循环来创建Stress Conditions语句块执行的次数。TSL提供了三种类型的循环:for,while,do/while。
1)for loop循环
for loop指示WinRunner执行一个或多个语句的次数。见下面的语法:
for([expression1];[expression2];[expression3])
statement
-----------------------
例子:
set_window("Open")
for(i=0;i list_select_item("File Name:_1","UI_test");#Item Number2
-----------------------
2)While loop
while loop反复执行一个语句块,只要当条件为真。语法如下:
while(expression)
statement;
-----------------------
例子:
set_window("Open");
i=0;
while(i i++;
list_select_item("File Name:_1","UI_TEST");#Item Number2
}
-----------------------
3)Do/While循环
基本同二。语法如下:
do
statement;
while(expression);
-----------------------
例子:
set_window("Flight Reservation");
i=0;
do{
menu_select_item("File; Open Order..");
set_window("Open Order");
button_press("Cancel");
i++;
}while(i -----------------------
4、Incorporating Decision-Making Statements
1)IF/ELSE语句,语法如下:
if(expression)
statement1;
[else
statement2;]
-----------------------
例子:
#Open a new order
set_window("Flight Reservation_1");
menu_select_item("File;New Order");
#Type in a date in the Date of Flight:box
edit_set_insert_pos("Date of Flight:",0,0);
type("120196");
#Type in a value in the Fly From: box
list_select_item("Fly From:","Portland");
#Type in a value in teh Fly to: box
list_select_item("Fly to:","Denver");
#Check that the Flights button is enabled
button_get_state("FLIGHT",value);
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");
-----------------------
2)Switch语句
语法如下:
switch(expression){
case case_1:
statements
case case_2:
statements
case case_n:
statements
default:statement(s)
}
-----------------------
例子:
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);
}
}
-----------------------