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

本文介绍三种在QuickTest Professional (QTP)中使用不同方法实现列表框元素选择的自定义函数,包括利用正则表达式进行精确匹配及模糊匹配的方法1和方法2,以及采用VBS脚本InStr和Mid方法进行字符串匹配的选择方法3,并对比了它们的执行效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

方法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/

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值