zoukankan      html  css  js  c++  java
  • QTP(10)

    一、VBS语言基础
      1.运算符和表达式
        (1)运算符
        (2)表达式
          a.数学表达式:由算术运算符连接,计算结果为数字
          b.字符串表达式:由字符串连接符连接,计算结果为字符串
          c.条件表达式:由关系运算符或逻辑运算符连接,计算结果为布尔值----重点!!

          Test10001_VBS基础_关系运算符_强制转换

    'Dim a,b,c,d,e,f,g
    'a=100
    'b=99
    'c="100"
    'd="100"
    'e="99"
    'f="Hello"
    'g="World"
    'msgbox cstr(a)+f'100Hello
    
    'msgbox a>b'True
    'msgbox c<>d'False
    'msgbox a=c'Flase数据子类型不同,不相等
    'msgbox cint(a)=cint(c)'True
    'msgbox cstr(a)=cstr(c)'True
    'msgbox a>e'False???
    'msgbox a>cint(e)'将e强制转换为integer,再比较大小
    'msgbox f>g'False
    
    'msgbox 10/3
    'msgbox cint(10/3)'3
    'msgbox 10/7
    'msgbox cint(10/7)'1
    'msgbox cint(10.56)'11
    'msgbox cint(10.500001)'11
    msgbox cint(10.5)'10
    msgbox cint(8.5)'8
    msgbox cint(6.5)'6
    msgbox cint(11.5)'12
    msgbox cint(3.5)'4
    msgbox cint(1.5)'2

      2、强制转换子类型
        (1)cstr:强制转换为String子类型
          例:a=cstr(b)
        (2)cint:强制转换为Integer子类型
          例:a=cint(b)
            转换原则:
              a.当小数部分小于0.5时,舍掉
              b.当小数部分大于0.5时,进位
              c.当小数部分等于0.5时,取最接近的偶数作为结果。

            CBool Function | CByte Function | CCur Function | CDate Function | CDbl Function | CLng Function | CSng Function | CStr Function | Int, Fix Functions

          练习:录制Flight3个Action:登录、订票、退出,在订票Action中对订票的数据做DataTable参数化,其中航班在DataTable中准备选项编号。
            Test10002_Flight_订票_DataTable参数化

    登录Action()
    Dialog("Login").WinEdit("Agent Name:").Set "12345"
    Dialog("Login").WinEdit("Password:").SetSecure "5a5eb3b62861ca09e91a9690d13c8327db9c7737"
    Dialog("Login").WinButton("OK").Click
    
    订票Action()
    Window("Flight Reservation").WinButton("Button").Click
    Window("Flight Reservation").ActiveX("MaskEdBox").Type "111119"
    Window("Flight Reservation").WinComboBox("Fly From:").Select "Denver"
    Window("Flight Reservation").WinComboBox("Fly To:").Select "Frankfurt"
    Window("Flight Reservation").WinButton("FLIGHT").Click
    Window("Flight Reservation").Dialog("Flights Table").WinList("From").Select cint(DataTable("航班",dtLocalSheet))
    Window("Flight Reservation").Dialog("Flights Table").WinButton("OK").Click
    Window("Flight Reservation").WinEdit("Name:").Set "ooo"
    Window("Flight Reservation").WinButton("Insert Order").Click
    
    退出Action()
    Window("Flight Reservation").Close

      3.数组:用于存放一组数据
        (1)用法一:
          Dim 数组名称(最大下标)
            数组名称(下标)=值
          注意:下标从0开始,每个元素可以赋值任意子类型的数值。
          例:
            '用法一
              Dim stu(3)
              stu(0)="zhangsan"
              stu(1)="lisi"
              stu(2)="wangwu"
              stu(3)=100
              msgbox stu(1)
            Test10003_VBS基础_数组

    '用法一
    'Dim stu(3)
    'stu(0)="zhangsan"
    'stu(1)="lisi"
    'stu(2)="wangwu"
    'stu(3)=100
    '用法二:
    Dim stu
    stu=array("zhagnsan","lisi","wangwu",100)
    msgbox stu(3)

        (2)用法二:
          Dim 变量
          变量=array(值1,值2,值3,值4,值5)
          说明:变量就可以当做数据名称来使用。
          例:
            '用法二:
              Dim stu
              stu=array("zhagnsan","lisi","wangwu",100)
              msgbox stu(3)

          练习:录制两位数加法器加法计算后退出的步骤,将2个加数的数据提前存储在一个数组中,由数组元素提供加数数据。
          Test10004_两位数加法器_数组

    '用法一:
    'Dim num(1)
    'num(0)="30"
    'num(1)="80"
    '用法二:
    Dim num
    num=array("30","80")
    VbWindow("Form1").VbEdit("Text1").Set num(0)
    VbWindow("Form1").VbEdit("Text2").Set num(1)
    VbWindow("Form1").VbButton("计算(J)").Click
    VbWindow("Form1").VbButton("退出(E)").Click
    VbWindow("Form1").Dialog("退出提示").WinButton("确定").Click

      4.输入输出
        (1)msgbox:弹出信息框输出内容,测试时一般用于调试代码时输出关键变量的当前值。
          语法:msgbox "内容"
        (2)inputbox:弹出输入框来接收用户的输入,一般用于和用户交互。
          语法:变量=inputbox("请输入……")
          注意:只有当用户点击“确定”后,才会返回输入框中的内容,如果点击“取消”或直接关闭输入框,都返回空字符串。
          例:
            'inputbox "请输入您的姓名:"
            Dim n'姓名
            n=inputbox("请输入您的姓名:")
            msgbox n
        (3)特点:inputbox和msgbox都会打断程序的执行,必须等待用户操作后才继续执行,自动化测试时,尽量减少输入输出框的使用。
        (4)msgbox的替换方案:QTP提供的打印日志功能,最终自动化测试代码中建议使用print代替msgbox
          语法:Print "内容"

          练习:不录制,书写代码实现:
            (1)使用两个Inputbox获得两个操作数,存储在一个数组中
              说明:运行时都输入整数(例如20和30)
            (2)对这两个数分别做乘法和加法计算,将结果存储在两个变量中
            (3)使用两个msgbox输出以下格式的信息
              例如:

                20乘以30等于600
                20加上30等于50
              Test10005_VBS基础_综合

    Option Explicit
    '练习:不录制,书写代码实现:
    '(1)使用两个Inputbox获得两个操作数,存储在一个数组中
    Dim num(1)
    num(0)=Inputbox("请输入第一个数:")
    num(1)=Inputbox("请输入第二个数:")
    '说明:运行时都输入整数(例如20和30)
    '(2)对这两个数分别做乘法和加法计算,将结果存储在两个变量中
    Dim result1,result2'结果1和结果2
    result1=num(0)*num(1)
    result2=cint(num(0))+cint(num(1))
    '(3)使用两个msgbox输出以下格式的信息
    '例如:20乘以30等于600
    '            20加上30等于50
    msgbox num(0)&"乘以"&num(1)&"等于"&result1
    msgbox num(0)&"加上"&num(1)&"等于"&result2

    二、总结VBS语法规则
      1.总结VBS函数:
        (1)wait 5
        (2)msgbox "Hello"
        (3)a=Inputbox("请输入")
        (4)a=cint(b)
        (5)a=cstr(b)
        (6)a=array(1,6,8)


      2.VBS函数使用的语法规则:---非常重要!
        (1)如果函数无返回值,或有返回值但是不接收返回值时,将参数写在函数名称的空格后。
        (2)如果函数有返回值并且接收返回值,那么将参数写在函数名称后的小括号内。
          说明:除了函数,对象的方法也遵循这种语法规则。


      3.GetROProperty方法
        (1)作用:获得RO指定属性的当前值。
        (2)语法:变量=对象.GetROProperty("属性名称")
        (3)前提:该TO对象必须在对象仓库中存在
        (4)返回值:属性当前值(子类型是String)。
          例:
            num=Window("Flight Reservation").WinEdit("Order No:").GetROProperty("text")

          练习:使用jack用户名登录Flight,订票后,获得新订单号,退出后,使用rose用户登录Flight,打开jack刚订的新订单号的票,再退出
          Test10006_Flight_GetROProperty

    SystemUtil.Run "C:Program FilesHPQuickTest Professionalsamplesflightappflight4a.exe","","C:Program FilesHPQuickTest Professionalsamplesflightapp","open"
    Dialog("Login").WinEdit("Agent Name:").Set "jack"
    Dialog("Login").WinEdit("Password:").SetSecure "5a5efc499ef99cc3a3509a9dd5b95f25b33c87a0"
    Dialog("Login").WinButton("OK").Click
    wait 5
    Window("Flight Reservation").ActiveX("MaskEdBox").Type "111119"
    Window("Flight Reservation").WinComboBox("Fly From:").Select "Frankfurt"
    Window("Flight Reservation").WinComboBox("Fly To:").Select "Denver"
    Window("Flight Reservation").WinButton("FLIGHT").Click
    Window("Flight Reservation").Dialog("Flights Table").WinList("From").Select "20174   FRA   10:24 AM   DEN   11:09 AM   SR     $323.80"
    Window("Flight Reservation").Dialog("Flights Table").WinButton("OK").Click
    Window("Flight Reservation").WinEdit("Name:").Set "alice"
    Window("Flight Reservation").WinButton("Insert Order").Click
    wait 10
    '获得新订单号
    Dim num
    num=Window("Flight Reservation").WinEdit("Order No:").GetROProperty("text")
    print num
    Window("Flight Reservation").Close
    SystemUtil.Run "C:Program FilesHPQuickTest Professionalsamplesflightappflight4a.exe","","C:Program FilesHPQuickTest Professionalsamplesflightapp","open"
    Dialog("Login").WinEdit("Agent Name:").Set "rose"
    Dialog("Login").WinEdit("Password:").SetSecure "5a5efcd5b7328b37323ce6442cfa0b1f87d7a9d8"
    Dialog("Login").WinButton("OK").Click
    Window("Flight Reservation").WinButton("Button").Click
    Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON"
    Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set num
    Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
    Window("Flight Reservation").Close

    三、随机数参数化
      1.作用:做随机测试


      2.实现方式一:在关键字视图中,点击Value列的数据,点击其后的<#>按钮,在弹框中选择Parameter,选择下拉框中的Random Number,输入From下限和To上限值,点击OK
        缺陷:下限和上限不能输入负数


      3.实现方式二:在专家视图中,使用RandomNumber(下限,上限)的语法格式代替常量即可。


      4.注意:RandomNumber(下限,上限)的语法格式生成的数据子类型是Integer

        练习:录制Flight登录、订票、退出的步骤,订票时的数据做随机数参数化
        Test10007_Flight_随机数参数化

    Dialog("Login").WinEdit("Agent Name:").Set "1234"
    Dialog("Login").WinEdit("Password:").SetSecure "5a5f09bbe317fe069bb1dc4a7facfffe5b6cea25"
    Dialog("Login").WinButton("OK").Click
    wait 8
    Window("Flight Reservation").ActiveX("MaskEdBox").Type "111119"
    Window("Flight Reservation").WinComboBox("Fly From:").Select RandomNumber(0,9)
    Window("Flight Reservation").WinComboBox("Fly To:").Select RandomNumber(0,8)
    Window("Flight Reservation").WinButton("FLIGHT").Click
    Dim c'航班数(列表框的选项个数,items count属性值)
    c=Window("Flight Reservation").Dialog("Flights Table").WinList("From").GetROProperty("items count")
    Window("Flight Reservation").Dialog("Flights Table").WinList("From").Select RandomNumber(0,c-1)
    Window("Flight Reservation").Dialog("Flights Table").WinButton("OK").Click
    Window("Flight Reservation").WinEdit("Name:").Set "jack"
    Window("Flight Reservation").WinEdit("Tickets:").SetSelection 0,1
    Window("Flight Reservation").WinEdit("Tickets:").Set cstr(RandomNumber(1,10))
    Window("Flight Reservation").WinButton("Insert Order").Click
    Window("Flight Reservation").Close

          Dim c'航班数(列表框的选项个数,items count属性值)
          c=Window("Flight Reservation").Dialog("Flights Table").WinList("From").GetROProperty("items count")
          Window("Flight Reservation").Dialog("Flights Table").WinList("From").Select RandomNumber(0,c-1)

        练习:录制Flight登录-打开1-10之间随机编号的订单-退出,打开完订单后,退出前,使用msgbox输出“顾客x预定了y从z到w的k张票!”
        说明:x是顾客姓名、y是日期、z是FlyFrom、w是FlyTo、k是Tickets票数

  • 相关阅读:
    使用JavaScript实现新闻滚动效果
    ArcGIS Server建立缓存(切图)原理解析[图解] (转载)
    tp5 中使用自定义扩展类和函数
    phpstorm 配置点右上角浏览器图标时用浏览器打开当前页面
    PHP命名空间(Namespace)的使用简介
    linux 查看和修改文件时间
    头像上传uploadPreview插件
    富文本编辑器 kindEditor
    linux 计划任务 crontab 简单用法
    阿里 短信 使用方法
  • 原文地址:https://www.cnblogs.com/KalosOwen/p/8810499.html
Copyright © 2011-2022 走看看