QTP的使用以及过程中遇到的问题及解决
基于QTP来实现自动化测试
1.遇到的第一个问题就是在回放也就是run的时候出现了一些错误。后来了解到登录名和里面Flight订单的名字并没有什么关系。还有一个特别要注意的是在开始录制前,即Record之前要在菜单的Automation>Record and Run Settings里面的Windows Applications(如图)
+添加Application Details-OK(默认即可):让QTP自动启动小飞机程序
点击图片中的绿色加号(或者直接Record,里面会弹出一样的窗口)
订单打开的时候,要先点一下左上角的小图标。(表示开始一个新的order)
如果运行完一遍订单,保存后直接运行是有问题的。原因是在这一遍中登录的操作也有录入,但是Flight订单如果没有关掉的话,那这个run就运行不了login,所有要把Flight订单窗口删掉。
所以相应的,把login部分的代码删掉,那么Flight订单的窗口就不用删除。
运行成功后,如图。
接下来是关于循环脚本的设计
先放一个单个订单的脚本
Window("Flight Reservation").WinButton("Button").Click
Window("Flight Reservation").WinObject("Date of Flight:").Type "070819"
Window("Flight Reservation").WinComboBox("Fly From:").Select "Denver"
Window("Flight Reservation").WinComboBox("Fly To:").Select "London"
Window("Flight Reservation").WinButton("FLIGHT").Click
Window("Flight Reservation").Dialog("Flights Table").WinList("From").Select "20264 DEN 12:12 AM LON 07:23 PM AA $112.20"
Window("Flight Reservation").Dialog("Flights Table").WinButton("OK").Click
Window("Flight Reservation").WinEdit("Name:").Set "wangmiao"
Window("Flight Reservation").WinEdit("Tickets:").SetSelection 0,1
Window("Flight Reservation").WinEdit("Tickets:").Set "2"
Window("Flight Reservation").WinRadioButton("Business").Set
Window("Flight Reservation").WinButton("Insert Order").Click
Window("Flight Reservation").Minimize
下面我设计了一个循环的脚本
1.以防运行次数过多,我首先只循环了航班
Window("Flight Reservation").WinButton("Button").Click
Window("Flight Reservation").WinObject("Date of Flight:").Type "070819"
Window("Flight Reservation").WinComboBox("Fly From:").Select "Denver"
Window("Flight Reservation").WinComboBox("Fly To:").Select "London"
Window("Flight Reservation").WinButton("FLIGHT").Click
IntFlight=Window("Flight Reservation").Dialog("Flights Table").WinList("From").GetItemsCount
For k=0 To IntFlight-1
If k>0 Then
Window("Flight Reservation").WinButton("Button").Click
Window("Flight Reservation").WinObject("Date of Flight:").Type "070819"
Window("Flight Reservation").WinComboBox("Fly From:").Select "Denver"
Window("Flight Reservation").WinComboBox("Fly To:").Select "London"
Window("Flight Reservation").WinButton("FLIGHT").Click
End If
Window("Flight Reservation").Dialog("Flights Table").WinList("From").Select k
Window("Flight Reservation").Dialog("Flights Table").WinButton("OK").Click
Window("Flight Reservation").WinEdit("Name:").Set "Louise"
Window("Flight Reservation").WinEdit("Tickets:").SetSelection 0,1
Window("Flight Reservation").WinEdit("Tickets:").Set "2"
Window("Flight Reservation").WinRadioButton("Business").Set
Window("Flight Reservation").WinButton("Insert Order").Click
Next
这个脚本测试通过了,仅有的两个航班都跑了一遍。
2.按这个套路,让出发地,到达地和航班三重循环
Window("Flight Reservation").WinButton("Button").Click
Window("Flight Reservation").WinObject("Date of Flight:").Type "070819"
IntFlyFrom=Window("Flight Reservation").WinComboBox("Fly From:").GetItemsCount
IntFlyTo=Window("Flight Reservation").WinComboBox("Fly To:").GetItemsCount
For i=0 To IntFlyFrom-1
Window("Flight Reservation").WinComboBox("Fly From:").Select i
For j=0 To IntFlyTo-1
If j>0 Then
Window("Flight Reservation").WinButton("Button").Click
Window("Flight Reservation").WinObject("Date of Flight:").Type "070819"
Window("Flight Reservation").WinComboBox("Fly From:").Select i
End If
Window("Flight Reservation").WinComboBox("Fly to:").Select j
Window("Flight Reservation").WinButton("FLIGHT").Click
IntFlight=Window("Flight Reservation").Dialog("Flights Table").WinList("From").GetItemsCount
For k=0 To IntFlight-1
If k>0 Then
Window("Flight Reservation").WinButton("Button").Click
Window("Flight Reservation").WinObject("Date of Flight:").Type "070819"
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
Window("Flight Reservation").Dialog("Flights Table").WinButton("OK").Click
Window("Flight Reservation").WinEdit("Name:").Set "Louise"
Window("Flight Reservation").WinEdit("Tickets:").SetSelection 0,1
Window("Flight Reservation").WinEdit("Tickets:").Set "3"
Window("Flight Reservation").WinRadioButton("Business").Set
Window("Flight Reservation").WinButton("Insert Order").Click
Next
Next
Next
循环脚本的设计过程中出现了第一个问题,如图
原因:Flight窗口最小化,导致对象不可见。
解决方法:点击一下Flight窗口,让它出现在桌面上。
问题2:循环后,执行次数过多,运行一段时间到第一个出发地的目的地和航班都循环结束准备循环出发地的时候,在最后订单生成的那一行出现了一个错误,object is diabled.即对象已禁用,还没有找出原因。