|
'正则表达式在QTP中的应用,通过使用以下的代码重写WinList控件的Select方法来实现
Function SelectRegExp(Obj,patrn,Button,Offset)
Dim NumOfItems,i, CurrentValue, regEx, ItemToSelect, oldFilter
Set regEx = New RegExp '初始化正则表达式
regEx.Pattern = patrn
regEx.IgnoreCase = False '区分大小写
oldFilter = Reporter.Filter '保存默认设置
Reporter.Filter = 2 ' 仅发送设置
ItemToSelect = -1 '获得测试对象的items count属性
NumOfItems = obj.GetRoProperty("items count") '为什么需要获得测试对象的items count属性?
'下面这个循环实现的什么功能?
For i = 0 To NumOfItems-1
CurrentValue = obj.GetItem(i)
If regEx.Test(CurrentValue) Then
If (ItemToSelect <> -1) Then
SelectRegExp = -1 ' 表示匹配项不唯一
Reporter.Filter = oldFilter
Exit Funtion
End If
ItemToSelect = i
End If
Next
Reporter.Filter = oldFilter '重置默认设置
'做出选择的动作
If(ItemToSelect >= 0) Then
SelectRegExp = obj.Select(ItemToSelect, Button, Offset)
Else
SelectRegExp = -1
End If
End Function
‘重写WinList控件的Select方法
RegisterUserFunc "WinList", "Select", "SelectRegExp"
Window("Flight Reservation").Dialog("Flights Table").Activate
Window("Flight Reservation").Dialong("Flights Table").WinList("From").Select "15797.*" |
|