QTP自带Flight航班练习__一次订多张机票

上一篇 / 下一篇  2010-03-05 16:00:18 / 个人分类:QTP学习历程

    本来是想通过http://newtours.mercury.com这个网站去进行练习的..但无奈最近这个网站都打不开了.所以我就使用QTP自带的Flight航班进行练习.

    通过在网上查阅资料,结合该例子的一些问题.完成了关于Flight例子中订多张机票的形式,其中采用随机数的方式获取Fly from和Fly to两个下拉框的值,同时使用循环进行订多张机票的例子,根据for循环语句的不同修改了两次脚本:

1)脚本一如下:

Dim Get_Count1,Ran_Number1,Result1,Get_Count2,Ran_Number2,Result2 '声明六个变量
Dim j'声明循环变量
'声明一个函数
Function GetList(i)
   Randomize
   GetList=RandomNumber(0,i)
End Function

For j=1 to 10
SystemUtil.Run "D:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\flight4a.exe","","D:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\","open"
Dialog("Login").WinEdit("Agent Name:").Set "mercury"
Dialog("Login").WinEdit("Password:").SetSecure "4b8dbe97c21c421fc9d4cb76558d19f04c2ccc5a"
Dialog("Login").WinButton("OK").Click
'Window("Flight Reservation").WinObject("Date of Flight:").Type "100316"


'将声明变量放到第一行Dim Get_Count1,Ran_Number1,Result1,Get_Count2,Ran_Number2,Result2 '声明六个变量

'使用循环语句去订十张机票
'Dim i '声明循环变量i
'For j=1 to 10
Window("Flight Reservation").WinObject("Date of Flight:").Type "100316" 
Get_Count1=Window("Flight Reservation").WinComboBox("Fly From:").GetItemsCount '取得下拉框的记录的总条数
Ran_Number1=GetList(Get_Count1-1) '调用函数GetList,随机取一个数字
Result1=Window("Flight Reservation").WinComboBox("Fly From:").GetItem(Ran_Number1) '获取下拉框第Ran_Number1条记录的值

Window("Flight Reservation").WinComboBox("Fly From:").Select Result1 '选择刚才获取到的值

Get_Count2=Window("Flight Reservation").WinComboBox("Fly To:").GetItemsCount '取得Fly to字段下拉框记录的总条数
Ran_Number2=GetList(Get_Count2-1) '调用函数GetList随机取一个数字
Result2=Window("Flight Reservation").WinComboBox("Fly To:").GetItem(Ran_Number2) '获取下拉框第Ran_Number2条记录的值

Window("Flight Reservation").WinComboBox("Fly To:").Select Result2 '选择刚才获取到的值

Window("Flight Reservation").WinButton("FLIGHT").Click
Window("Flight Reservation").Dialog("Flights Table").WinList("From").Select 1

'Window("Flight Reservation").Dialog("Flights Table").Activate
Window("Flight Reservation").Dialog("Flights Table").WinButton("OK").Click
'Window("Flight Reservation").Activate
'Window("Flight Reservation").Dialog("Flights Table").WinButton("OK").Click '点击OK按钮,自写
Window("Flight Reservation").WinEdit("Name:").Type "M"
Window("Flight Reservation").WinEdit("Name:").Set "May"
Window("Flight Reservation").WinButton("Insert Order").Click
wait(5)
Window("Flight Reservation").WinMenu("Menu").Select "File;New Order"
'Window("Flight Reservation").WinMenu("Menu").Select "File;Open Order..."
'Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Flight Date").Set "ON"
'Window("Flight Reservation").Dialog("Open Order").WinObject("10/03/16").Type "100316"
'Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
'Window("Flight Reservation").Dialog("Open Order").Dialog("Search Results").WinButton("OK").Click
'next
Window("Flight Reservation").WinMenu("Menu").Select "File;Exit"
next
systemutil.CloseProcessByName "D:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\flight4a.exe" '关闭flight程序

2)脚本二如下:

Dim Get_Count1,Ran_Number1,Result1,Get_Count2,Ran_Number2,Result2 '声明六个变量
Dim j'声明循环变量
'声明一个函数
Function GetList(i)
   Randomize
   GetList=RandomNumber(0,i)
End Function

'For j=1 to 10
SystemUtil.Run "D:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\flight4a.exe","","D:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\","open"
Dialog("Login").WinEdit("Agent Name:").Set "mercury"
Dialog("Login").WinEdit("Password:").SetSecure "4b8dbe97c21c421fc9d4cb76558d19f04c2ccc5a"
Dialog("Login").WinButton("OK").Click
'Window("Flight Reservation").WinObject("Date of Flight:").Type "100316"


'将声明变量放到第一行Dim Get_Count1,Ran_Number1,Result1,Get_Count2,Ran_Number2,Result2 '声明六个变量

'使用循环语句去订十张机票
'Dim i '声明循环变量i
For j=1 to 10
Window("Flight Reservation").WinObject("Date of Flight:").Type "100316" 
Get_Count1=Window("Flight Reservation").WinComboBox("Fly From:").GetItemsCount '取得下拉框的记录的总条数
Ran_Number1=GetList(Get_Count1-1) '调用函数GetList,随机取一个数字
Result1=Window("Flight Reservation").WinComboBox("Fly From:").GetItem(Ran_Number1) '获取下拉框第Ran_Number1条记录的值

Window("Flight Reservation").WinComboBox("Fly From:").Select Result1 '选择刚才获取到的值

Get_Count2=Window("Flight Reservation").WinComboBox("Fly To:").GetItemsCount '取得Fly to字段下拉框记录的总条数
Ran_Number2=GetList(Get_Count2-1) '调用函数GetList随机取一个数字
Result2=Window("Flight Reservation").WinComboBox("Fly To:").GetItem(Ran_Number2) '获取下拉框第Ran_Number2条记录的值

Window("Flight Reservation").WinComboBox("Fly To:").Select Result2 '选择刚才获取到的值

Window("Flight Reservation").WinButton("FLIGHT").Click
Window("Flight Reservation").Dialog("Flights Table").WinList("From").Select 1

'Window("Flight Reservation").Dialog("Flights Table").Activate
Window("Flight Reservation").Dialog("Flights Table").WinButton("OK").Click
'Window("Flight Reservation").Activate
'Window("Flight Reservation").Dialog("Flights Table").WinButton("OK").Click '点击OK按钮,自写
Window("Flight Reservation").WinEdit("Name:").Type "M"
Window("Flight Reservation").WinEdit("Name:").Set "May"
Window("Flight Reservation").WinButton("Insert Order").Click
wait(5)
Window("Flight Reservation").WinMenu("Menu").Select "File;New Order"
'Window("Flight Reservation").WinMenu("Menu").Select "File;Open Order..."
'Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Flight Date").Set "ON"
'Window("Flight Reservation").Dialog("Open Order").WinObject("10/03/16").Type "100316"
'Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
'Window("Flight Reservation").Dialog("Open Order").Dialog("Search Results").WinButton("OK").Click
next
Window("Flight Reservation").WinMenu("Menu").Select "File;Exit"
'next
systemutil.CloseProcessByName "D:\Program Files\Mercury Interactive\QuickTest Professional\samples\flight\app\flight4a.exe" '关闭flight程序

PS:在该例子中,使用到了VBScript的一些知识进行完善脚本;

1)DIM声明变量。声明变量的一种方式是使用Dim语句、Public语句和Private语句在 Script. 中显式声明变量。声明多个变量需要使用逗号分隔变更。

VBSCRIP的变量命名规则:

  • 第一个字符必须是字母。
  • 不能包含嵌入的句点。
  • 长度不能超过 255 个字符。
  • 在被声明的作用域内必须唯一。
  • 2)FUNCTION语句。声明Function过程的名称、参数以及构成其主体的代码。

    语法
    [Public|Private]Functionname[(arglist)]
    [statements]
    [name=expression]
    [Exit Function]
    [statements]
    [name=expression]
    End Function

    Function语句的语法包含以下部分:

    部分描述
    Public表示Function过程可被所有 Script. 中的所有其他过程访问。
    Private表示Function过程只可被声明它的 Script. 中的其他过程访问。
    nameFunction的名称,遵循标准的变量命名约定。
    arglist代表调用时要传递给Function过程的参数的变量列表。用逗号隔开多个变量。
    statementsFunction过程的主体中执行的任意语句组。
    expressionFunction的返回值。

    3)RANDOMIZE语句

    描述
    初始化随机数生成器。
    语法
    Randomize[number]

    number参数可以是任何有效的数值表达式

    说明
    Randomize使用number参数初始化Rnd函数的随机数生成器,赋给它新的种子值。如果省略number,则使用系统计时器返回的值作为新的种子值。

    如果不使用Randomize,则第一次调用Rnd函数(无参数)时,它将使用相同的数字作为种子值,随后使用最后生成的数值作为种子值。


    注意要重复随机数的序列,请在使用数值参数调用Randomize之前,立即用负值参数调用Rnd。使用相同的number值的Randomize不能重复先前的随机数序列。

    4)FOR....NEXT语句

    描述
    以指定次数重复执行一组语句。
    语法
    Forcounter=startToend[Stepstep]
    [statements]
    [Exit For]
    [statements]
    Next

    For...Next语句的语法包含以下部分:

    部分描述
    counter用做循环计数器的数值变量。这个变量不能是数组元素或用户自定义类型的元素。
    startcounter的初值。
    endcounter的终值。
    stepcounter的步长。如果没有指定,则step的默认值为 1。
    statementsForNext之间的一条或多条语句,将被执行指定次数。

    说明
    step参数可以是正数或负数。step参数值决定循环的执行情况,如下所示:

    如果 ... 则循环执行
    正数或 0counter<=end
    负数counter>=end

    当循环启动并且所有循环中的语句都执行后,step值被加到counter中。这时,或者循环中的语句再次执行(基于循环开始执行时同样的测试),或者退出循环并从Next语句之后的语句继续执行。


    提示在循环中改变counter的值,将会使程序代码的阅读和调试变得更加困难。

    Exit For只能用于For Each...NextFor...Next结构中,提供另一种退出循环的方法。可在语句中的任意位置放置任意个Exit For语句。Exit For经常和条件判断语句一起使用(例如If...Then),并立即将控制权转移到Next之后的语句。

    可以将一个For...Next循环放置在另一个For...Next循环中,组成嵌套循环。每个循环中的counter要使用不同的变量名。下面的结构是正确的:

    ForI = 1To10ForJ = 1To10ForK = 1To10. . .NextNextNext


    TAG:

     

    评分:0

    我来说两句

    Open Toolbar