qtp使用技巧之正则表达式

上一篇 / 下一篇  2012-10-30 09:10:12 / 个人分类:行走在测试的途中

qtp使用技巧之正则表达式(一)

如下实例:
Browser("title:=石家庄市区域卫生信息平台").Page("title:=石家庄市区域卫生信息平台").Link("text:=[新用户注册]")

[]这个QTP貌似支持的不太好.
我们可以用正则表达式来匹配
Browser("title:=石家庄市区域卫生信息平台").Page("title:=石家庄市区域卫生信息平台").Link("text:=.*新用户注册.*")
.可以代表除换行外任意字符,*表示重复任意次,我想到这大家都知道应该怎么去学习了.主要是学习正则表达式


正则表达式的另外一个用法
用于Description的定义
实例:如何将页面中类似于这样的一些link找出来:
1.name1
2.text2
3.edit3
...
这种时候用上两个技术:一个是Description, 一个是ChildObjects方法
代码如下:

Set bjDesc=Description.Create() '声明一个Description对象
objDesc("micclass").value="Link" '定义这个Descriptio对象的micclass的属性为link
objDesc("text").value="^\d+\..*\d$" '这一句就用上了正则表达式

^匹配字符串开始,

\d匹配任意数字,

+前面的字符重复至少一次,

\.因为点在正则表达式是有意义的,而\.就表示点本身,.匹配除换行外任意字符,*表示重复任意次,

那么这个正则表达式其实就是匹配以上据说的那些link的形式。

Set arrLink=Browser("title:=石家庄市区域卫生信息平台").Page("title:=石家庄市区域卫生信息平台").ChildObjects(objDesc)

'ChildObjects方法返回的是子对象数组,这里是将符合Description定义的那些了对象返回,那些需要的link就全部找到了。



 

qtp使用技巧之正则表达式(二)

正则另外一个很强大的功能:
QTP是VBS写的.VBS的有一个特别强大的对象就是RegExp对象
介绍一下RegExp基本信息:
Global属性:代表全局匹配
IgnoreCase属性:大小写忽略
Pattern属性:正则表达式
Execute方法:匹配搜索,返回匹配结果集合
Replace方法:匹配代替,返回替代匹配结果
Test方法:测试匹配,返回布尔类型
 
下面是总结了网上和项目中经常用到的一些方法实例:
 
'判断正则匹配是否正确
'msgbox (IsRegMatch("a123","http://www.a123.com"))
Function IsRegMatch(patrn,str)
   Dim regEx
   Set regEx = New RegExp
   regEx.Pattern = patrn
   regEx.IgnoreCase = False
   IsRegMatch = regEx.Test(str)
   Set regEx = nothing
End Function
 
'替换匹配字符串
'msgbox (ReplaceRegMatch("9","QC9.0, QTP9.0","10"))
Function ReplaceRegMatch(patrn,str,replaceStr)
   Dim regEx
   Set regEx = New RegExp
   regEx.Pattern = patrn
   regEx.IgnoreCase = False
   regEx.Global = True   'false的时候只会替换第一个匹配的字符串。若为true则会替换所有匹配的字符串
   ReplaceRegMatch = regEx.Replace(str,replaceStr)
End Function
 
'返回匹配内容
'returnRegMatch "qc .","qc 1 qc 2 qc3 qc 4"
Function ReturnRegMatch(patrn,str)
   Dim regEx,matches,match
   Set regEx = New RegExp
   regEx.Pattern = patrn
   regEx.IgnoreCase = true
   regEx.Global = true  '打开全局搜索
   Set matches = regEx.Execute(str)
   For Each match in matches
           print cstr(match.firstIndex) + " " + match.value + " " + cstr(match.length)
   Next
End Function
 
 
 

qtp使用技巧之正则表达式(三)

结合实例来看看如何使用VBS的RegExp对象

WinList,WinComboBox,WebList这些对象QTP都是有直接的方法可以直接选择的,但是我们多余一点说一下怎么来用正则去操作这些对象.
实例如下:
对象:WebList.
选项:name1
     name2
     name3
     .......

Function SelectRegExp(Obj,patrn)

         Dim numOfItems,i,CurrentValue,regEx,ItemToSelect,oldFilter

         Set regEx = New RegExp

         regEx.Pattern = patrn

         regEx.IgnoreCase = False

         ldFilter = Reporter.Filter

         Reporter.Filter = 2

         ItemToSelect = -1

       

         NumOfItems = obj.GetRoProperty("items count")

         for i=1 to NumOfItems

                   CurrentValue = Obj.GetItem(i)         ‘注释1

                   If regEx.Test(CurrentValue) Then

                            If(ItemToSelect<>-1) then

                                     SelectRegExp =-1 '项不唯一

                                     Reporter.Filter = oldFilter

                                     Exit Function

                            End If

                            ItemToSelect = i

                   End If

         Next

         Reporter.Filter = oldFilter

         If(ItemToSelect>=0) Then

                   SelectRegExp = obj.Select(ItemToSelect - 1)   ‘注释2

         Else

                   SelectRegExp = -1

         End If

End Function
'注释1跟注释2不一样的地方,就是下标的表示上,说明了GetItem这个方法的下标是从1开始的,而Select方法的下标是从0开始的,所以在 Select方法处我用了原来的坐标-1来作为下标

对于winlist这个对象的SelectRegExp方法已经写完了.然后我们需要注册这个方法请看以下api
2012082817455232.png 

RegisterUserFunc "WebList","Select","SelectRegExp"

Browser("title:=石家庄市区域卫生信息平台").Page("title:=石家庄市区域卫生信息平台").WebList("title:=test").SelectRegExp("name\d")
UnRegisterUserFunc "WebList","Select" 会将该Method返回到原始状态

或则

SelectRegExp(Browser("title:=石家庄市区域卫生信息平台").Page("title:=石家庄市区域卫生信息平台").WebList("title:=test"),"name\d")

 


 


TAG:

 

评分:0

我来说两句

Open Toolbar