QTP读取写入表及循环脚本,随机数设计的整合
此处附上一份代码,用的是QTP自带的小飞机程序。
Dim excelapp,excelsheet,excelbook
Set excelapp=createobject("Excel.Application")
excelapp.Visible = True
Set excelbook=excelapp.WorkBooks.open("D:\Result.xlsx")
Set excelsheet=excelbook.Worksheets("Sheet1")
'获取起飞地点总数'
IntFlyForm = Window("Flight Reservation").WinComboBox("Fly From:").GetItemsCount
row=2
For i = 0 To IntFlyForm - 1
Window("Flight Reservation").ActiveX("MaskEdBox").Type "070119"
Window("Flight Reservation").WinComboBox("Fly From:").Select i
'获取目的地点总数
IntFlyTo = Window("Flight Reservation").WinComboBox("Fly To:").GetItemsCount
For j = 0 To IntFlyTo - 1
Window("Flight Reservation").WinComboBox("Fly To:").Select j
Window("Flight Reservation").WinButton("FLIGHT").Click
'从起飞点到目的地的航班总数'
IntFly = Window("Flight Reservation").Dialog("Flights Table").WinList("From").GetItemsCount
For k = 0 To IntFly -1
If k > 0 Then
Window("Flight Reservation").ActiveX("MaskEdBox").Type "070119"
Window("Flight Reservation").WinComboBox("Fly From:").Select i
Window("Flight Reservation").WinComboBox("Fly To:").Select j
Window("Flight Reservation").WinButton("FLIGHT").Click
End if
Window("Flight Reservation").Dialog("Flights Table").WinList("From").Select k
excelsheet.Cells(row,4).Value=Window("Flight Reservation").Dialog("Flights Table").WinList("From").GetSelection()
Window("Flight Reservation").Dialog("Flights Table").WinButton("OK").Click
Window("Flight Reservation").WinEdit("Name:").Set "louise"
'生成票数的随机值 产生1 - 8的随机票数'
Window("Flight Reservation").WinEdit("Tickets:").SetSelection 0,1
Window("Flight Reservation").WinEdit("Tickets:").Set Int((8 * Rnd ) + 1)
' 产生1 - 3的随机数'
num = Int((3 * Rnd) + 1)
'选择舱位 头等舱'
If num=1 Then
Window("Flight Reservation").WinRadioButton("First").Set
End If
'商务舱'
If num=2 Then
Window("Flight Reservation").WinRadioButton("Business").Set
End If
'经济舱'
If num=3 Then
Window("Flight Reservation").WinRadioButton("Economy").Set
End If
excelsheet.Cells(row,1).Value="070119"
excelsheet.Cells(row,2).Value=Window("Flight Reservation").WinComboBox("Fly From:").GetSelection()
excelsheet.Cells(row,3).Value=Window("Flight Reservation").WinComboBox("Fly To:").GetSelection()
excelsheet.Cells(row,5).Value= "wangmiao"
excelsheet.Cells(row,6).Value=Window("Flight Reservation").WinEdit("Tickets:").GetROProperty("text")
row=row+1
Next
Next
Next
excelapp.DisplayAlerts=False
excelsheet.SaveAs ("D:\data.xlsx")
excelbook.Close (True)
excelapp.Quit
Set excelsheet = Nothing
Set excelbook = Nothing
Set excelapp = Nothing
需要在相应读取路径下建两个表,一个(Result.xlsx)负责写入,测试完成后不会保存数据,一个(data.xlsx)负责保存测试完成后过程中的所有数据。
小飞机页面如下:
表格读取后显示如下:
需要自己先Record录制一遍,在其基础上再改代码,最后会有个生成订单,但一个订单生成要很久,所以此处删去。若需要跑很多数据,不建议生成订单。
过程中可能会有一些小问题,会弹出一个提示框,有时候需要debug,有时候需要skip,但归根结底,只要代码的设计符合程序的运行思路,就没有太大问题。