1、為什么要使用框架?
框架是一組自動化測試的規范、測試腳本的基礎代碼,以及測試思想、慣例的集合?捎糜跍p少冗余代碼、提高代碼生產率、提高代碼重用性和可維護性。例如QTestWare 就是QTP 自動化測試框架中的一類。
2 、SAFFRON 簡介
SAFFRON 是針對Web 開發的一個簡單的QTP 測試框架原型,是Adam Gensler 于06 年寫的,需要QTP 9.1 版本以上。完整的SAFFRON 腳本代碼可到以下地址獲。篽ttp://www.itestware.com/ctest/index.php?option=com_content&view=article&id=62:webqtp-saffron&catid=35:testing_is_believing
3 、如何使用SAFFRON?
SAFFRON 框架以外部VBS 文件的形式出現,因此使用方法比較簡單,直接在測試腳本中以資源形式導入即可使用,如圖所示:
導入后,可在"Available Keywords" 視圖中看到SAFFRON 的所有函數,如圖所示:
選中某個函數,拖拽到專家視圖的編輯器中,如圖所示:
后接一個URL 地址,例如http://www.itestware.com ,即可使用SAFFRON 框架中的BrowseTo 函數導航到指定的URL 地址,如下腳本所示:
'BrowseTo(url)
BrowseTo “http://www.itestware.com
4 、SAFFRON 框架代碼剖析
為了深入了解SAFFRON ,以及框架的使用方法,下面我們將分別介紹SAFFRON 中的主要函數,對SAFFRON 代碼進行深入剖析。
4.1 導航到指定URL
SAFFRON 使用名為BrowseTo 函數來負責導航到指定的URL ,如果瀏覽器尚未啟動,則先調用函數Launch 來打開瀏覽器。BrowseTo 函數的定義如下所示:
Public Function BrowseTo (url)
thirdlevel = ""
Report micPass, "Navigate to URL", "Navigating to URL: " & Quote(url)
If initialized Then
Execute GenerateDescription("Browser") & "Navigate " & Quote(url)
Else
Launch "website", url
End If
Reporter.Filter = rfDisableAll
End Function
在腳本中,會判斷是否初始化了瀏覽器,如果有則執行導航動作,導航到指定的URL 。導航動作是執行這行腳本來完成的:
Execute GenerateDescription("Browser") & "Navigate " & Quote(url)
Execute 是一個用于執行指定VBScript 腳本語句的函數,GenerateDescription 函數的定義如下所示:
' 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 hierarchy
Public Function GenerateDescription (levelstr)
l = IndexOf(level, levelstr)
If l >=0 Then
fdesc = level(0) & "(" & Quote(desc(0)) & ")."
If l >= 1 Then
fdesc = fdesc + level(1) & "(" & Quote(desc(1)) & ")."
If 2 >= l Then
If thirdlevel <> "" Then
fdesc = fdesc + level(2) & "(" & Quote(desc(2)) & "," & Quote("name:=" & thirdlevel) & ")."
End If
End If
End If
End If
GenerateDescription = fdesc
End Function
4.2 返回測試對象的描述
GenerateDescription 函數用于返回對象的描述性語句,例如,指定Browser ,則返回如下語句:
"Browser("micclass:=Browser")."
該語句代表了當前瀏覽器對象,并且后面加了個點號,這是為了方便后接"Navigate " 這個瀏覽器對象的導航操作,以及指定的URL 字符串,例如"http://blog.csdn.net/testing_is_believing " 。在Execute 時,其實執行的VBScript 語句如下所示:
Browser("micclass:=Browser").Navigate "http://blog.csdn.net/testing_is_believing "
經過SAFFRON 的框架封裝后,則只需要使用如下語句即可達到同樣的效果:
BrowseTo "http://blog.csdn.net/testing_is_believing "
4.3 啟動瀏覽器
SAFFRON 使用名為BrowseTo 函數來負責導航到指定的URL ,但是如果瀏覽器未啟動,則會先調用函數Launch 來打開瀏覽器。Launch 函數的定義如下所示:
prepares the framework for usage, and configures all internal framework
' 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 launch
' returns - always returns true
Public Function Launch (apptype, val)
If "website" = apptype Then
thirdlevel = ""
Report micPass, "Initialize", "Initializing Framework"
level = split(webLevels, leveldelimiter, -1, 1)
desc = split(webLevelsDesc, leveldescdelimiter, -1, 1)
object = split(objects, objectdelimiter, -1, 1)
objectDescription = split(objectsDescription, objectsDescriptiondelimiter, -1, 1)
CloseBrowsers
Set IE = CreateObject("InternetExplorer.Application")
IE.visible = true
IE.Navigate val
While IE.Busy
wait 1
Wend
End If
initialized = true
Launch = true
End Function
可看到腳本中創建了IE 的COM 對象,然后設置IE 的Visible 屬性設置為Tue ,讓瀏覽器可見,然后調用IE 對象的Navigate 方法導航到指定的URL 。除了創建IE 的COM 對象外,在Launch 函數中還進行框架其它方面的初始化。
4.4 給指定字符串前后加雙引號
在BrowseTo 函數的定義腳本中,調用了一個名為Quote 的函數,該函數的定義如下所示:
' generates a string with embedded/surrounding quotes
Public Function Quote (txt)
Quote = chr(34) & txt & chr(34)
End Function
該函數的作用是給指定的字符串前后加上雙引號字符,例如下面代碼
Msgbox "The message is " & Quote("hello world!")
執行結果顯示如圖所示。
如果我們不使用這個函數,則需要這樣寫我們的代碼來實現同樣的功能:
Msgbox "The message is ""hello world!"""
很明顯,這樣的寫法寫出來的代碼的可讀性和可維護性都差一截。
4.5 點擊鏈接
作為一個針對WEB 應用的腳本框架,除了能啟動瀏覽器導航到指定的頁面外,還需要針對頁面的各種元素進行測試操作,例如鏈接的點擊、按鈕的點擊操作。在SAFFRON 框架中,使用Activate 函數來點擊鏈接、按鈕,其函數定義如下所示:
' 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 control - for a link, it's the text of the link
Public Function Activate (objtype, text)
localDesc = ""
If thirdlevel <> "" Then
localDesc = GenerateDescription(level(2))
Else
localDesc = GenerateDescription(level(1))
End If
AutoSync()
Select Case objtype
Case "Link"
Execute localDesc & GenerateObjectDescription("Link","innertext:=" & text) & "Click"
Report micPass, "Link Activation", "The Link " & Quote(text) & " was clicked."
Case "WebButton"
Execute localDesc & GenerateObjectDescription("WebButton", "value:=" & text) & "Click"
Report micPass, "WebButton Activation", "The WebButton " & Quote(text) & " was clicked."
End Select
End Function
函數首先判斷對象的類型,然后根據對象類型分別處理,如果是鏈接對象,則通過以下語句組合成可執行的VBScript 語句,然后用Execute 函數來執行:
Execute localDesc & GenerateObjectDescription("Link","innertext:=" & text) & "Click"
如果是按鈕對象,則組合成:
Execute localDesc & GenerateObjectDescription("WebButton", "value:=" & text) & "Click"
在這里,調用了GenerateObjectDescription 函數,GenerateObjectDescription 函數的作用與GenerateDescription 函數的作用類似,都是用于返回一個測試對象的描述,不同的是GenerateObjectDescription 函數需要傳入測試對象的描述數組,GenerateObjectDescription 函數的定義如下:
' Generates an object description based upon the object, and objectDescription arrays
' obj - name of the object in the object array
' prop - additional property to help uniquely identify the object
' returns - a string representative of the object description
Public Function GenerateObjectDescription (obj, prop)
i = IndexOf(object, obj)
ndesc = ""
If i <> -1 Then
ndesc = obj & "(" & Quote(objectDescription(i)) & "," & Quote(prop) & ")."
End If
GenerateobjectDescription = ndesc
End Function
有了Activate 函數,我們在寫腳本的時候就可以充分利用,簡化腳本的編寫,例如下面是兩句簡單的腳本,分別點擊頁面上的一個鏈接和一個按鈕:
Activate "Link", "Person"
Activate "WebButton", "Search"
在Activate 函數中,調用了一個名為AutoSync 的函數,該函數的作用與QTP 的Sync 方法是一樣的,只是在外面封裝了一層,函數定義如下所示:
' waits for the web page to finish loading
Public Function AutoSync
Execute GenerateDescription("Browser") & "Sync"
End Function
AutoSync 函數用于等待WEB 頁面加載完成。
4.6 一個小例子
到現在為止,我們可以使用SAFFRON 的Launch 、BrowserTo 和Activate 函數來編寫簡單的腳本啟動瀏覽器,導航到指定的頁面,點擊鏈接和按鈕,例如下面就是一個綜合了這幾個功能的腳本:
' 啟動瀏覽器
Launch "website","http://127.0.0.1:1080 "
' 導航到“http://127.0.0.1:1080/WebTours ”
BrowseTo "http://127.0.0.1:1080/WebTours/ "
' 點擊名為“administration ”的鏈接
Activate "Link","administration"
該腳本調用SAFFRON 框架的Launch 函數啟動IE 瀏覽器,然后導航到“http://127.0.0.1:1080/WebTours ”,點擊如圖所示的頁面中名為“administration ”的鏈接。
腳本的測試結果如圖所示。
4.7 檢查對象是否存在
前面的小例子僅僅實現了啟動瀏覽器、導航、點擊鏈接和按鈕的功能,如果要組成一個完整的測試用例,還缺少一些東西,例如檢查指定的對象是否存在,在SAFFRON 中,用Verify 函數來實現這個功能,Verify 函數的定義如下所示:
' Verify the Existence of an object
' objtype - values should be limited to 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 = false
localDesc = ""
estr = ""
If thirdlevel <> "" Then
localDesc = GenerateDescription(level(2))
Else
localDesc = GenerateDescription(level(1))
End If
AutoSync()
Select Case objtype
Case "Page"
Execute "rval = " & GenerateDescription(level(1)) & "Exist (0)"
If rval Then
Execute "title = " & GenerateDescription(level(1)) & "GetROProperty(" & Quote("title") & ")"
If title = text Then
rval = true
Else
rval = false
End If
End If
Case "CurrentFrame"
If thirdlevel <> "" Then
estr = "rval = " & localDesc
End If
Case "Link"
estr = "rval = " & localDesc & GenerateObjectDescription("Link", "innertext:=" & text)
Case "WebButton"
estr = "rval = " & localDesc & GenerateObjectDescription("WebButton", "value:=" & text)
Case "WebList"
estr = "rval = " & localDesc & GenerateObjectDescription("WebList", "name:=" & text)
Case "WebEdit"
estr = "rval = " & localDesc & GenerateObjectDescription("WebEdit", "name:=" & text)
End Select
If estr <> "" Then
Execute estr + "Exist (0)"
End If
If rval Then
Report micPass, objtype & " Verification", "The " & objtype & " " & Quote(text) & " was verified to exist"
Else
Report micFail, objtype & " Verification", "The " & objtype & " " & Quote(text) & " was not found"
End If
If "True" = rval Then
rval = True
Else
rval = False
End If
Verify = rval
End Function
文章來源于領測軟件測試網 http://www.k11sc111.com/