从现在开始,每天一点点……

发布新日志

  • 获取Weblist数据并输出到Excel

    2012-05-31 14:54:53

    1.打开URL:http://newtours.demoaut.com/mercuryreservation.php
    2.下面代码是获取Weblist对象中的数据并输出到Excel

    ListSize = Browser("Welcome: Mercury Tours").Page("Find a Flight: Mercury").WebList("fromPort").GetTOProperty("Items count")
    Dim FromArray()
    ReDim FromArray(ListSize - 1)

    'msgbox ListSize
    For i = 1 To ListSize
        FromArray(i - 1) = Browser("Welcome: Mercury Tours").Page("Find a Flight: Mercury").WebList("fromPort").GetItem(i)
        WriteFile(FromArray(i - 1))
    Next

    Function WriteFile( ByVal Var)
        Const ForReading = 1, ForWriting = 2, ForAppending = 8
        Dim fso,f
        Set fso = CreateObject("Scripting.FileSystemObject")
        Set f = fso.OpenTextFile("d:\QTP\test.xls", ForAppending, True)
        f.WriteLine Var
        f.Close
    End Function
  • Ordinal Identifier(关于index,Location,CreationTime应用)

    2012-03-07 16:39:26

    除了学习在Object Identification对话框中((Tools > Object Identification))规定的强制性和辅助属性,QuickTest还可以了解每一个测试对象的备份顺序标识,分配给对象的数字ordinal identifier与其他对象的an otherwise identical description有关(在mandatory and assistive property 的列表中指定的对象的所有属性值都一样)。定制的值使QuickTest创建一个唯一的描述当mandatory and assistive 属性不能满足时.

    QTP只有在得到描述和Smart Identification机制 不能足够唯一识别一个对象的时候,才会使用an ordinal identifier机制。如果QTP在运行一个会话时能使用其他测试对象属性去识别对象的,an ordinal identifier 将会被忽略

    Index.表示对象出现在应用程序代码中相对于其他对象具有的不一样的描述顺序

    Location.表示对象出现在父WindowFrameDialog的顺序不同于其他对象的唯一描述

    CreationTime.(仅适用于浏览器)被打开浏览器顺序相对于其他打开的浏览器具有唯一的描述

     

    使用Index属性识别对象

    QTP得到一个对象时,会分配一个值给测试对象的index属性使其能唯一的识别这个对象。这个值是基于对象在源代码中出现的顺序分配的。第一次出现的值为0

    Index property values are object-specific。因此,当你使用index:=3去描述WebEdit 测试对象时,QTP就会在页面中搜索第四个WebEdit对象。而如果使用index:=3描述一个WebElement对象时,QTP就会搜在页面中搜索第四个web对象,与对象的类型无关,因为WebElement对象适用所有web对象。

    ·         an image with the name Apple

    ·         an image with the name UserName

    ·         a WebEdit object with the name UserName

    ·         an image with the name Password

    ·         a WebEdit object with the name Password

    下面中的陈述指列表中的第三项,因为这是页面上第一个名称为UserNameWebEdit对象

    WebEdit(“Name:=UserName”, “Index:=0”)

    相反,下面的陈述指的是列表中的第二项,因为那是指所有(WebElement)对象中名称为UserName的第一个对象。

    WebElement(“Name:=UserName”, “Index:=0”)

     

     

    使用Location属性识别对象

    但得到一个对象的时候,QTP会给测试对象的Location属性分配一个值以唯一的识别这个对象,这个值基于相对于具有相同属性的其他对象在WindowFrameDialog box中出现的顺序给出的,

    第一次出现的值为0。这些值是按照从上到下,从左到右的顺序分配的。例如:

    Location属性值是指定对象的。因此,如果使用Location:=3描述一个WinButton测试对象,QTP会在页面上按照从上到下,从左到右的顺序搜索第四个WinButton对象。然而,如果使用Location:=3描述一个WinObject对象,QTP会按照从上到下,从左到右的顺序搜索页面上第四个标准对象,与类型无关,因为WinObject适用于所有标准对象。

    例如:一个对话框包含下面的对象

    ·         a button object with the name OK

    ·         a button object with the name Add/Remove

    ·         a check box object with the name Add/Remove

    ·         a button object with the name Help

    ·         a check box object with the name Check spelling

    下面的陈述指的是列表中的第三项,因为它是页面上第一个名为Add/RemoveCheck box对象

    WinCheckBox(“Name:=Add/Remove”, ”Location:=0”)

    相反,下面的陈述值得是列表中的第二项,因为他是第一个名为Add/Remove的对象

    WinObject("Name:=Add/Remove", "Location:=0")

    使用CreationTime属性识别对象

    当得到一个浏览器对象的时候,如果QTP不能根据它的对象描述唯一的识别这个对象时,他会给测试对象属性CreationTime分配一个值。这个值表示被打开的浏览器相对于已经打开浏览器顺序的唯一不同的描述。第一个打开的浏览器接收到CreationTime=0.

    在运行一个会话期间,如果QTP不能根据测试对象仅有的描述去识别一个浏览器对象,它会检查浏览器被打开的顺序,并且使用CreationTime属性去识别正却的浏览器

    例如:你录制了一个模块分别在9:01 pm, 9:03 pm, and 9:05 pm打开的浏览器上,QTP会这样分配CreationTime的值:9:01 pm 打开的浏览器为CreationTime:=0, 9. 03 pm 打开的浏览器为CreationTime:=1, 9.05 pm 打开的浏览器为CreationTime:=2

    10:30pm要运行那个模块,假设浏览器被打开分别在10:31 pm, 10:33 pm, and 10:34 pm.QTP会这样识别浏览器:用具有CreationTime:=0测试对象识别在10:31 pm被打开的浏览器,用具有CreationTime:=1测试对象识别在10:33 pm被打开的浏览器,用具有CreationTime:=2测试对象识别在10:34 pm被打开的浏览器

    如果打开了几个浏览器,拥有最小CreationTime值的浏览器是第一个被打开的,拥有最大CreationTime值的浏览器是最后一个被打开的。如果有三个或者更多的浏览器被打开,拥有CreationTime:=2的是第三个被打开的浏览器。如果在运行一个session的时候打开了七个浏览器,

    拥有CreationTime:=6属性的浏览器是最后打开的

    如果在有指定CreationTime值的浏览器中录制一个step,但是在运行session的时候没有具有CreationTime值的浏览器打开,the step将会在具有最大CreationTime值的浏览器上运行。

    下面这个例子:

    SystemUtil.Run "iexplore.exe", "http://www.baidu.com"               'CreationTime 0

    SystemUtil.Run "iexplore.exe", "http://www.google.com.hk/webhp?hl=zh-CN&sourceid=cnhp"      'CreationTime 1

    SystemUtil.Run "iexplore.exe", "http://www.taobao.com"

     

    wait(30)

    Browser( "creationtime:=" ).Highlight

    wait(30)

    Browser( "creationtime:=1" ).Highlight

    wait(30)

    Browser( "creationtime:=2" ).Highlight

    我不知道为什么设置第一个creationtime:=0时,Hight顺序就不正确了
  • QTP使用Excel参数化

    2012-03-07 14:58:48

    1.创建一个文件在D:\TestFile.xls


    2.编写下面的代码
    Dim  xlApp    'Excel Application Automation Object Reference
    Dim xlBook  'Reference to the workbook located in D:\
    Dim xlSheet 'Reference to Sheet1
    Dim iRow, sUserName, sPassword
    Const iUserNameCol = 1
    Const iPasswordCol = 2

    Set xlApp = CreateObject("Excel.Application")
    Set xlBook = xlApp.Workbooks.Open("D:\TestFile.xls")
    Set xlSheet = xlBook.Worksheets("Sheet1")

    'SystemUtil.Run "iexplore.exe", "http://newtours.demoaut.com"

    For iRow = 2 to xlSheet.UsedRange.Rows.Count
        sUserName = xlSheet.Rows(iRow).Columns(iUserNameCol).Value
        sPassword = xlSheet.Rows(iRow).Columns(iPasswordCol).Value
        With Browser("title:=Welcome: Mercury Tours").Page("title:=Welcome: Mercury Tours")
            .WebEdit("name:=userName").Set  sUserName
            .WebEdit("name:=password").Set sPassword
            .Image("name:=login").Click
        End With
        If Browser("title:=Find a Flight: Mercury Tours:").Exist(15) Then
            Browser("title:=Find a Flight: Mercury Tours:").Link("text:=Home").Click
            Reporter.ReportEvent micPass,"Iteration " &iRow-1,"UserName:"&sUserName& "is valid"
        Else
            Reporter.ReportEvent micFail,"Iteration " &iRow-1,"UserName:"&sUserName& "is invalid"
            Browser("title:=Sign-on: Mercury Tours", "index:=0").Link("namet:=SIGN-ON").Click
        End If
    Next
    xlBook.Close
    xlApp.Quit
    Set xlSheet = Nothing
    Set xlBook = Nothing
    Set xlApp = Nothing

  • 关于activeX安全控件正确回放

    2012-03-01 16:01:22

    打开淘宝的登录界面,录制
    Browser("淘宝网 - 淘我喜欢!").Page("淘宝网 - 淘我喜欢!").WebEdit("TPL_username").Set "username"
    Browser("淘宝网 - 淘我喜欢!").Page("淘宝网 - 淘我喜欢!").ActiveX("EditCtrl Class").WinObject("ATL:Edit").Click 40,6
    Browser("淘宝网 - 淘我喜欢!").Page("淘宝网 - 淘我喜欢!").WebButton("登录").Click
    Browser("淘宝网 - 淘我喜欢!").Page("淘宝网 - 淘我喜欢!").WebEdit("TPL_password").SetSecure "password"

    运行后不能正确登录!


    修改代码后:
    Browser("淘宝网 - 淘我喜欢!").Page("淘宝网 - 淘我喜欢!").WebEdit("TPL_username").Set "username"
    Browser("淘宝网 - 淘我喜欢!").Page("淘宝网 - 淘我喜欢!").ActiveX("EditCtrl Class").WinObject("ATL:Edit").Click 121,15
    Browser("淘宝网 - 淘我喜欢!").Page("淘宝网 - 淘我喜欢!").ActiveX("EditCtrl Class").WinObject("ATL:Edit").Type "password"
    Browser("淘宝网 - 淘我喜欢!").Page("淘宝网 - 淘我喜欢!").WebButton("登录").Click

    可以成功的回放了



  • 如何点击左右键菜单上的内容

    2012-02-15 16:20:04

    下面的例子是是从右键菜单打开google大全
    1.将link对象对加到本地对象库并保存
    2.编写下面的代码
    Setting.WebPackage("ReplayType") = 2

    Browser("Google").Page("Google").Link("Google 大全").FireEvent "onclick", , ,micRightBtn

    Setting.WebPackage("ReplayType") = 1

    Index =1
    Set wshShell = CreateObject("Wscript.Shell")
    For i =1 to index
        WshShell.sendKeys "{DOWN}"

    Next
    WshShell.sendKeys "{ENTER}"
    Set WshShell = nothing

    这里面的重点是学习FireEvent的使用和QTP的两种运行模式
    1.Event模式  --  事件跟踪

    2. Mouse模式 --   鼠标跟踪

    Event模式就是我们平时默认用的模式,也就是事件,其实QTP的click方法只是触发了对象的CLICK事件,而并未真正通过鼠标来进行点击按钮。而如果我们使用的是Mouse模式的话,我们会看到回放的时候,鼠标是跟着对象走的,即鼠标跟踪模式。


    下面的例子是左键菜单的问题

    1.打开连接:http://ucenter.51cto.com/reg_01.php?reback=http%253A%252F%252Fwww.51cto.com%252F#

    录制录制代码:

    Browser("51cto用户注册").Page("51cto用户注册").WebCheckBox("guanzhu").Set "ON"
    Browser("51cto用户注册").Page("51cto用户注册").WebCheckBox("guanzhu_2").Set "ON"
    Browser("51cto用户注册").Page("51cto用户注册").Image("ico_queding").Click 15,9

    Browser("51cto用户注册").Page("51cto用户注册").WebCheckBox("hangye").Set "ON"
    Browser("51cto用户注册").Page("51cto用户注册").WebCheckBox("hangye_2").Set "ON"
    Browser("51cto用户注册").Page("51cto用户注册").Image("ico_queding").Click 21,12
    Browser("51cto用户注册").Page("51cto用户注册").WebList("companysize").Select "1000-4999人"

    运行后结果如下:

    2.将下面这个两个不能识别的对象添加到对象库并保存

     

    对象库显示如下

    3.修改代码(红色字体问修改的部分)

    Setting.WebPackage("ReplayType") = 2

    Browser("51cto用户注册").Page("51cto用户注册").WebElement("WebElement").FireEvent "onclick", , ,micLeftBtn

    Setting.WebPackage("ReplayType") = 1


    Set wshShell = CreateObject("Wscript.Shell")

    If true Then
        WshShell.sendKeys "{DOWN}"
        WshShell.sendKeys "{TAB}"
    End If
    Set WshShell = nothing

    Browser("51cto用户注册").Page("51cto用户注册").WebCheckBox("guanzhu").Set "ON"
    Browser("51cto用户注册").Page("51cto用户注册").WebCheckBox("guanzhu_2").Set "ON"
    Browser("51cto用户注册").Page("51cto用户注册").Image("ico_queding").Click 15,9

    Setting.WebPackage("ReplayType") = 2

    Browser("51cto用户注册").Page("51cto用户注册").WebElement("WebElement_2").FireEvent "onclick", , ,micLeftBtn

    Setting.WebPackage("ReplayType") = 1

    Index =1
    Set wshShell = CreateObject("Wscript.Shell")
    If true Then
        WshShell.sendKeys "{DOWN}"
        WshShell.sendKeys "{TAB}"
    End If
    Set WshShell = nothing

    Browser("51cto用户注册").Page("51cto用户注册").WebCheckBox("hangye").Set "ON"
    Browser("51cto用户注册").Page("51cto用户注册").WebCheckBox("hangye_2").Set "ON"
    Browser("51cto用户注册").Page("51cto用户注册").Image("ico_queding").Click 21,12
    Browser("51cto用户注册").Page("51cto用户注册").WebList("companysize").Select "1000-4999人"


    代码运行之后的结果如下:

  • QTP用户登录(使用Localsheet参数化)

    2012-02-09 09:53:18

    使用localsheet循环登录的例子
    Option Explicit

    Dim row
    Dim i

    row = DataTable.GetSheetCount

    For i = 1 to row
        Browser("title:=Welcome: Mercury Tours").Page("title:=Welcome: Mercury Tours").WebEdit("name:=userName").Set DataTable.GetSheet("Action1").GetParameter("Username")
        Browser("title:=Welcome: Mercury Tours").Page("title:=Welcome: Mercury Tours").WebEdit("name:=password").Set DataTable.GetSheet("Action1").GetParameter("password")
        Browser("title:=Welcome: Mercury Tours").Page("title:=Welcome: Mercury Tours").Image("name:=login").Click
        Browser("title:=Find a Flight: Mercury Tours:").Page("title:=Find a Flight: Mercury Tours:").Link("text:=Home").Click
        DataTable.GetSheet("Action1").SetNextRow
    Next


  • Benefits of Automated Testing

    2008-03-13 11:04:34

    Fast
    QuickTest runs tests significantly faster than human users.
    Reliable
    Tests perform precisely the same operations each time they are run, thereby eliminating human error.
    Repeatable
    You can test how the Web site or application reacts after repeated execution of the same operations.
    Programmable
    You can program sophisticated tests that bring out hidden information.
    Comprehensive
    You can build a suite of tests that covers every feature in your Web site or application.
    Reusable
    You can reuse tests on different versions of a Web site or application, even if the user interface changes.
Open Toolbar