zoukankan      html  css  js  c++  java
  • 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}"

  • 相关阅读:
    SpringCloud Alibaba Nacos详解
    常见设计模式
    Mybatis TypeHandler类型处理器
    Mybatis存取json字段转为Java对象方案
    Spring AOP 切面编程详解
    Spring Data 、Spring Data JPA 、Hibernate之间的关系及SpringDataJPA简单使用
    28张图解 | 互联网究竟是「如何连接,如何进行通信」的?
    Java代码实现 图片添加多行水印且自动换行
    SpringMVC 快速入门
    获取本地磁盘得到.txt文件
  • 原文地址:https://www.cnblogs.com/seniortestingdev/p/2416999.html
Copyright © 2011-2022 走看看