在QTP的Select方法中使用正規表示式

TIB發表於2010-04-20

方法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中的InStrMid方法

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/

 

 

相關文章