您的位置:首页 > 编程语言 > Qt开发

在QTP的Select方法中使用正则表达式

2010-04-20 09:15 246 查看
方法1:
Function RegexSelectQTP(Object, sPattern)
       Dim oRegExp, arrAllItems, ix
 
       'Create RegExp Object
       Set oRegExp = New RegExp
       oRegExp.IgnoreCase = False
       oRegExp.Pattern = sPattern
 
       'Split Object's all_items property
       arrAllItems = Split(Object.GetROProperty("all items"), ";")
       For ix = LBound(arrAllItems) To UBound(arrAllItems)
              'If RegExp pattern matches list item, we're done!
              If oRegExp.Test(arrAllItems(ix)) Then
                     Object.Select "#" & ix
                     Set oRegExp = Nothing
                     Exit Function
              End If
       Next
 
       'Select Item #1 by default
       Object.Select "#0"
End Function
RegisterUserFunc "WebList", "RegexSelectQTP", "RegexSelectQTP"
 
使用的例子:
Browser("").Page("").WebList("").RegexSelectQTP "London - Heathrow" 'Select London-Heathrow
Browser("").Page("").WebList("").RegexSelectQTP "London - Heath.*"  'Select London-Heathrow
Browser("").Page("").WebList("").RegexSelectQTP "London - /D+"      'Select London-Heathrow
 
 
方法2:
 
Function RegexSelectDOM(Object, sPattern)
       Dim oRegExp, oOptions, ix
 
       'Create RegExp Object
       Set oRegExp = New RegExp
       oRegExp.IgnoreCase = False
       oRegExp.Pattern = sPattern
 
       'DOM options
       Set oOptions = Object.Object.Options
       For ix = 0 to oOptions.Length - 1
              'If RegExp pattern matches list item, we're done!
              If oRegExp.Test(oOptions(ix).Text) Then
                     Object.Select "#" & ix
                     Set oRegExp = Nothing
                     Exit Function
              End If
       Next
 
       'Select Item #1 by default
       Object.Select "#0"
End Function
RegisterUserFunc "WebList", "RegexSelectDOM", "RegexSelectDOM"
 
使用的例子:
 
 
 
方法3:
这种方法并没有使用正则表达式,而是使用了VBS中的InStr和Mid方法
Public Function VBSSelect(Object, sString)
       Dim sAllItems, varLocation, varEnd, varBeginning
 
       'Retrieve Object's all_items property
       sAllItems = Object.GetROProperty("all items")
 
       'Verify if the supplied string is found in list's all_items property
       varLocation = InStr(1, sAllItems, sString)
       'If found:
       If varLocation > 0 Then
        varEnd = InStr(varLocation, sAllItems, ";")
              If varEnd = 0 Then varEnd = Len(sAllItems) + 1
              varBeginning = InStrRev(sAllItems, ";", varLocation)
              Object.Select "" & Mid(sAllItems, varBeginning + 1, varEnd - varBeginning - 1)
              Exit Function
       End If
 
       'Select Item #1 by default
       Object.Select "#0"
End Function
RegisterUserFunc "WebList", "VBSSelect", "VBSSelect"
 
使用的例子:
Browser("").Page("").WebList("").VBSSelect "London - Heathrow" 'Select London-Heathrow
Browser("").Page("").WebList("").VBSSelect "London - Heath"    'Select London-Heathrow
Browser("").Page("").WebList("").VBSSelect "London - "         'Select London-Heathrow
 
 
三种方法的执行效率比较:
Run Mode
 Normal
 Fast
RegexSelectQTP
0.44 s
0.38 s
RegexSelectDOM
0.45 s
0.40 s
VBSSelect
0.39 s
0.35 s
 
 
 
参考:
http://relevantcodes.com/regular-expressions-with-select-method-listbox/
 
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息