多测师是一家拥有先进的教学理念,强大的师资团队,业内好评甚多的接口自动化测试培训机构!

17727591462

联系电话

您现在所在位置:接口自动化测试培训 > 新闻资讯

Vbscript正则表达式实例及在QTP中应用-自动化测试

更新时间:2022-05-30 09:00:42 作者:多测师 浏览:143

  一、实例:

  Function RegExpTest(patrn, strng)

  Dim regEx, Match, Matches ' 建立变量。

  Set regEx = New RegExp ' 建立正则表达式。

  regEx.Pattern = patrn ' 设置模式。

  regEx.IgnoreCase = True ' 设置不区分字符大小写。如果设置成 false 则区分大小小写

  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("is.", "IS1 is2 IS3 is4"))

Vbscript正则表达式实例及在QTP中应用-自动化测试

  二、在QTP中应用实例:

  正则表达式在自动化测试中可以起到意想不到的效果,这里对WinList控件的操作使用的是Select方法,但是需要指定一长串的字符串,例如下代码:

  Function SelectRegExp(Obj,Patrn,Button,Offset)

  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属性

  NumOfItems=Obj.getroproperty("items count")

  For i=0 to NumOfItems-1

  CurrentValue=Obj.GetItem(i)

  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,Button,Offset)

  else

  SelectRegExp=-1

  End If

  End Function

  利用”RegisterUserFunc“把SelectRegExp函数注册注册到WinList控件的Select方法后,就可以使用重用后的方法,如以下代码:

  '重写WinList控件的Select方法

  RegisterUserFunc"WinList","select","SelectRegExp"

  '使用重写后的WinList控件的Select方法

  Window("Flight Reservation").Dialog("Flights Table").Activate

  Window("Flight Reservation").Dialog("Flights Table").WinList("From").select "15797.*"

  完整的代码如下:

  Function SelectRegExp(Obj,Patrn,Button,Offset)

  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属性

  NumOfItems=Obj.getroproperty("items count")

  For i=0 to NumOfItems-1

  CurrentValue=Obj.GetItem(i)

  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,Button,Offset)

  else

  SelectRegExp=-1

  End If

  End Function

  Dialog("Login").WinEdit("Password:").Set "Cheers.lee"

  Dialog("Login").WinEdit("Edit").SetSecure "48d51f921459c0234cb35e05206c9141a5197c6a"

  Dialog("Login").WinButton("OK").Click

  Window("Flight Reservation").WinObject("Flight No:").Type "092208"

  Window("Flight Reservation").WinComboBox("Departure Time:").Select "London"

  Window("Flight Reservation").WinComboBox("Arrival Time:").Select "Los Angeles"

  Window("Flight Reservation").WinCheckBox("text:=FLIGHT").click

  RegisterUserFunc"WinList","select","SelectRegExp"

  Window("Flight Reservation").Dialog("Flights Table").Activate

  Window("Flight Reservation").Dialog("Flights Table").WinList("ListBox").select "19170.*"

  'Window("Flight Reservation").Dialog("Flights Table").WinList("ListBox").Select"19170 LON 08:00 AM LAX 08:45 AM AA $100.50"

  Window("Flight Reservation").Dialog("Flights Table").WinButton("OK").Click

  Window("Flight Reservation").WinEdit("Edit").Set "lihuichang"

  Window("Flight Reservation").WinRadioButton("Business").Set

  Window("Flight Reservation").WinButton("Insert Order").Click

  Window("Flight Reservation").Close

  以上内容为大家介绍了自动化测试中的Vbscript正则表达式实例及在QTP中应用,本文由多测师亲自撰写,希望对大家有所帮助。了解更多自动化测试相关知识:https://www.aichudan.com/xwzx/

联系电话

17727591462

返回顶部