本文主要是介绍在QTP的Select方法中使用正则表达式,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
方法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/
这篇关于在QTP的Select方法中使用正则表达式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!