QTP测试多个Windows应用程序的解决方案

relevantcodes.com的《QTP: Working With Multiple Windows Applications》这篇文章介绍了如何测试多个相同Windows应用程序的方法:

http://relevantcodes.com/qtp-working-with-multiple-windows-applications/

 

其核心思想是把出现的窗口句柄存到数组中,并给窗口一个指定的名字,这样后面访问窗口对象时,只要指定名字就可以了。

 

核心类colWindows封装在名为clsWindow.qfl的函数库文件中,主要由以下函数组成:

LaunchAdd: Launch a new window and retain its reference throughout the test cycle

AddNew: Automatically add a new open window to the collection without you having to specify its properties (see list of dependencies)

AddCustom: Adds a custom window by specifying its properties as an array

 

 

使用例子如下所示(其中WindowObject就是由colWindows类产生的对象):

'Please make sure the library file (clsWindow.qfl) is associated with the test.

'clsWindow.qfl is located in the test's directly: /clsWindow95Demo/clsWindow.qfl

'=================================

 

 

'============================================

'LaunchAdd

'============================================

 

'Open the AUT and add it to our collection as "Flight1"

'If using 9.2 and below:

WindowObject.LaunchAdd "C:/Program Files/HP/QuickTest Professional/samples/flight/app/flight4a.exe", "Flight1"

'If using 9.5 and higher:

'WindowObject.LaunchAdd "C:/Program Files/HP/QuickTest Professional/samples/flight/app/flight4a.exe", "Flight1"

'Use the reference we gave in the previous statement (Flight1):

With WindowObject.Name("Flight1")

       .WinEdit("attached text:=Agent Name:").Set "mercury"

       .WinEdit("attached text:=Password:").Set "mercury"

       .WinButton("text:=OK").Click

End With

 

 

'============================================

'AddCustom

'============================================

 

MsgBox "Demonstrating AddCustom: It will add the windows, whose properties we specify through an array."

 

'Add a custom window by specifying its description as FlightReservation2

WindowObject.AddCustom Array("regexpwndtitle:=Flight Reservation", "index:=1"), "FlightReservation2"

With WindowObject.Name("FlightReservation2")

       .Highlight

       .WinObject("attached text:=Date of Flight:", "index:=0").Type "09/09/12"

       .WinComboBox("attached text:=Fly From:", "index:=0").Select "Denver"

       .WinComboBox("attached text:=Fly To:", "index:=0").Select "Frankfurt"

       .WinButton("text:=FLIGHT").Click

End With

Dialog("micclass:=Dialog").WinButton("text:=OK").Click

With WindowObject.Name("FlightReservation2")

       .WinEdit("attached text:=Name:", "index:=0").Type "Relevant Codes 2"

       .WinButton("text:=&Insert Order").Click

       .WinObject("nativeclass:=AfxWnd40").WaitProperty "regexpwndtitle", "Insert Done...", 30000

End with

 

 

'============================================

'LaunchAdd

'============================================

 

'Open another instance of our AUT and add it to our collection as "Flight2"

'If using 9.2 and below:

WindowObject.LaunchAdd "C:/Program Files/HP/QuickTest Professional/samples/flight/app/flight4a.exe", "Flight2"

'If using 9.5 and higher:

'WindowObject.LaunchAdd "C:/Program Files/HP/QuickTest Professional/samples/flight/app/flight4a.exe", "Flight2"

'Use the reference we gave in the previous statement (Flight2):

With WindowObject.Name("Flight2")

       .WinEdit("attached text:=Agent Name:").Set "mercury"

       .WinEdit("attached text:=Password:").Set "mercury"

       .WInButton("text:=OK").Click

End With

 

 

'============================================

'AddNew

'============================================

 

MsgBox "Demonstrating AddNew: It will automatically add the newly open window and execute a few statements."

 

'Add the last open window to the collection as "FlightReservation1"

WindowObject.AddNew "FlightReservation1", 20

With WindowObject.Name("FlightReservation1")

       .Highlight

       .WinObject("attached text:=Date of Flight:", "index:=0").Type "09/09/12"

       .WinComboBox("attached text:=Fly From:", "index:=0").Select "Denver"

       .WinComboBox("attached text:=Fly To:", "index:=0").Select "Frankfurt"

       .WinButton("text:=FLIGHT").Click

End With

Dialog("text:=Flights Table").WinButton("text:=OK").Click

With WindowObject.Name("FlightReservation1")

       .WinEdit("attached text:=Name:", "index:=0").Type "Relevant Codes 1"

       .WinButton("text:=&Insert Order").Click

       .WinObject("nativeclass:=AfxWnd40").WaitProperty "regexpwndtitle", "Insert Done...", 30000

End with

 

 

 

'Release object

WindowObject.Destroy

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
clsWindow类使用说明 clsWindow是VB6环境下使用的一个操作外部程序窗口的类,比如得到窗口句柄,得到窗口里某个文本框的内容。非常方便,使用它可以让您脱身于一堆api函数,功能强大使用简单! 这个类楼主很早就开始封装了,原本打算做成类似DOM对象那样,通过一堆getElmentByXXX等方法实现对桌面程序下各个窗口以及里面各个控件对象的自由访问,但是具体要做的工作太多,目前只实现了一部分,期待大家一起加入更新维护。 目前该类封装了绝大部分对windows窗口的常用操作,例如:获取窗口句柄,设置窗口为活动窗口,设置窗口内文本框内容,点击窗口内的某些按钮等。 这个类现在还在一直不断地扩充,功能已经很强大很广泛,使用它可以轻而易举地设置窗口标题栏文字,移动窗体等等。以前要实现这些操作常常需要一大堆api函数,现在只需要一点点代码就可以了,完全让您脱身于api函数的海洋。当然您如果想知道里面的方法实现原理的话可以看一看源代码。 使用范例: 1)关闭腾讯新闻窗口“腾讯网迷你版”。 Dim window As New clsWindow If window.GetWindowHwndByTitle("腾讯网迷你版") > 0 Then window.CloseWindow '关闭窗口 End If 以上是不是很简洁呢? 2)获取某个打开的记事本里面的内容。假设记事本标题为“测试.txt - 记事本”,通过SPY等工具查看得知记事本的文本框类名为:Edit,那么我们编写程序如下: Dim window As New clsWindow If window.GetWindowHwndByTitle("测试.txt - 记事本") > 0 Then MsgBox window.GetElementTextByClassName("Edit") End If 这个看起来也很简单,方法自由还可以使用正则匹配,可以写成下面这样: Dim window As New clsWindow If window.GetWindowHwndByTitleRegExp("测试\.txt.*?") > 0 Then MsgBox window.GetElementTextByClassName("Edi", , True)'第三个参数表示是否使用正则,默认为false End If clsWindow类最新版下载请关注博客: http://blog.csdn.net/sysdzw/article/details/9083313 '======================================================================= '描 述:一个操作windows窗口的类,可对窗口进行很多常用的操作(类名为clsWindow) '使用范例:Dim window As New clsWindow ' window.GetWindowByTitle "计算器" ' window.closeWindow '编 程:sysdzw 原创开发,如果有需要对模块扩充或更新的话请邮箱发我一份,共同维护 '发布日期:2013/06/01 '博 客:http://hi.baidu.com/sysdzw ' http://blog.csdn.net/sysdzw 'Email :sysdzw@163.com 'QQ :171977759 '版 本:V1.0 初版 2012/12/03 ' V1.1 修正了几个正则相关的函数,调整了部分类结构 2013/05/28 ' V1.2 增加属性Caption,可以获取或设置当前标题栏 2013/05/29 ' V1.3 增加了方法Focus,可以激活当前窗口 2013/06/01 ' 增加了方法Left,Top,Width,Height,Move,处理窗口位置等 ' V1.4 增加了窗口位置调整的几个函数 2013/06/04 ' 增加了得到应用程序路径的函数AppName ' 增加了得到应用程序启动参数的函数AppCommandLine ' V1.5 增加了窗口最大最小化,隐藏显示正常的几个函数 2013/06/06 ' 增加了获取控件相关函数是否使用正则的参数UseRegExp默认F ' V1.6 将Left,Top函数改为属性,可获得可设置 2013/06/10 ' V1.7 增加函数:CloseApp 结束进程 2013/06/13 ' 修正了部分跟正则匹配相关的函数 ' 增加函数:GetElementTextByText ' 增加函数:GetElementHwndByText ' V1.8 增加函数:GetWindowByClassName 2013/06/26 ' 增加函数:GetWindowByClassNameEx ' 增加函数:GetWindowByAppName ' 增加私有变量hWnd_ ' 增加属性hWnd,可设置,单设置时候会检查,非法则设置为0 ' 更新GetWindowByTitleEx函数,使之可以选择性支持正则 ' 删除GetWindowByTitleRegExp函数,合并到上面函数 ' 增加SetFocus函数,调用Focus实现,为了是兼容VB习惯 ' 扩了ProcessID、AppPath、AppName、AppCommandLine三个函数,可带参数 ' 网友wwb(wwbing@gmail.com)提供了一些函数和方法属性: ' CheckWindow, Load, WindowState, Visible, hDC, ZOrder ' AlphaBlend, Enabled, Refresh, TransparentColor ' 采纳wwb网友的部分意见,将句柄变量改为hWnd_,但是hWnd作为公共属性 ' V1.9 修正函数:GetMatchHwndFromWindow 正则表达式的错误 2013/08/07 ' 修正函数:GetMatchHwndFromWindow 函数中的一些错误 2014/09/23 ' 增加函数:GetWindowByClassNameEx ' 增加函数:GetWindowByPID 根据PID取窗口句柄 ' 增加函数:GetCaptionByHwnd 根据句柄取得标题 ' 增加函数:SetTop设置窗体置顶,传入参数false则取消 2014/09/24 ' 增加函数:Shake、FadeIn、FadeOut 抖动、淡入、淡出特效 '=======================================================================
功能描述 clsWindow是VB6环境下使用的一个操作外部程序窗口的类,比如得到窗口句柄,得到窗口里某个文本框的内容。非常方便,使用它可以让您脱身于一堆api函数,功能强大使用简单! 这个类楼主很早就开始封装了,原本打算做成类似DOM对象那样,通过一堆getElmentByXXX等方法实现对桌面程序下各个窗口以及里面各个控件对象的自由访问,但是具体要做的工作太多,目前只实现了一部分,期待大家一起加入更新维护。 目前该类封装了绝大部分对windows窗口的常用操作,例如:获取窗口句柄,设置窗口为活动窗口,设置窗口内文本框内容,点击窗口内的某些按钮等。 这个类现在还在一直不断地扩充,功能已经很强大很广泛,使用它可以轻而易举地设置窗口标题栏文字,移动窗体等等。以前要实现这些操作常常需要一大堆api函数,现在只需要一点点代码就可以了,完全让您脱身于api函数的海洋。当然您需要研究每个方法实现原理的话可以看一看源代码。 使用范例(请在v1.9以上测试): 1)关闭腾讯新闻窗口“腾讯网迷你版”。 Dim window As New clsWindow If window.GetWindowByTitle("腾讯网迷你版").hWnd > 0 Then window.CloseWindow '关闭窗口 End If 以上是不是很简洁呢? 20150715更新追加: 最新1.9版本更简洁,一句话解决: w.GetWindowByTitle("腾讯网迷你版").CloseWindow 小伙伴,是不是简洁爆了呢?:) 为了防止程序找不到窗口而一直等待可以改成: w.GetWindowByTitle("腾讯网迷你版",1).CloseWindow (意思为超时等待1秒。默认会耐心等60秒,除非你确定窗口一定有,然后就用上面的。) 2)获取某个打开的记事本里面的内容。假设记事本标题为“测试要求.txt - 记事本”,通过SPY等工具查看得知记事本的文本框类名为:Edit,那么我们编写程序如下: Dim window As New clsWindow If window.GetWindowByTitle("测试要求.txt - 记事本").hWnd > 0 Then MsgBox window.GetElementTextByClassName("Edit") End If 这个看起来也很简单,方法自由还可以使用正则匹配,可以写成下面这样: Dim window As New clsWindow If window.GetWindowByTitleEx("工作任务\.txt.*?", , , True).hWnd > 0 Then MsgBox window.GetElementTextByClassName("Edi", , True) '第三个参数表示是否使用正则,默认为false End If 获取标题那边如果觉得要把标题写完整太麻烦,可以将GetWindowByTitle该车GetWindowByTitleEx然后后面只要写关键字就行啦。看招: Dim window As New clsWindow If window.GetWindowByTitleEx("工作任务").hWnd > 0 Then MsgBox window.GetElementTextByClassName("Edit") End If clsWindow类最新版下载请关注博客: http://blog.csdn.net/sysdzw/article/details/9083313 '============================================================================================== '名 称:windows窗体控制类v2.0 '描 述:一个操作windows窗口的类,可对窗口进行很多常用的操作(类名为clsWindow) '使用范例:Dim window As New clsWindow ' window.GetWindowByTitle "计算器" ' window.closeWindow '编 程:sysdzw 原创开发,如果有需要对模块扩充或更新的话请邮箱发我一份,共同维护 '发布日期:2013/06/01 '博 客:http://blog.163.com/sysdzw ' http://blog.csdn.net/sysdzw 'Email :sysdzw@163.com 'QQ :171977759 '版
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值