透析QTP自動化測試框架SAFFRON_第1頁
透析QTP自動化測試框架SAFFRON_第2頁
透析QTP自動化測試框架SAFFRON_第3頁
透析QTP自動化測試框架SAFFRON_第4頁
透析QTP自動化測試框架SAFFRON_第5頁
已閱讀5頁,還剩21頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1、1、為什么要使用框架?框架是一組自動化測試的規(guī)范、 測試腳本的基礎(chǔ)代碼,以及測試思想、慣例 的集合。可用于減少冗余代碼、提高代碼生產(chǎn)率、提高代碼重用性和可維護性。例如QTestWare就是QTP自動化測試框架中的一類。2、SAFFRON介SAFFRON針對 Webff發(fā)的一個簡單的 QTPW試框架原型,是AdamGensler 于06年寫的,需要QTP 9.1版本以上。完整的SAFFRON本代碼可到以下地址 獲?。?cle&id=62:webqtp-saffron&catid=35:testing_is_believing3、如何使用SAFFRON?SAFFRON架以外部VBS

2、i:件的形式出現(xiàn),因此使用方法比較簡單,直接在 測試腳本中以資源形式導(dǎo)入即可使用,如圖所示:導(dǎo)入后,可在"Available Keywords”視圖中看到SAFFRON所有函數(shù),如圖 所示:選中某個函數(shù),拖拽到專家視圖的編輯器中,如圖所示:后接一個 URL地址,例如 ,即可使用SAFFRON框架中的BrowseTo函數(shù)導(dǎo)航到指定的URL地址,如下腳本所示:UR.lcthii 口OtVjj E*r W ft* ETId#yrtf1 F&±rd » 用5 日立UHwTrrtfX AltlVlAl jh* ka oSjat a - a/ / O ti ilrwM

3、T i- jT EnfoTstli ;沁yW+EBHl1 /*#|!打ij*if aMlf$ Xis£«<f / Uwk J iQtjftU / Wwt / 50x5+41、 / 九wMrka同。 i必 /工 Ltl f uac-li. q*i" Twl Sii,;DJ XJFll丁于二 rlg,?“ 二:':1II H_hi T«,n遞$BrowseTo(url)BrowseTo " 4、SAFFRON架代碼剖析為了深入了解SAFFRO用及框架的使用方法,下面我們將分別介紹SAFFRON 中的主要函數(shù),對SAFFRON碼進行深入

4、剖析。4.1導(dǎo)航到指定URLSAFFRON用名為BrowseTo函數(shù)來負責導(dǎo)航到指定的URL如果瀏覽器尚未 啟動,則先調(diào)用函數(shù)Launch來打開瀏覽器。BrowseTo函數(shù)的定義如下所示:Public Function BrowseTo (url)thirdlevel =""Report micPass, "Navigate to URL", "Navigating to URL: " & Quote (url)If initialized ThenExecute GenerateDescription("Brows

5、er")& "Navigate "& Quote(url)ElseLaunch "website", urlEnd IfReporter.Filter = rfDisableAllEnd Function在腳本中,會判斷是否初始化了瀏覽器,如果有則執(zhí)行導(dǎo)航動作,導(dǎo)航到指 定的URL導(dǎo)航動作是執(zhí)行這行腳本來完成的:Execute GenerateDescription("Browser") & "Navigate " & Quote(url)Execute是一個用于執(zhí)行指定

6、VBScript腳本語句的函數(shù),GenerateDescription函數(shù)的定義如下所示:'Generates a generic description based up on the "level" viarable'levelstr - will be one of the values that is in the level array'returns - string representative of the object hierarchyPublic Function GenerateDescription (levelstr)l

7、= IndexOf(level, levelstr)If l >=0 Thenfdesc = level(0) & "(" & Quote(desc(0) & ")."If l >= 1 Thenfdesc = fdesc + level(1) & "(" & Quote(desc(1) & ")."If 2 >= l ThenIf thirdlevel <> "" Thenfdesc = fdesc + level(

8、2) & "(" & Quote(desc(2) & "," & Quote("na me:=" & thirdlevel) & ")."End IfEnd IfEnd IfEnd IfGenerateDescription = fdescEnd Function4.2返回測試對象的描述GenerateDescription函數(shù)用于返回對象的描述性語句,例如,指定Browser,則返回如下語句:"Browser("micclass:=Browser

9、")."該語句代表了當前瀏覽器對象,并且后面加了個點號,這是為了方便后接 "Navigate "這個瀏覽器對象的導(dǎo)航操作,以及指定的URL字符串,例如"。在 Execute 時,其實執(zhí)行的VBScript語句如下所示:Browser("micclass:=Browser").Navigate "經(jīng)過SAFFRON框架封裝后,則只需要使用如下語句即可達到同樣的效果:BrowseTo "4.3啟動瀏覽器SAFFRON用名為BrowseTo函數(shù)來負責導(dǎo)航到指定的URL但是如果瀏覽器 未啟動,則會先調(diào)用函數(shù)Lau

10、nch來打開瀏覽器。Launch函數(shù)的定義如下所示:prepares the framework for usage, and configures all internal frame work'variables and structures'apptype - used to launch different types of applications based'upon different technologies - currently there is only web'val - string that represents what to la

11、unch'returns - always returns truePublic Function Launch (apptype, val)If "website" = apptype Thenthirdlevel =""Report micPass, "Initialize", "Initializing Framework"level = split(webLevels, leveldelimiter, -1, 1)desc = split(webLevelsDesc, leveldescdelimi

12、ter, -1, 1)object = split(objects, objectdelimiter, -1, 1)objectDescription = split(objectsDescription, objectsDescriptiond elimiter, -1, 1)CloseBrowsersSet IE = CreateObject("InternetExplorer.Application")IE.visible = truelE.Navigate valWhile lE.Busywait 1WendEnd Ifinitialized = trueLaunc

13、h = trueEnd Function可看到腳本中創(chuàng)建了 IE的COM寸象,然后設(shè)置IE的Visible屬性設(shè)置為 Tue,讓瀏覽器可見,然后調(diào)用IE對象的Navigate方法導(dǎo)航到指定的URL除 了創(chuàng)建IE的COM寸象外,在Launch函數(shù)中還進行框架其它方面的初始化。4.4給指定字符串前后加雙引號在BrowseTo函數(shù)的定義腳本中,調(diào)用了一個名為Quote的函數(shù),該函數(shù)的定義如下所示:'generates a string with embedded/surrounding quotesPublic Function Quote (txt)Quote = chr(34) &

14、; txt & chr(34)End Function該函數(shù)的作用是給指定的字符串前后加上雙引號字符,例如下面代碼Msgbox "The message is " & Quote("hello world!")執(zhí)行結(jié)果顯示如圖所示。ThemsnovT' hdlo world!"如果我們不使用這個函數(shù),則需要這樣寫我們的代碼來實現(xiàn)同樣的功能:Msgbox "The message is ""hello world!"""很明顯,這樣的寫法寫出來的代碼的可讀性和可維

15、護性都差一截。4.5點擊鏈接作為一個針對WEBZ用的腳本框架,除了能啟動瀏覽器導(dǎo)航到指定的頁面外, 還需要針對頁面的各種元素進行測試操作,例如鏈接的點擊、按鈕的點擊操作。 在SAFFRON架中,使用Activate函數(shù)來點擊鏈接、按鈕,其函數(shù)定義如下所 示:'Activates an object based upon its object type'objtype - the type of object should be limited to values in the object array'text - identifying text for the co

16、ntrol - for a link, it's thetext of the linkPublic Function Activate (objtype, text )localDesc =""If thirdlevel <> "" ThenlocalDesc = GenerateDescription(level (2)ElselocalDesc = GenerateDescription(level (1)End IfAutoSync()Select Case objtypeCase "Link"Execut

17、e localDesc & GenerateObjectDescription("Link","innertext: "& text ) & "Click"Report micPass, "Link Activation", "The Link "& Quote( text ) & "was clicked."Case "WebButton"Execute localDesc & GenerateObject

18、Description("WebButton", "value: ="& text ) & "Click"Report micPass, "WebButton Activation", "The WebButton "& Quote(text ) & " was clicked."End SelectEnd Function函數(shù)首先判斷對象的類型,然后根據(jù)對象類型分別處理,如果是鏈接對象, 則通過以下語句組合成可執(zhí)行的VBScript語句,然后

19、用Execute函數(shù)來執(zhí)行:Execute localDesc & GenerateObjectDescription("Link","innertext:=" & text) & "Click"如果是按鈕對象,則組合成:Execute localDesc & GenerateObjectDescription("WebButton", "value:=" & text) & "Click"在這里,調(diào)用了 GenerateObj

20、ectDescription 函數(shù),GenerateObjectDescription函數(shù)的作用與 GenerateDescription函數(shù)的作用類似,都是用于返回一個測試對象的描述,不同的是 GenerateObjectDescription 函數(shù)需要傳入測試對象的描述數(shù)組,GenerateObjectDescription函數(shù)的定義如'Generates an object description based upon the object, and object Description arrays'obj - name of the object in the obj

21、ect array'prop - additional property to help uniquely identify the object'returns - a string representative of the object descriptionPublic Function GenerateObjectDescription (obj, prop)i = IndexOf(object, obj)ndesc =""If i <> -1 Thenndesc = obj & "("& Quote

22、(objectDescription(i) & ","& Quote(prop) & ")."End IfGenerateobjectDescription = ndescEnd Function有了 Activate函數(shù),我們在寫腳本的時候就可以充分利用,簡化腳本的編 寫,例如下面是兩句簡單的腳本,分別點擊頁面上的一個鏈接和一個按鈕:Activate "Link", "Person"Activate "WebButton", "Search"在Act

23、ivate函數(shù)中,調(diào)用了一個名為 AutoSync的函數(shù),該函數(shù)的作用與 QTP的Sync方法是一樣的,只是在外面封裝了一層,函數(shù)定義如下所示:'waits for the web page to finish loadingPublic Function AutoSyncExecute GenerateDescription("Browser") & "Sync"End FunctionAutoSync函數(shù)用于等待WEBS面加載完成。4.6 一個小例子到現(xiàn)在為止,我們可以使用 SAFFRON Launch、BrowserTo ft Ac

24、tivate 函 數(shù)來編寫簡單的腳本啟動瀏覽器, 導(dǎo)航到指定的頁面,點擊鏈接和按鈕,例如下 面就是一個綜合了這幾個功能的腳本:啟動瀏覽器Launch "website",":1080"'導(dǎo)航到 :1080/WebToursBrowseTo ":1080/WebTours/"點擊名為“ administration ”的鏈接Activate "Link","administration”該腳本調(diào)用SAFFRON

25、架的Launch函數(shù)啟動IE瀏覽器,然后導(dǎo)航到http:/127.0.O1:1080/WebTours,點擊如圖所示的頁面中名為"administration"的鏈接。腳本的測試結(jié)果如圖所示4.7 檢查對象是否存在object前面的小例子僅僅實現(xiàn)了啟動瀏覽器、 導(dǎo)航、點擊鏈接和按鈕的功能,如果 要組成一個完整的測試用例,還缺少一些東西,例如檢查指定的對象是否存在, 在SAFFRON,用Verify函數(shù)來實現(xiàn)這個功能,Verify函數(shù)的定義如下所示:Verify the Existence of anobjtype - values should be limited to

26、values in the object array'text - multi-purpose argument that indicates what to verify '-for a link, or button, it's the text of the control '-for a list, it's the name of the control '-for a frame, it's the name of the frame Public Function Verify (objtype, text) rval =

27、false localDesc ="" estr ="" If thirdlevel <>"" ThenlocalDesc = GenerateDescription(level(2)Else localDesc = GenerateDescription(level(1)End IfAutoSync()Select Case objtype Case "Page"Execute "rval = "& GenerateDescription(level( 1) & &

28、quot;Exist (0)”If rval ThenExecute "title = "& GenerateDescription(level( 1) & "GetROProperty("& Quote( "title" ) & ")"rval = trueElse rval = falseEnd IfEnd IfCase ”CurrentFrame""Then& localDescIf thirdlevel <> estr = "

29、rval = End IfLink" , "innCase "Link"estr = "rval = "& localDesc & GenerateObjectDescription(ertext尸"& text)Case "WebButton"estr = "rval = "& localDesc & GenerateObjectDescription("value尸"& text)Case "WebLis

30、t"estr = "rval = "& localDesc & GenerateObjectDescription(name:=" & text)Case "WebEdit"estr = "rval = "& localDesc & GenerateObjectDescription(name:=" & text)WebButton"WebList","WebEdit",End SelectExecute estr +

31、 "Exist (0)”End IfIf rval ThenReport micPass, objtype &" Verification" , "The " & objtype &" " & Quote(text) &" was verified to exist"ElseReport micFail, objtype &" Verification" , "The " & objtype &&quo

32、t; " & Quote(text) &" was not found"End IfIf "True" = rval Thenrval = TrueElserval = FalseEnd IfVerify = rvalEnd Function由于判斷不同對象的存在需要采用不同的屬性,因此 Verify函數(shù)中對不同 的對象類型進行判斷、分別處理。例如,對于 Link類型的對象,用innertext 屬性,對于 WebButton,則采用value屬性,但是最后都需要組合成一條語句, 后接"Exist",通過Ex

33、ecute方法執(zhí)行這個語句,從而實現(xiàn)對象是否存在的判斷。對于頁面對象(Page)的存在性檢查有點不一樣,采用的是以下腳本:Case "Page"Execute "rval = "& GenerateDescription(level( 1) & "Exist (0)"Execute "title = "& GenerateDescription(level( 1) & "GetROProperty(" & Quote( "title"

34、) & ")"If title = text Thenrval = trueElserval = falseEnd IfEnd If通過GetROProperty方法獲取當前頁面的title 屬性,然后與彳入的"text" 參數(shù)進行比較,如果相等,則認為頁面對象是存在的。在測試腳本中可以這樣使用 Verify函數(shù): 啟動瀏覽器Launch "website" , ":1080"'導(dǎo)航至U " :1080/WebTours”Brow

35、seTo ":1080/WebTours/"If Verify ( "Link" , "administration" )= False then Reporter.ReportEvent micFail,"檢查鏈接","鏈接不存在"Else '點擊名為" administration ”的鏈接 Activate "Link" , "administration" End IF 腳本中先用Verify檢查名為

36、"administration"的鏈接對象是否存在,如果不存在則提示錯誤,如果存在則進一步調(diào)用Activate函數(shù)點擊鏈接。4.8 在文本框輸入字符串在SAFFRON,可以使用EnterTextIn 函數(shù)來給輸入框(WebEdit對象)輸入 字符串。EnterTextIn函數(shù)的定義如下所示:'Enters text into an edit field'objname - name of the control - use Object Spy if you don't kn ow what it is'text - the text to

37、enter into the controlPublic Function EnterTextIn (objname, text)localDesc =""rval = trueIf thirdlevel <>"" ThenlocalDesc = GenerateDescription(level(2)ElselocalDesc = GenerateDescription(level(1)End IfAutoSync()localDesc = localdesc & GenerateObjectDescription( "W

38、ebEdit" , "nam e:=" & objname)Execute localDesc & "Set (" & Quote(text) &")"Report micPass, "Enter Text" , "Text: "& Quote(text) &" was entered into "& Quote(objname)EnterTextIn = rvalEnd Function例如,如果我們要在如圖

39、所示的登錄界面中輸入用戶名和密碼,則可以使用SAFFRON EnterTextIn 函數(shù)來實現(xiàn)。測試腳本可以這樣編寫:輸入用戶名EnterTextIn "username","chennengji”輸入密碼EnterTextIn "password","123"4.9 讀取文本框的字符串在SAFFRON,可以使用EnterTextIn 函數(shù)來給輸入框(WebEdit對象)輸入 字符串。對應(yīng)的有一個名為GetTextFrom的函數(shù),用于讀取輸入框和文本列表的 字符串,GetTextFrom的定義如下所示:'Obtain

40、s text from a control'objtype - is the type of control the get the text from'objname - is the name of the control - use Object Spy if you d on't know the name'returns - the text of the controlPublic Function GetTextFrom (objtype, objname)text =localDesc =""If thirdlevel <

41、;>"" ThenlocalDesc = GenerateDescription(level(2)ElselocalDesc = GenerateDescription(level(1)End IfAutoSync()Select Case objtypeCase "WebEdit"Execute "text = "& localDesc & GenerateObjectDescription( "WebEdit" , "name:=" & objname) &am

42、p; "GetROProperty (" & Quote( "value" ) & ")"Case "WebList"Execute "text = "& localDesc & GenerateObjectDescription( "WebList" , "name:=" & objname) & "GetROProperty (" & Quote( "value&qu

43、ot; ) & ")"End SelectReport micPass, "Capture Text" , "Text: "& Quote(text) &" was captured from the control "& Quote(objname)GetTextFrom = textEnd Function假設(shè)我們需要讀取如圖所示的界面中的"Departure City"和"Arrival City" 這兩個文本列表(WebList對象)中

44、的字符串,則可以使用 GetTextFrom函數(shù)。測試腳本可以這樣編寫:獲取航班起始城市DepartureCity = GetTextFrom( "WebList","depart")獲取航班終點城市ArrivalCity = GetTextFrom( "WebList","arrive")當然,也可以使用相同的函數(shù)來讀取文本框(WebEdit對象)的字符串,例如 下面的腳本讀取"NO. of Passengers"對應(yīng)的文本框中的字符串:'獲取乘客數(shù)量PassengerNumber =

45、 GetTextFrom( "WebEdit","numPassengers")4.10 選擇列表中的一項在SAFFRON,可以使用SelectFromList函數(shù)從下拉框列表(WebList對象) 中選擇指定的一項。SelectFromList的定義如下所示:'Selects a specific value from a listbox, or combobox'objname - name of the control - use Object Spy if you don't kn ow the name property

46、'text - the item in the combobox to selectPublic Function SelectFromList (objname, text)localDesc =""rv =""rval = falseIf thirdlevel <>"" ThenlocalDesc = GenerateDescription(level(2)Else localDesc = GenerateDescription(level(1)End If AutoSync() localDesc = loc

47、aldesc & GenerateObjectDescription( "WebList" , "nam e:=" & objname)Execute "cnt = "& localDesc & "GetROProperty(" & Quote( "items count" ) & ")"For i =1 to cntExecute "rv = "& localDesc & "Get

48、Item (" & i & ")"If rv = text Then rval = true End If Next If rval ThenExecute localDesc & "Select " & Quote(text)End IfIf rval ThenReport micPass, "WebList Selection" , "The WebList item "& Quote(text) &" was selected."E

49、lseReport micFail, "WebList Selection" , "The WebList item "& Quote(text) &" was NOT found."End IfSelectFromList = rvalEnd Function假設(shè)我們需要從如圖所示的界面中的"Departure City"的下拉框中選擇其中一項,則可使用SelectFromList函數(shù)來實現(xiàn)。測試腳本可以這樣寫:'選擇航班起始城市為"San Francisco"Sele

50、ctFromList "depart","San Francisco4.11關(guān)閉瀏覽器We頃面測試的最后一個步驟一般都是關(guān)閉瀏覽器,在 SAFFRON,也把這 個過程封裝成了一個名為"CloseBrowsers"的函數(shù),該函數(shù)的定義如下:'close all opened browsersPublic Function CloseBrowsersIf Browser( "micclass:=Browser" ).Exist (0) ThenBrowser( "micclass:=Browser"

51、).CloseEnd IfWhile Browser( "micclass:=Browser" , "index:=1" ).Exist (0)Browser( "index:=1" ).CloseWendIf Browser( "micclass:=Browser" ).Exist (0) ThenBrowser( "micclass:=Browser" ).CloseEnd IfEnd FunctionCloseBrowsers函數(shù)會把當前所有打開的瀏覽器都關(guān)閉,腳本中采用描述性編程的方式獲

52、取所有對象類型為“Browser”的測試對象,然后循環(huán)逐個關(guān)閉所有 這種類型的測試對象。5、對SAFFRON架進行擴展SAFFRON一個基本的框架,它封裝了瀏覽器的相關(guān)測試操作、封裝了一些 基本對象的測試操作,例如 Link、WebButton、WebEdit、WebList等控件,可用 于基本的WEEK面的測試,并且簡化了測試腳本的編寫,可以讓代碼的可讀性和 可維護性得到增強。但是SAFFRON僅是一個基礎(chǔ)框架,我們還需要進一步地對其擴展才能應(yīng)用 到實際的WEB1動化測試項目中去,例如擴展對更多的控件的支持。下面是一個 對Activate函數(shù)擴展Image對象的點擊操作的過程:(1)首先打開

53、SAFFRON架的VBS文件,找到開頭的變量定義處,添加Image 對象,讓框架可以識別和支持Image對象:'擴展對Image對象的支持objects = "Link|WebButton|WebList|WebEdit|Image"objectsDescription ="micclass:=Link|micclass:=WebButton|micclass:=WebList|micclass:=WebEd it|micclass:=Image”(2)修改Activate方法,添加對Image對象的Click操作的支持,腳本修改 成如下所示:'A

54、ctivates an object based upon its object type'objtype - the type of object should be limited to values in the object array'text - identifying text for the control - for a link, it's the text of the linkPublic Function Activate (objtype, text)localDesc =""If thirdlevel <>"" ThenlocalDesc = GenerateDescription(level(2)ElselocalDesc = GenerateDescription(level(1)End IfAutoSync()Select Case objtypeCase "Link"Execute localDesc & GenerateObjectDescription( "Link" , "innertext:= "& text) & "Click"Repor

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論