在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")
4.10选择列表中的一项
在SAFFRON中,可以使用SelectFromList函数从下拉框列表(WebList对象)中选择指定的一项。SelectFromList的定义如下所示:
' Selects a specific value from a listbox, or combobox
' objname - name of the control -- use Object Spy if you don't know the name property
' text - the item in the combobox to select
Public Function SelectFromList (objname, text)
localDesc = ""
rv = ""
rval = false
If thirdlevel <> "" Then
localDesc = GenerateDescription(level(2))
Else
localDesc = GenerateDescription(level(1))
End If
AutoSync()
localDesc = localdesc & GenerateObjectDescription("WebList", "name:=" & objname)
Execute "cnt = " & localDesc & "GetROProperty(" & Quote("items count") & ")"
For i = 1 to cnt
Execute "rv = " & localDesc & "GetItem (" & i & ")"
If rv = text Then
rval = true
End If
Next
If rval Then
Execute localDesc & "Select " & Quote(text)
End If
If rval Then
Report micPass, "WebList Selection", "The WebList item " & Quote(text) & " was selected."
Else
Report micFail, "WebList Selection", "The WebList item " & Quote(text) & " was NOT found."
End If
SelectFromList = rval
End Function
假设我们需要从如图所示的界面中的“Departure City”的下拉框中选择其中一项,则可使用SelectFromList函数来实现。
测试脚本可以这样写:
' 选择航班起始城市为"San Francisco"
SelectFromList "depart","San Francisco"
4.11 关闭浏览器
原文转自:http://www.uml.org.cn/Test/200810108.asp