练习:录制Flight登录-打开1-10之间随机编号的订单-退出,打开完订单后,退出前,使用msgbox输出“顾客x预定了y从z到w的c类型的k张票!”
说明:x是顾客姓名、y是日期、z是FlyFrom、w是FlyTo、k是Tickets票数、c是舱位类型
Test11001_Flight_GetROProperty
Dialog("Login").WinEdit("Agent Name:").Set "1234" Dialog("Login").WinEdit("Password:").SetSecure "5a5ff267c9e6daeadbe432336ed074e40c8e88af" Dialog("Login").WinButton("OK").Click Window("Flight Reservation").WinButton("Button").Click Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON" Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set cstr(RandomNumber(1,10)) Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click Dim x,y,z,w,k,c'定义一组变量 '获得顾客姓名 x=Window("Flight Reservation").WinEdit("Name:").GetROProperty("text") print x '获得日期 y=Window("Flight Reservation").ActiveX("MaskEdBox").GetROProperty("text") print y '获得起飞城市 z=Window("Flight Reservation").WinComboBox("Fly From:").GetROProperty("selection") print z '获得到达城市 w=Window("Flight Reservation").WinComboBox("Fly To:").GetROProperty("selection") print w '获得舱位类型 Dim f,b,e'First、Business、Economy三个单选按钮的选中状态 f=Window("Flight Reservation").WinRadioButton("First").GetROProperty("checked") print f b=Window("Flight Reservation").WinRadioButton("Business").GetROProperty("checked") print b e=Window("Flight Reservation").WinRadioButton("Economy").GetROProperty("checked") print e If f="ON" Then c="头等舱" Elseif b="ON" Then c="商务舱" Else c="经济舱" End If '获得票数 k=Window("Flight Reservation").WinEdit("Tickets:").GetROProperty("text") print k '输出信息 msgbox "顾客"&x&"预定了"&y&"从"&z&"到"&w&"的"&c&"类型的"&k&"张票!" Window("Flight Reservation").Close
一、分支结构:
1、If语句---重点!!
(1)语法:
If 条件表达式1 Then
语句块1
Elseif 条件表达式2 Then
语句块2
Elseif 条件表达式3 Then
语句块3
Else
语句块n
End if
(2)说明:
a.If分支只有一个,而且出现在开头位置
b.Elseif分支可以出现0到多个
c.Else分支可以出现0到1个,而且出现在最后一个分支
(3)功能:
a.当条件表达式1成立时,执行语句块1
b.
当条件1不成立并且条件2成立时,执行语句块2
c.以此类推
d.如果所有条件都不成立时,执行语句块n
e.每个分支的语句块执行后,都直接跳转到End If后继续执行
(4)注意:
a.If和End if配对出现
b.Elseif中间不要加空格
c.条件表达式后都要在同一行中加Then
d.Else单独占用一行
练习:判断当前是什么季节?
2-4月 春季 5-8月夏季
9-10月 秋季 11-1月 冬季
Test11002_VBS分支_四季
Dim d 'd=date()'获得当前日期 'msgbox d d="2018-10-01" Dim m m=month(d)'获得指定日期的月份数,返回值是1-12的Integer 'msgbox m If m>=11 or m=1 Then msgbox "冬季" msgbox "希望下雪!" Elseif m>=2 and m<=4 Then msgbox "春季" Elseif m>=5 and m<=8 Then msgbox "夏季" Else msgbox "秋季" End If
例:
Dim d
'd=date()'获得当前日期
'msgbox d
d="2018-10-01"
Dim m
m=month(d)'获得指定日期的月份数,返回值是1-12的Integer
'msgbox m
If m>=11 or m=1 Then
msgbox "冬季"
msgbox "希望下雪!"
Elseif m>=2 and m<=4 Then
msgbox "春季"
Elseif m>=5 and m<=8 Then
msgbox "夏季"
Else
msgbox "秋季"
End If
练习:两位数加法器,录制加法计算后退出的步骤,将两个加数数据提前通过Inputbox获取到,判断如果两个加数都有效,就输入到被测系统中计算,再退出系统,否则直接退出被测系统。
说明:都有效指都在-99和99之间。
Test11003_两位数加法器_If
Dim n1,n2 n1=cint(Inputbox("请输入第一个数")) n2=cint(Inputbox("请输入第二个数")) '判断如果两个加数都有效 If n1>=-99 and n1<=99 and n2>=-99 and n2<=99 Then '就输入后计算 VbWindow("Form1").VbEdit("Text1").Set n1 VbWindow("Form1").VbEdit("Text2").Set n2 VbWindow("Form1").VbButton("计算(J)").Click End If '否则就直接退出 VbWindow("Form1").VbButton("退出(E)").Click VbWindow("Form1").Dialog("退出提示").WinButton("确定").Click
练习:Flight,录制3个Action:登录-试图打开1000号订单,对提示信息做检查,关闭提示框,关闭打开订单的窗口-退出,对打开订单中的订单号做Action参数化,在DataTable准备数据如下:1、1000、5、8、2000、10,书写代码判断如果订单号大于200,那么检查提示信息后取消打开订单
注意:检查点的预期值也需要做Action参数化
Test11004_Flight_打开订单_If
登录Action() Dialog("Login").WinEdit("Agent Name:").Set "1234" Dialog("Login").WinEdit("Password:").SetSecure "5a603c6bef79099c027adcffab89b8da7d25f599" Dialog("Login").WinButton("OK").Click 打开订单Action() Window("Flight Reservation").WinButton("Button").Click Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON" Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set DataTable("订单号",dtLocalSheet) Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click If cint(DataTable("订单号",dtLocalSheet))>200 Then Window("Flight Reservation").Dialog("Open Order").Dialog("Flight Reservations").Static("Order number 1000 does").Check CheckPoint("Order number 1000 does not exist") Window("Flight Reservation").Dialog("Open Order").Dialog("Flight Reservations").WinButton("确定").Click Window("Flight Reservation").Dialog("Open Order").WinButton("Cancel").Click End If 退出Action() Window("Flight Reservation").Close
2、Select Case语句
(1)作用:类似于java语言中的switch结构,判断同一个内容等于不同值时,做不同的处理,一般用于多分支结构中。
(2)语法:
Select Case 内容
Case 值1
语句块1
Case 值2
语句块2
Case Else
语句块n
End Select
(3)说明:
a.Case分支可以出现1到多个
b.Case Else分支可以出现0到1个,并且在所有Case分支后
c.Select Case和End Select配对出现
(4)功能:
a.当判断的内容等于值1时,执行语句块1
b.当等于值2时,执行语句块2
c.当不等于以上所有值时,就执行语句块n
d.任何一个分支的语句块执行后,都直接跳转到End Select后继续执行。
(5)注意:
a.每个值都和判断的内容子类型相同
b.每个Case后的值不要重复
c.Case Else单独占用一行,其后不要书写值
d.不要在Select Case后,第一个Case前书写任何代码
练习:判断当前是周几?
Test11005_VBS分支_星期几
Dim d d=date() Dim w w=weekday(d)'获得当前是周几的整数1-7 msgbox w'1代表周日、2代表周一……6代表周五、7代表周六 Dim n n=4 Select Case cint(w) '注意:此位置不要书写代码 Case 1 msgbox "周日" msgbox "===" Case 2 msgbox "周一" Case 3 msgbox "周二" Case n msgbox "周三" Case n+1 msgbox "周四" Case Else msgbox "周五或周六" End Select
练习:录制Flight的3个Action:登录-订票-退出,对订票时的顾客姓名和舱位类型做Action参数化
jack 商务舱
rose 经济舱
peter 头等舱
alice 商务舱
Test11006_Flight_订票_Select Case
登录Action() Dialog("Login").WinEdit("Agent Name:").Set "1234" Dialog("Login").WinEdit("Password:").SetSecure "5a605288711b4f31adca14510c20341fe1415f7f" Dialog("Login").WinButton("OK").Click 订票Action() Window("Flight Reservation").WinButton("Button").Click Window("Flight Reservation").ActiveX("MaskEdBox").Type "111119" Window("Flight Reservation").WinComboBox("Fly From:").Select "Frankfurt" Window("Flight Reservation").WinComboBox("Fly To:").Select "Los Angeles" Window("Flight Reservation").WinButton("FLIGHT").Click Window("Flight Reservation").Dialog("Flights Table").WinList("From").Select "20334 FRA 12:12 AM LAX 07:23 PM AA $112.20" Window("Flight Reservation").Dialog("Flights Table").WinButton("OK").Click Window("Flight Reservation").WinEdit("Name:").Set DataTable("姓名",dtLocalSheet) Select Case DataTable("舱位",dtLocalSheet) Case "头等舱" Window("Flight Reservation").WinRadioButton("First").Set Case "商务舱" Window("Flight Reservation").WinRadioButton("Business").Set Case Else Window("Flight Reservation").WinRadioButton("Economy").Set End Select Window("Flight Reservation").WinButton("Insert Order").Click 退出Action() Window("Flight Reservation").Close
二、循环结构
1.For循环---重点!!
(1)作用:实现固定次数的循环。
Test11007_VBS循环_For
'Option Explicit 'Dim abc 'For abc=10 to 2 step -2 ' print abc ' print "====" ' print "******" 'Next '练习:1+2+3+4+5+……+9+10=? 'Dim i 'Dim sum 'sum=0 'For i=1 to 10 ' print i ' sum=sum+i 'Next 'print sum'55 '练习:1+2+3+4+5+……+9+10, '过程中如果结果大于20就不再加 Dim i Dim sum sum=0 For i=1 to 10 print i sum=sum+i print "==========="'打印??次-----6次 If sum>20 Then print "###########"'打印??次-----1次 Exit For'强制退出For循环,类似于java的break print "+++++++++++++"'打印??次----0次 End If print "***************"'打印??次----5次 Next print sum'1+2+3+4+5+6=21
(2)语法:
For 循环变量=初值 To 终值 Step 步长
循环体
If 条件表达式 Then
Exit For
End if
Next
(3)说明:
a.Step 步长可以出现0到1次
b.Exit For可以出现0到1次
c.默认步长是1,步长可以是整数或小数,也可以是负数(初值要大于终值)
d.Exit For一般用于一个分支中
(4)功能:
a.开始执行时,循环变量赋值为初值
b.每次执行循环体前,都会判断循环变量的当前值,如果未超过终值,会执行本次循环,否则就退出循环
c.每次执行循环体后,循环变量都会自动增加步长的值。
d.执行到Exit For,就退出整个循环。
(5)注意:
a.For和Next配对出现
b.在Exit For的同分支中,其后不要书写代码
(6)在自动化测试中,使用任何循环语句,都要保证循环体的首尾步骤能衔接操作执行。
练习:Flight,实现登录-打开1到5号订单-退出
Test11008_Flight_打开订单_For
Dialog("Login").WinEdit("Agent Name:").Set "1234" Dialog("Login").WinEdit("Password:").SetSecure "5a606bf62bc7faefd0565da1055934be2b003951" Dialog("Login").WinButton("OK").Click Dim i For i=1 to 5 Window("Flight Reservation").WinButton("Button").Click Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON" Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set i Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click Next Window("Flight Reservation").Close
练习:Flight,实现登录-打开2到7号订单-退出
Test11009_Flight_打开订单_For
Dialog("Login").WinEdit("Agent Name:").Set "1234" Dialog("Login").WinEdit("Password:").SetSecure "5a606bf62bc7faefd0565da1055934be2b003951" Dialog("Login").WinButton("OK").Click Dim i For i=1 to 6 Window("Flight Reservation").WinButton("Button").Click Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON" Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set i+1 Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click Next Window("Flight Reservation").Close
方法一:初值改为2,终值改为7
方法二:初值是1,终值是6,订单号使用i+1的值
练习:Flight,实现登录-打开2、4、6、8、10号订单-退出
方法一:初值改为2,终值改为10,步长2
方法二:初值是1,终值是5,订单号使用i*2的值