51Testing软件测试论坛

标题: 关于QTP中的正则表达式 [打印本页]

作者: yakezone    时间: 2011-7-15 09:28
标题: 关于QTP中的正则表达式
扩展saffron框架。在verify函数中添加一个判断。 假设输入的值是lun,期望的查询结果有lun0、lun1、lun2. 要用verify判断结果是否正确,在verify中怎么使用正则表达式实现?
作者: lanchozxd    时间: 2011-7-15 10:20
lun[/d]{0,}
我也不太会,错了别见怪哈
作者: lyscser    时间: 2011-7-15 11:49
我空间有个PAFAWEB的XX函数,无非就是.*一下,楼主可以去看下
作者: 阳光下的橙子    时间: 2011-7-15 11:57
Function RegExpTest(patrn, strng)
   Dim regEx, Match, Matches   
   Set regEx = New RegExp   
   regEx.Pattern = patrn   
   regEx.IgnoreCase = True   
   regEx.Global = True   
   Set Matches = regEx.Execute(strng)   
   For Each Match in Matches  
    RetStr = RetStr & "Match found at position "
      RetStr = RetStr & Match.FirstIndex & ". Match Value is '"
      RetStr = RetStr & Match.Value & "'." & vbCRLF
   Next
   RegExpTest = RetStr
End Function
MsgBox(RegExpTest("lun.", strng(你所需要进行判断的字符串))


这段函数直接在QTP帮助里面就有,我改了下发出来,但建议以后先看看帮助再伸手
作者: 阳光下的橙子    时间: 2011-7-15 12:01
你也可以把判断写的简单点,比如
   For Each Match in Matches  
     If Match.Value  Then
      Reporter.ReportEvent MicPass, "Verify", "Pass"
      Exit For
     End if
   Next
这样只要找到一个带lunxx的就算成功了
作者: 阳光下的橙子    时间: 2011-7-15 14:15
回复 5# 阳光下的橙子


    这段我试了一下,不能直接这样判断,我修改了一下:

Function RegExpTest(patrn, strng)
   Dim regEx, Match, Matches, count   
   count = 0
   Set regEx = New RegExp   
   regEx.Pattern = patrn   
   regEx.IgnoreCase = True   
   regEx.Global = True  
   Set Matches = regEx.Execute(strng)   
   For Each Match in Matches   
       count = count+1
       Regstr = Regstr&count&":"&Match.Value&vbcrlf
   Next
   If count > 0 Then
   MsgBox "在字符串中找到"&count&"个匹配值",1,"结果输出"
   RegExpTest = Regstr
   Else
   MsgBox "在字符串中找不到匹配值",1,"结果输出"
   RegExpTest = "结果为空"
   End if
End Function

msgbox(RegExpTest("is.", "IS1 is2 IS3 is4"))


楼主你可以直接放到QTP里跑跑就知道了,这个可以直接用




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2