relevantcodes.com的《QTP: Working with Multiple Browser Applications》这篇文章介绍了如何测试多个浏览器窗口的情况:
http://relevantcodes.com/qtp-working-with-multiple-browser-applications-revised/
解决办法的关键是根据什么来区别出不同的浏览器窗口并保存其句柄。文章介绍了2类方法:
1、使用标题来识别出浏览器窗口并保存浏览器窗口句柄
2、最近打开的浏览器窗口拥有的CreationTime属性最大,可以据此来区别不同的浏览器窗口,并分别保存它们的句柄
把这些识别并保存浏览器窗口句柄的代码封装在clsBrowser.cls文件中的colBrowser类。主要函数有:
AddUsingTitle:Uses the "AddBrowser" method to add browsers to the collection using their Title Property
AddUsingCreationTime :Uses the "AddBrowser" method to add browsers to the collection using their CreationTime Property
AddLastOpen:Uses the "AddBrowser" method to add the last (most recent) open browser
Note: The last open browser always has the greatest CreationTime
使用例子如下所示(BrowserObject对象就是colBrowser类产生的对象):
'==================================
' Add Using Title
'==================================
SystemUtil.Run "iexplore.exe", "http://newtours.demoaut.com", "", "", 3 : Wait(4)
'Add the browser above using its title, and with the name DemoAUT
BrowserObject.AddUsingTitle "DemoAUT", ".*Mercury Tours.*"
SystemUtil.Run "iexplore.exe", "http://blog.csdn.net/testing_is_believing", "", "", 3 : Wait(4)
'Add the browser above using its title, and with the name RelevantCodes
BrowserObject.AddUsingTitle "MyBlog", "实用性测试(Pragmatistic Testing) - CSDN博客"
With BrowserObject.Name("DemoAUT")
.WebEdit("name:=userName").Set "test"
.WebEdit("name:=password").Set "test"
.Image("name:=login").Click
.Sync
If .WebList("name:=fromPort").Exist(10) Then
.WebList("name:=fromPort").Select "Frankfurt"
.WebList("name:=fromMonth").Select "December"
.WebList("name:=toPort").Select "Paris"
.WebList("name:=toMonth").Select "December"
.WebRadioGroup("name:=servClass").Select "#1"
.WebList("name:=airline").Select "Unified Airlines"
.Image("name:=findFlights").Click
End If
End with
With BrowserObject.Name("MyBlog")
.Link("text:=《QTP自动化测试实践》", "index:=").Click
End with
With BrowserObject
.Name("DemoAUT").Close
.Name("MyBlog").Close
End with
'==================================
' Add Last Open
'==================================
SystemUtil.Run "iexplore.exe", "http://newtours.demoaut.com", "", "", 3 : Wait(4)
'Add the above browser using AddLastOpen
BrowserObject.AddLastOpen "DemoAUT"
SystemUtil.Run "iexplore.exe", "http://blog.csdn.net/testing_is_believing", "", "", 3 : Wait(4)
'Add the above browser using AddLastOpen
BrowserObject.AddLastOpen "MyBlog"
With BrowserObject.Name("DemoAUT")
.WebEdit("name:=userName").Set "test"
.WebEdit("name:=password").Set "test"
.Image("name:=login").Click
.Sync
If .WebList("name:=fromPort").Exist(10) Then
.WebList("name:=fromPort").Select "Frankfurt"
.WebList("name:=fromMonth").Select "December"
.WebList("name:=toPort").Select "Paris"
.WebList("name:=toMonth").Select "December"
.WebRadioGroup("name:=servClass").Select "#1"
.WebList("name:=airline").Select "Unified Airlines"
.Image("name:=findFlights").Click
End If
End with
With BrowserObject.Name("MyBlog")
.Link("text:=《QTP自动化测试实践》", "index:=").Click
End with
With BrowserObject
.Name("DemoAUT").Close
.Name("MyBlog").Close
End with
'==================================
' Add Using CreationTime
'==================================
SystemUtil.Run "iexplore.exe", "http://newtours.demoaut.com", "", "", 3 : Wait(4)
'Add the above browser using CreationTime 0, assuming no other browsers are open
BrowserObject.AddUsingCreationTime "DemoAUT", 0
SystemUtil.Run "iexplore.exe", "http://blog.csdn.net/testing_is_believing", "", "", 3 : Wait(4)
'Add the above browser using CreationTime 1, assuming no other browsers are open prior to above
BrowserObject.AddUsingCreationTime "MyBlog", 1
With BrowserObject.Name("DemoAUT")
.WebEdit("name:=userName").Set "test"
.WebEdit("name:=password").Set "test"
.Image("name:=login").Click
.Sync
If .WebList("name:=fromPort").Exist(10) Then
.WebList("name:=fromPort").Select "Frankfurt"
.WebList("name:=fromMonth").Select "December"
.WebList("name:=toPort").Select "Paris"
.WebList("name:=toMonth").Select "December"
.WebRadioGroup("name:=servClass").Select "#1"
.WebList("name:=airline").Select "Unified Airlines"
.Image("name:=findFlights").Click
End If
End with
'Added Index - November 06, 2009 - Credits: Clive Farrington
With BrowserObject.Name("MyBlog")
.Link("text:=《QTP自动化测试实践》", "index:=").Click
End with
With BrowserObject
.Name("DemoAUT").Close
.Name("MyBlog").Close
End with