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 在文本框輸入字符串
在SAFFRON中,可以使用EnterTextIn函數來給輸入框(WebEdit對象)輸入字符串。EnterTextIn函數的定義如下所示:
' Enters text into an edit field
' objname - name of the control -- use Object Spy if you don't know what it is
' text - the text to enter into the control
Public Function EnterTextIn (objname, text)
localDesc = ""
rval = true
If thirdlevel <> "" Then
localDesc = GenerateDescription(level(2))
Else
localDesc = GenerateDescription(level(1))
End If
AutoSync()
localDesc = localdesc & GenerateObjectDescription("WebEdit", "name:=" & objname)
Execute localDesc & "Set (" & Quote(text) & ")"
Report micPass, "Enter Text", "Text: " & Quote(text) & " was entered into " & Quote(objname)
EnterTextIn = rval
End Function
例如,如果我們要在如圖所示的登錄界面中輸入用戶名和密碼,則可以使用SAFFRON的EnterTextIn函數來實現。
測試腳本可以這樣編寫:
' 輸入用戶名
EnterTextIn "username","chennengji"
' 輸入密碼
EnterTextIn "password","123"
4.9 讀取文本框的字符串
在SAFFRON中,可以使用EnterTextIn函數來給輸入框(WebEdit對象)輸入字符串。對應的有一個名為GetTextFrom的函數,用于讀取輸入框和文本列表的字符串,GetTextFrom的定義如下所示:
' Obtains 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 don't know the name
' returns - the text of the control
Public Function GetTextFrom (objtype, objname)
text = ""
localDesc = ""
If thirdlevel <> "" Then
localDesc = GenerateDescription(level(2))
Else
localDesc = GenerateDescription(level(1))
End If
AutoSync()
Select Case objtype
Case "WebEdit"
Execute "text = " & localDesc & GenerateObjectDescription("WebEdit", "name:=" & objname) & "GetROProperty (" & Quote("value") & ")"
Case "WebList"
Execute "text = " & localDesc & GenerateObjectDescription("WebList", "name:=" & objname) & "GetROProperty (" & Quote("value") & ")"
End Select
Report micPass, "Capture Text", "Text: " & Quote(text) & " was captured from the control " & Quote(objname)
GetTextFrom = text
End Function
假設我們需要讀取如圖所示的界面中的“Departure City”和“Arrival City”這兩個文本列表(WebList對象)中的字符串,則可以使用GetTextFrom函數。
測試腳本可以這樣編寫:
' 獲取航班起始城市
DepartureCity = GetTextFrom( "WebList","depart")
' 獲取航班終點城市
ArrivalCity = GetTextFrom( "WebList","arrive")
當然,也可以使用相同的函數來讀取文本框(WebEdit對象)的字符串,例如下面的腳本讀取“NO. of Passengers”對應的文本框中的字符串:
' 獲取乘客數量
PassengerNumber = GetTextFrom( "WebEdit","numPassengers")
文章來源于領測軟件測試網 http://www.kjueaiud.com/