在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
三种方法的执行效率比较:
参考:
http://relevantcodes.com/regular-expressions-with-select-method-listbox/
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脚本中使用正则表达式,匹配字符串方法
- Python 正则表达式 RE模块的使用方法
- 正则表达式的基本使用方法
- JS使用replace()方法和正则表达式进行字符串的搜索与替换实例
- C#常用正则表达式验证(使用C#3.0扩展方法)(转)
- 正则表达式使用方法
- 使用JS中的exec()方法构造正则表达式验证
- JavaScript中的String对象的常用方法、文本框对象的常用方法和事件、正则表达式的概念、正则表达式的几种构造方式、RegExp对象使用什么方法匹配正则表达式、
- iOS中正则表达式的基本使用方法
- 举例说明在android中使用正则表达式去掉字符串中标点符号及其他一些特殊符号的方法
- 正则表达式学习笔记(一)——使用正则表达式的分析方法
- .net正则表达式大全(.net 的 System.Text.RegularExpressions.Regex.Match()方法使用)
- QTP描述编程中使用正则表达式
- JS使用正则表达式获取小括号、中括号及花括号内容的方法示例
- Python 正则表达式 RE模块的使用方法
- Python使用正则表达式过滤或替换HTML标签的方法详解
- Delphi正则表达式使用方法(TPerlRegEx)
- JS中的正则表达式小小的技巧---TEST方法的使用
- python网络爬虫学习(三)正则表达式的使用之re.match方法
- Java使用正则表达式删除所有HTML标签的方法示例