QTP測試多個瀏覽器視窗的解決方案

TIB發表於2010-03-19

 

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類。主要函式有:

AddUsingTitleUses 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

AddLastOpenUses 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

 

 

相關文章