透析QTP自動化測試框架SAFFRON(3)

發表于:2014-11-25來源:uml.org.cn作者:陳能技點擊數: 標簽:
到現在為止,我們可以使用SAFFRON的Launch、BrowserTo和Activate函數來編寫簡單的腳本啟動瀏覽器,導航到指定的頁面,點擊鏈接和按鈕,例如下面就是一個綜合

到現在為止,我們可以使用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

由于判斷不同對象的存在需要采用不同的屬性,因此Verify函數中對不同的對象類型進行判斷、分別處理。例如,對于 Link類型的對象,用innertext屬性,對于WebButton,則采用value屬性,但是最后都需要組合成一條語句,后接“Exist”,通過Execute方法執行這個語句,從而實現對象是否存在的判斷。

對于頁面對象(Page)的存在性檢查有點不一樣,采用的是以下腳本:
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

通過GetROProperty方法獲取當前頁面的title屬性,然后與傳入的“text”參數進行比較,如果相等,則認為頁面對象是存在的。

在測試腳本中可以這樣使用Verify函數:
' 啟動瀏覽器
Launch "website","http://127.0.0.1:1080"
' 導航到“http://127.0.0.1:1080/WebTours”
BrowseTo "http://127.0.0.1:1080/WebTours/"

If Verify ("Link","administration")= False then
Reporter.ReportEvent micFail,"檢查鏈接","鏈接不存在"
Else
' 點擊名為“administration”的鏈接
Activate "Link","administration"
End IF

腳本中先用Verify檢查名為“administration”的鏈接對象是否存在,如果不存在則提示錯誤,如果存在則進一步調用Activate函數點擊鏈接。

4.8 在文本框輸入字符串

原文轉自:http://www.uml.org.cn/Test/200810108.asp

国产97人人超碰caoprom_尤物国产在线一区手机播放_精品国产一区二区三_色天使久久综合给合久久97