QTP的那些事--WebList与正则表达式

简介:

Function IsRegEqual(s_Text, s_Pattern)
  Dim regEx, retVal ' 变量
  Set regEx = New RegExp ' 创建正则表达式 .
  regEx.Pattern = s_Pattern ' 模式
  regEx.IgnoreCase = True
  IsRegEqual = regEx.Test(s_Text)
End Function
Function SelectByText(objWebList,s_Text,b_RegExpression)
  Set obj_Options=objWebList.object.options
  i_Count =obj_Options.length - 1
  For i=0 to i_Count
    If b_RegExpression And IsRegEqual(obj_Options(i).text,"^"+ s_Text) Then
      obj_Options(i).selected=True
      Exit for
    Elseif Lcase(s_text)=Lcase(obj_Options(i).text) then
      obj_Options(i).selected=True
      Exit for
    End If
  Next
End Function
Function SelectByValue(objWebList,s_Value,b_RegExpression)
  Set obj_Options=objWebList.object.options
  i_Count =obj_Options.length - 1
  For i=0 to i_Count
    If b_RegExpression And IsRegEqual(obj_Options(i).value,"^" & s_Value) Then
      obj_Options(i).selected=True
      Exit for
    Elseif Lcase(s_text)=Lcase(obj_Options(i).value) then
      obj_Options(i).selected=True
      Exit for
    End If
  Next
End Function
Function SelectByIndex(objWebList,i_Index)
  objWebList.object.options(i_Index).selected=True
End Function
下面是例子:
SelectByText Browser("Find a Flight: Mercury").Page("Find a Flight: Mercury").WebList("fromPort"),".*ond.*",TRUE
SelectByValue Browser("Find a Flight: Mercury").Page("Find a Flight: Mercury").WebList("fromPort"),"san.*francisco",TRUE
SelectByIndex Browser("Find a Flight: Mercury").Page("Find a Flight: Mercury").WebList("fromPort"),3

 

 

 

第二种方法是:

'Select WinList中选项时支持使用正则表达式
Function RegExpSelect(objWinList, strPattern)
Dim objRegExp, arrAllItems, intIndex
'创建正则表达式对象,设置区分大小写
Set objRegExp = New RegExp
objRegExp.IgnoreCase = False
objRegExp.Pattern = strPattern
'取到WinList下的所有选项的文本,赋值到数组
arrAllItems = Split(objWinList.GetROProperty("all items"), VbLf)
'遍历选项数组
For intIndex = 0 To UBound(arrAllItems)
  '判断表达式是否能匹配当前选项,能匹配则选中之,否则继续循环
  If objRegExp.Test(arrAllItems(intIndex)) Then
   objWinList.Select intIndex
   Reporter.ReportEvent micPass, "RegExpSelect Successful", "Pattern=" & strPattern & "   First Matched Item=" & arrAllItems(intIndex)
   Set objRegExp = Nothing
   Exit Function
  End If
Next
'若遍历完所有选项都不能匹配,则报出不能匹配的错误,写入日志中
Reporter.ReportEvent micFail, "RegExpSelect Failed ", "No Item Matched, Pattern=" & strPattern
End Function

 

以下为QTP中的注册自己的函数:

'将RegExpSelect函数注册到WinList的方法中去
RegisterUserFunc "WinList", "RegExpSelect", "RegExpSelect"
'在WinList中使用RegExpSelect方法,选择第一个能符合表达式的选项
'比如这里希望能自动选一个上午10点到12点间Portland飞往Los Angeles且价格低于180美金的航班
Window("Flight Reservation").Dialog("Flights Table").WinList("From").RegExpSelect "\d+ POR 1[01]:[0-5][0-9] AM LAX \d{2}:\d{2} [AP]M \w+ $1[0-7]\d\.\d{2}"




本文转自hcy's workbench博客园博客,原文链接:http://www.cnblogs.com/alterhu/archive/2012/03/25/2416999.html ,如需转载请自行联系原作者。
目录
相关文章
|
7月前
|
数据采集 搜索推荐 算法
十一、正则表达式详解:掌握强大的文本处理工具(三)
十一、正则表达式详解:掌握强大的文本处理工具(三)
|
9月前
推荐一个正则表达式测试的工具
不会就debug,这个工具支持一步一步走。
53 0
|
机器学习/深度学习 存储 Unix
shell 中匹配正则 字符串处理【整理版】
shell 中匹配正则 字符串处理【整理版】
1348 0
|
Shell Perl
【shell】正则表达式、正则常用命令、流编辑器
文章目录 前言 一、内容概述 二、正则元字符 2.1 基础表达式 2.2 扩展表达式 三、搭配命令
85 0
【shell】正则表达式、正则常用命令、流编辑器
|
Python
PyCharm正则替换
PyCharm正则替换
100 0
|
测试技术 C#
C#正则表达式语法教程
C#语法之正则 本文提供全流程,中文翻译。 Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 —— 高分辨率用户请根据需求调整网页缩放比例) Chinar —— 心分享、心创新...
1818 0

热门文章

最新文章