在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 一個小例子
原文轉自:http://www.uml.org.cn/Test/200810108.asp