自动化测试

发布新日志

  • 揭秘QTP的DeviceReplay对象

    2008-07-20 19:02:03

    揭秘QTPDeviceReplay对象

    原文:The Undocumented DeviceReplayhttp://www.advancedqtp.com/2008/03/undocumented-devicereplay/www.advancedqtp.com

     

    不知道为什么HP的帮助文档中没有提供关于DeviceReplay的强大功能的信息描述。你可以在Java插件中却可以找到DeviceReplay的属性,但是对于那些不使用Java插件的人可能会觉得这个对象仅在Java程序的测试中可用。

    为什么要用DeviceReplay

    有些时候我们需要针对界面做一些指定的动作,例如右键单击一个对象,使用功能键(Fx)来激活某些热键的功能,这时候就可以使用DeviceReplay对象,或者在Object.SetObject.Type方法不生效时使用DeviceReplay

     

    并且DeviceReplay在输入特殊符号以及不同语言的文字时会很有用,因为不需要安装指定的字体或改变键盘布局,这对于测试多语言环境的应用程序会非常有用。

     

    在鼠标操作方面,我发现DragDrop方法非常有用,可以使用它来执行拖拽的操作,把一个Item从一个Frame拖动到另外一个Frame,或者在应用程序之间拖动。

    Mercury.DeviceReplay对象

    Mercury.DeviceReplay对象用于模拟鼠标单击和移动,还有键盘输入等操作。要使用DeviceReplay,你必须确保被测试的应用程序(AUT)是处于激活状态的窗口。如果你想对某个对象执行一项操作,则该对象必须拥有焦点(focus)。对于Windows应用程序,可以使用Activate方法:

    Window( "W" ).Activate micLeftBtn

     

    如果想把焦点设置到某个指定的对象上,通常使用Click方法可以完成。

     

    对于Web环境的应用程序,Activate方法不被支持,因此可以使用下面的技巧来完成:

    hwnd = Browser( "B" ).GetROProperty( "hwnd" )

    Window( "hwnd:=" & hwnd ).Activate micLeftBtn

     

    通常可以使用FireEvent “onfocusin”object.focus,例如WebEdit(“WE”).Object.focusWebEdit(“WE”)FireEvent “onfocusin”

     

    在调用DeviceReplay对象的方法之前,你需要首先创建DeviceReplay对象:

    Set deviceReplay = CreateObject( "Mercury.DeviceReplay" )

    Microsoft.VisualBasic.Devices.Keyboard

    为什么我要在介绍DeviceReplay对象之前介绍这个.NET的类呢?DeviceReplay是一个强大的未被文档化的对象,但是有一定的局限性。其中一个局限就是不能判断一个Control键是否已经被按下。在输入一个大写字母之前,我们需要知道CAPS-LOCK键是否已经按下。在使用数字键盘之前我们需要检查NUM-LOCK键是否已经被按下。否则我们在切换键盘输入状态时可能得到的并不是我们想要的状态。

     

    Devices.Keyboard类提供了属性,可用于获取当前的键盘状态,例如当前什么键被按下了,并且提供一个方法用于向激活的窗口发送键盘敲击事件。

     

    几个有用的属性包括:

    AltKeyDown - 判断ALT键是否处于按下状态。

    CapsLock -  判断CAPS LOCK键是否处于打开状态。

    CtrlKeyDown - 判断CTRL 键是否处于按下状态。

    NumLock - 判断NUM LOCK键是否处于打开状态。

    ScrollLock - 判断SCROLL LOCK键是否处于打开状态。

    ShiftKeyDown - 判断SHIFT键是否处于按下状态。

     

    Set Keyboard = DotNetFactory.CreateInstance(

    "Microsoft.VisualBasic.Devices.Keyboard", "Microsoft.VisualBasic" )

    Print CBool( Keyboard.AltKeyDown )

    Print CBool( Keyboard.CapsLock )

    Print CBool( Keyboard.CtrlKeyDown )

    Print CBool( Keyboard.NumLock )

    Print CBool( Keyboard.ScrollLock )

    Print CBool( Keyboard.ShiftKeyDown )

     

    注意:在使用DotNetFactory时数据类型必须被转换

    System.Windows.Forms.Control

    DeviceReplay的另外一个局限是不能获取当前鼠标(光标)在屏幕的位置。而System.Windows.Forms.Control这个类定义了那些拥有视觉表现的控件的基类。

     

    通过MousePosition属性可以获取当前鼠标光标在屏幕坐标的位置。访问MousePosition属性时,可以返回代表鼠标光标位置的Point数据。

    我的鼠标在哪?

    Set ctlr = DotNetFactory.CreateInstance("System.Windows.Forms.Control")

    For i = 1 To 10

    Wait 2

    Print "1. X=" & ctlr.MousePosition.X & "; Y=" & ctlr.MousePosition.Y

    Next

    Mercury.DeviceReplay的方法

    SendString方法

    描述

    向激活的窗口发送一个或多个键盘按键,就像敲击键盘一样。

    语法

    object.SendString( str )

    参数

    object Mercury.DeviceReplay对象。

    str 敲击的字符串。

    返回值

    无。

    例子

    下面的例子会激活记事本(notepad)并输入一段字符:

    Set deviceReplay = CreateObject( "Mercury.DeviceReplay" )

    SystemUtil.Run "notepad.exe", "", "", "open"

    ' ** this line always identifies the notepad window.

    Window( "nativeclass:=Notepad", "index:=0" ).Activate micLeftBtn

    deviceReplay.SendString( "DeviceReplay" )

    Set deviceReplay = Nothing

    KeyDown方法

    描述

    模拟一个按键的按下并保持(相当于Win32KEY_DOWN事件)。

    语法

    object.KeyDown( key )

    参数

    object Mercury.DeviceReplay对象。

    key 按键的数值码。可查阅后面的“Key Codes 参考”。

    返回值

    无。

    例子

    下面的例子会激活记事本(notepad)程序并使用大写和小写的方式输入字符串。注意在发送第一个字符串时,SHIFT键保持被按下的状态:

    Const VK_SHIFT = 42

    Const VK_RETURN = 28

    Set deviceReplay = CreateObject( "Mercury.DeviceReplay" )

    SystemUtil.Run "notepad.exe", "", "", "open"

    Window( "nativeclass:=Notepad", "index:=0" ).Activate micLeftBtn

    ' ** Typing uppercase

    deviceReplay.KeyDown VK_SHIFT

    deviceReplay.SendString( "devicereplay" )

    deviceReplay.PressKey VK_RETURN

    deviceReplay.KeyUp VK_SHIFT

    ' ** Typing in lower case

    deviceReplay.SendString( "devicereplay" )

    Set deviceReplay = Nothing

    提示

    KeyDown后应该有相应的KeyUp方法的调用。

    KeyDown方法就像人工按下一个按键并保持按下的状态。

    KeyUp方法

    描述

    模拟通过键盘释放某个按下的按键。

    语法

    object.KeyUp( key )

    参数

    object Mercury.DeviceReplay对象。

    key 按键的数值码。可查阅后面的“Key Codes 参考”。

    返回值

    无。

    例子

    下面的例子会激活并并使用热键CTRL+O来打开记事本(notepad)的菜单,然后用ESC键关闭对话框。

    Const VK_O = 24

    Const VK_CONTROL = 29

    Const VK_ESCAPE = 1

    Set deviceReplay = CreateObject( "Mercury.DeviceReplay" )

    SystemUtil.Run "notepad.exe", "", "", "open"

    Window( "nativeclass:=Notepad", "index:=0" ).Activate micLeftBtn

    ' ** Typing uppercase

    Wait 1

    ' ** Opening the menu Ctrl + O

    deviceReplay.KeyDown VK_CONTROL

    deviceReplay.PressKey VK_O

    deviceReplay.KeyUp VK_CONTROL

    Wait 2

    ' ** Closing the menu

    deviceReplay.PressKey VK_ESCAPE

    deviceReplay.SendString "Menu Open, was closed."

    Set deviceReplay = Nothing

    提示

    KeyUp方法应该与KeyDown方法配对使用。

    多个KeyUp不会对应用程序造成影响。

    如果需要组合热键,仅需要像人工执行的方式一样即可。

    PressKey方法

    描述

    模拟通过键盘按下一个按键并立即释放。

    语法

    object.PressKey( key )

    参数

    object Mercury.DeviceReplay对象。

    key 按键的数值码。可查阅后面的“Key Codes 参考”。

    返回值

    无。

    例子

    下面的例子会激活记事本并使用热键CTRL+O来模拟选择文件打开菜单,然后用ESCAPE按键关闭对话框。

    Const VK_O = 24 : Const VK_F = 33

    Const VK_CONTROL = 29 : Const VK_ESCAPE = 1 : Const VK_MENU = 56

    Set deviceReplay = CreateObject( "Mercury.DeviceReplay" )

    SystemUtil.Run "notepad.exe", "", "", "open"

    Window( "nativeclass:=Notepad", "index:=0" ).Activate micLeftBtn

    Wait 1

    ' ** Opening the menu Alt + F + O

    deviceReplay.PressKey VK_MENU

    deviceReplay.PressKey VK_F

    deviceReplay.PressKey VK_O

    Wait 2

    ' ** Closing the menu

    deviceReplay.PressKey VK_ESCAPE

    deviceReplay.SendString "Open menu was closed."

    Set deviceReplay = Nothing

    PressNKeys方法

    描述

    模拟通过键盘多次按下一个按键并立即释放。

    语法

    object.PressNKey( key, N )

    参数

    object Mercury.DeviceReplay对象。

    key 按键的数值码。可查阅后面的“Key Codes 参考”。

    N:重复的次数。

    返回值

    无。

    例子

    1 – 美国的州

    Option Explicit

    Const VK_RETURN = 28 : Const VK_F = 33 : Const VK_O = 24

    Const VK_TAB = 15 : Const VK_F5 = 63

    Const VK_CAPITAL = 58 : Const VK_NUMLOCK = 69

    Const VK_SUBTRACT = 74 : Const VK_MULTIPLY = 55

    Const VK_MENU = 56

    Dim deviceReplay

    Private Sub SetupKeyboard()

    Const CLASS_NAME = "Microsoft.VisualBasic.Devices.Keyboard"

    Const ASSEMBLY = "Microsoft.VisualBasic"

    Dim Keyboard

    Set Keyboard = DotNetFactory.CreateInstance( CLASS_NAME, ASSEMBLY )

    If CBool( Keyboard.CapsLock ) Then

    deviceReplay.PressKey VK_CAPITAL

    End If

    If CBool( Keyboard.NumLock ) = False Then

    deviceReplay.PressKey VK_NUMLOCK

    End If

    Set Keyboard = Nothing

    End Sub

    Private Sub SetupNotepad()

    deviceReplay.PressKey VK_MENU

    deviceReplay.PressKey VK_O

    deviceReplay.PressKey VK_F

    deviceReplay.SendString "Courier New"

    deviceReplay.PressKey VK_TAB

    deviceReplay.PressKey VK_TAB

    deviceReplay.SendString "14"

    deviceReplay.PressKey VK_RETURN

    Wait 1

    End Sub

    Private Sub PrintRow( ByVal state, ByVal usps, byVal capital )

    deviceReplay.SendString state

    deviceReplay.PressKey VK_TAB

    If Len( state ) < 8 Then

    deviceReplay.PressKey VK_TAB

    End If

    deviceReplay.SendString usps

    deviceReplay.PressKey VK_TAB

    deviceReplay.SendString capital

    deviceReplay.PressKey VK_RETURN

    End Sub

    Set deviceReplay = CreateObject( "Mercury.DeviceReplay" )

    SystemUtil.Run "notepad.exe", "", "", "open", 3

    Window( "nativeclass:=Notepad", "index:=0" ).Activate micLeftBtn

    ' ** Setup Notepad - Font courier new, size 14,

    ' ** NUM-LOCK pressed and CAPS-LOCK unpressed

    Call SetupKeyboard()

    Call SetupNotepad()

    ' ** inserting date

    deviceReplay.PressKey VK_F5

    deviceReplay.PressKey VK_RETURN

    ' ** Inserting Title

    deviceReplay.PressNKeys VK_TAB, 3

    deviceReplay.SendString "United States of America"

    deviceReplay.PressKey VK_RETURN

    deviceReplay.PressNKeys VK_TAB, 3

    deviceReplay.PressNKeys VK_MULTIPLY, Len( "United States of America" )

    deviceReplay.PressNKeys VK_RETURN, 2

    ' ** Table Headers

    deviceReplay.SendString "State"

    deviceReplay.PressKey VK_TAB

    deviceReplay.PressKey VK_TAB

    deviceReplay.SendString "USPS"

    deviceReplay.PressKey VK_TAB

    deviceReplay.SendString "Capital"

    deviceReplay.PressKey VK_RETURN

    deviceReplay.PressNKeys VK_SUBTRACT, 31

    deviceReplay.PressKey VK_RETURN

    ' ** Print Data

    Call PrintRow( "Alabama", "AL", "Montgomery" )

    Call PrintRow( "Alaska", "AK", "Juneau" )

    Call PrintRow( "Arizona", "AZ", "Phoenix" )

    Call PrintRow( "Arkansas", "AR", "Little Rock" )

    Call PrintRow( "California", "CA", "Sacramento" )

    Call PrintRow( "Colorado", "CO", "Denver" )

    Call PrintRow( "Connecticut", "CT", "Hartford" )

    Call PrintRow( "Delaware", "DE", "Dover" )

    Call PrintRow( "Florida", "FL", "Tallahassee" )

    Call PrintRow( "Georgia", "GA", "Atlanta" )

    Call PrintRow( "Hawaii", "HA", "Honolulu" )

    Call PrintRow( "Idaho", "ID", "Boise" )

    Call PrintRow( "Illinois", "IL", "Springfield" )

    Call PrintRow( "Indiana", "IN", "Indianapolis" )

    Call PrintRow( "Iowa", "IA", "Des Moines" )

    Call PrintRow( "Kansas", "KS", "Topeka" )

    Call PrintRow( "Kentucky", "KY", "Frankfort" )

    Call PrintRow( "Louisiana", "LA", "Baton Rouge" )

    Call PrintRow( "Maine", "ME", "Augusta" )

    Call PrintRow( "Maryland", "MD", "Annapolis" )

    Call PrintRow( "Massachusetts", "MA", "Boston" )

    Call PrintRow( "Michigan", "MI", "Lansing" )

    Call PrintRow( "Minnesota", "MN", "Saint Paul" )

    Call PrintRow( "Mississippi", "MS", "Jackson" )

    Call PrintRow( "Missouri", "MO", "Jefferson City" )

    Call PrintRow( "Montana", "MT", "Helena" )

    Call PrintRow( "Nebraska", "NE", "Lincoln" )

    Call PrintRow( "Nevada", "NV", "Carson City" )

    Call PrintRow( "New Hampshire", "NH", "Concord" )

    Call PrintRow( "New Jersey", "NJ", "Trenton" )

    Call PrintRow( "New Mexico", "NM", "Santa Fe" )

    Call PrintRow( "New York", "NY", "Albany" )

    Call PrintRow( "North Carolina", "NC", "Raleigh" )

    Call PrintRow( "North Dakota", "ND", "Bismarck" )

    Call PrintRow( "Ohio", "OH", "Columbus" )

    Call PrintRow( "Oklahoma", "OK", "Oklahoma City" )

    Call PrintRow( "Oregon", "OR", "Salem" )

    Call PrintRow( "Pennsylvania", "PA", "Harrisburg" )

    Call PrintRow( "Rhode Island", "RI", "Providence" )

    Call PrintRow( "South Carolina", "SC", "Columbia" )

  • 关于虚拟对象的使用

    2008-07-20 19:02:03

    很好的东西,建议初学者动手操作一下!(转自xiaonan的空间,感谢作者!)

     

    当我们在录制脚本中,有时会遇到某个对象回放时不能识别.那么就可以考虑是不是可以做个虚拟对象来代替它.您可以将这些对象定义为虚拟对象,并将它们映射到标准类,例如按钮或复选框。QuickTest 在运行会话过程中模拟虚拟对象上的用户操作。在测试结果中,虚拟对象显示时类似标准类对象。
       下面我们还是通过一个例子来具体操作一下.这次选用LoadRuner自带的b/s架构的web例子,来录制脚本.(注:运行这个例子,同时把web server先给开起来.).这次只录制一个点击sign up(注册)的步骤.
    录出脚本如下:
    Browser("Mercury Tours").Page("Mercury Tours").Frame("navbar").Image("signup").Click 45,12

    QTP把这个button对象识别成了Image(图片)对象,导致最后回放的时候,QTP不能完成Click操作,弹出报错信息.对于这个对象,我马上想到了做个虚拟的button对象来代替它.
    下面是整个虚拟对象的制作过程:
    1.选择“工具”>“虚拟对象”>“新建虚拟对象”。或者,也可以在虚拟对象管理器中,单击“新建”。将打开虚拟对象向导。单击“下一步”。

    2.选择要将您的虚拟对象映射到的标准类(这里有六种对象类供你选择,分别是object,button,table,list,check box,radio button).这里我们要映射一个button对象,所以选择button.单击"下一步"

    3.单击“标记对象”。选择你要做虚拟对象的区域,拖动鼠标.尽量能覆盖整个你想替代的对象.软件测试专业网站:51Testing软件测试网kDTl#Ip

    4.在“标识对象使用”框中,选择您希望 QTP 标识和映射虚拟对象的方式。

    如果您想要 QuickTest 标识所有出现的虚拟对象,请选择“Parent only”。
    如果想要 QuickTest 仅标识一次出现的虚拟对象,请选择“Entire parent hierarchy"。
    这里我们选择默认的“Parent only”。
    5.指定虚拟对象的名称和集合。从集合列表中选择,或通过在“集合名”框中输入新名称来新建集合

    点击完成,整个虚拟对象就做好了.
    虚拟对象做好后,保存在虚拟对象管理器中.我们可以通过虚拟对象管理器来删除或禁用虚拟对象定义.
    选择“工具”>“虚拟对象”>“虚拟对象管理器”。将打开虚拟对象管理器。软件测试专业网站:51Testing软件测试网@9{Dn @$_V

    注意:如果希望 QuickTest 在录制时识别虚拟对象,请确保清除“选项”对话框的“常规”选项卡中的“录制时禁止识别虚拟对象”复选框。
    接下来我们再来录制一遍对sign up的操作,录制代码如下:
    Browser("Mercury Tours").Page("Mercury Tours").Frame("navbar").VirtualButton("button").Click
    在录制过程中值得注意的是,一定要点击到我们做好虚拟对象的范围内,这样QTP才能识别到这个虚拟对象.这里我们的虚拟对象识别成功.再次回放,QTP顺利的执行了脚本.
    还有两点要注意的是:
    1.虚拟对象管理器中显示的虚拟对象集合存储在您的计算机中,而不是随包含虚拟对象步骤的测试或组件存储。这意味着如果您在测试或组件步骤中使用虚拟对象,则仅当在包含正确的虚拟对象定义的计算机中运行时,该对象在运行会话过程中才能被识别。要将您的虚拟对象集合定义复制到另一个计算机,请将您的 <QuickTest 安装文件夹>\dat\VoTemplate 文件夹的内容(或该文件夹中的单个 .vot 集合文件)复制到目标计算机上的相同文件夹中。
    2.仅当录制和运行测试或组件时,才能使用虚拟对象。您不能在虚拟对象上插入任何类型的检查点,也不能使用“对象探测器”来查看其属性。
    接下来你可以继续完成整个sign up过程,还有多个类似的对象需要做虚拟对象.你也可以试一下?

  • 转载!(QTP使用中的一些好的方法)

    2008-07-20 18:48:13

     
    2008-06-01 22:52
    SystemUtil Object Descrīption
    An object used to control applications and processes during a run session.
    在脚本运行过程中,可使用该对象控制应用程序或进程.
    Operations
    The tables below list the built-in methods and properties that you can use as operations for the SystemUtil object. 下表列出了SystemUtil对象的内建方法与属性.
    Methods BlockInput Prevents keyboard and mouse input events from reaching applications.
    禁止键盘或鼠标操作.
    CloseDescendentProcesses Closes all processes opened by QuickTest.
    关闭由QuickTest打开的所有进程.
    CloseProcessByHwnd Closes a process that is the owner of a window with the specified handle.
    根据窗口的句柄找到相应的进程,并关闭该进程.
    CloseProcessById Closes a process according to its Process ID (PID).
    关闭指定进程PID的进程.
    CloseProcessByName Closes a process according to its name.
    关闭指定进程名称的进程.
    CloseProcessByWndTitle Closes all processes that are owners of windows with the specified title.
    关闭指定窗口标题的进程.
    Run Runs a file or application.
    运行文件或应用程序.
    UnblockInput Re-enables keyboard and mouse input events after a BlockInput statement
    was used to block them.
    在BlockInput语句禁止键盘或鼠标之后,使用UnblockInput语句可解除对键盘或
    鼠标的禁止.
    BlockInput Method Descrīption
    Prevents keyboard and mouse input events from reaching applications.
    禁止键盘或鼠标操作.
    Syntax object.BlockInput
    Argument Descrīption
    Object A test object of type SystemUtil.
    SystemUtil类型的测试对象.
    Return Value
    None
    Remarks
    You can use this method to prevent a run session being accidentally interrupted by someone using the keyboard or mouse on a QuickTest Professional computer.
    当电脑正在运行QTP脚本时,使用BlockInput方法可以防止因键盘或鼠标的操作扰乱QTP的正常运行.
    After using this method, keyboard and mouse input is blocked until one of the following occurs:
    使用BlockInput方法后,使用以下方法可以解除对键盘或鼠标的禁止:
    An UnblockInput statement is used
    当使用了UnblockInput语句后.
    A run session ends or is paused for any reason (end of test run, run error, breakpoint, and so forth)
    当脚本运行结束或中断(如整个test结束,运行错误,断点等等).
    The Ctrl+Alt+Delete key combination is pressed on the keyboard
    在键盘上按下Ctrl+Alt+Delete结合键后.
    A critical system error occurs
    当出现了严重的系统错误时.
    Example
    Block Keyboard and Mouse Input During a Run Session
    下面的例子使用BlockInput方法,在QTP脚本运行过程中禁用了键盘及鼠标.
    Sub BlockInput_Example()
    SystemUtil.BlockInput
    Browser("Welcome: Mercury Tours").Page("Welcome: Mercury Tours").WebEdit("userName").Set
    "mercury"
    Browser("Welcome: Mercury Tours").Page("Welcome: Mercury
    Tours").WebEdit("password").SetSecure "4082986e39ea469e70dbf8c5a29429fe138c6efc"
    Browser("Welcome: Mercury Tours").Page("Welcome: Mercury Tours").Image("Sign-In").Click 2,
    2 、SystemUtil.UnblockInput
    End Sub
    UnblockInput Method
    Descrīption
    Re-enables keyboard and mouse input events after a BlockInput statement was used to block them.
    在BlockInput语句禁止键盘或鼠标之后,使用UnblockInput语句可解除对键盘或鼠标的禁止.
    Syntax
    object.UnblockInput
    Argument Descrīption
    object A test object of type SystemUtil.
    SystemUtil类型的测试对象.
    Return Value
    None
    Remarks
    You can use this method to unblock keyboard and mouse input that was earlier blocked using a BlockInput
    statement.
    如果先前使用了BlockInput方法禁止了键盘及鼠标,使用UnblockInput方法可解除禁止.
    Example
    Unblock Keyboard and Mouse Input That Was Previously Blocked
    本例在开始使用BlockInput方法禁止了键盘及鼠标,后来使用UnblockInput方法解除了禁止.
    Sub UnblockInput_Example()
    SystemUtil.BlockInput
    Browser("Welcome: Mercury Tours").Page("Welcome: Mercury Tours").WebEdit("userName").Set
    "mercury"
    Browser("Welcome: Mercury Tours").Page("Welcome: Mercury
    Tours").WebEdit("password").SetSecure "4082986e39ea469e70dbf8c5a29429fe138c6efc"
    Browser("Welcome: Mercury Tours").Page("Welcome: Mercury Tours").Image("Sign-In").Click 2,
    2
    SystemUtil.UnblockInput
    End Sub
    CloseDescendentProcesses Method
    Descrīption
    Closes all processes opened by QuickTest.
    关闭由QuickTest打开的所有进程.
    Syntax
    object.CloseDescendentProcesses
    Argument Descrīption
    object A test object of type SystemUtil.
    SystemUtil类型的测试对象.
    Return Value
    A Long value. The number of instances of the application that are closed when the statement runs.
    一个长整型值.被CloseDescendentProcesses方法语句关闭的程序实例数.
    Remarks
    Note: QuickTest initially tries to close the process by sending a WM_CLOSE message to the process window. If
    the process is still open after 5 seconds, QuickTest terminates the process.
    注意:执行该语句时,QuickTest首先向进程窗口发送一个WM_CLOSE信息,偿试关闭该进程;如果该进程在5秒钟后存未
    关闭,则QuickTest强制结束进程.
    Example
    Close All Applications Opened By QuickTest
    在本例中:假定在运行程序之初,Record and Run对话框自动打开了某个窗口,然后通过Run语句打开了一个NotePad窗口,
    那么该CloseDscendentProcessed方法的返回值就就该是2,因此Message对话框显示的信息应该是"2".
    Sub CloseDescendentProcesses_Example()
    SystemUtil.Run "Notepad.exe"
    MsgBox SystemUtil.CloseDescendentProcesses
    End Sub
    CloseProcessByHwnd Method
    Descrīption
    Closes a process that is the owner of a window with the specified handle.
    根据窗口的句柄找到相应的进程,并关闭该进程.
    Syntax
    object.CloseProcessByHwnd (hWnd)
    Argument Descrīption
    object A test object of type SystemUtil.
    SystemUtil类型的测试对象.
    hWnd Required. An ULong object.
    The handle of the window owned by the process you want to close.
    Tip: You can retrieve the window handle using the hwnd property. For
    example:
    Window("MyAppName").GetROProperty("hwnd")
    必须.一个ULong对象.
    你想要关闭的进程的窗口的句柄.
    Tip:你可以通过获取Runtime窗口对象的hwnd属性值来获得句柄信息.例如:
    Window("MyAppName").GetRoProperty("hwnd")
    Return Value
    A Boolean value.
    一个Boolean值.
    True--The specified process was successfully closed.
    True――指定的进程已被成功关闭.
    False--The specified process was not closed.
    False――指定的进程未被关闭.
    Remarks
    Note: QuickTest initially tries to close the process by sending a WM_CLOSE message to the process window. If
    the process is still open after 5 seconds, QuickTest terminates the process.
    注意:执行该语句时,QuickTest首先向进程窗口发送一个WM_CLOSE信息,偿试关闭该进程;如果该进程在5秒钟后存未
    关闭,则QuickTest强制结束进程.
    Example
    Close the Process that is an Owner of a Window with the Specified Handle
    在本例中,首先通过GetroProperty方法获取NotePad窗口的句柄,然后使用CloseProcessByHwnd方法关闭Notepad程序.
    Sub CloseProcessByHwnd_Example()
    hWnd = Window("Notepad").GetROProperty("hwnd")
    SystemUtil.CloseProcessByHwnd (hWnd)
    End Sub
    CloseProcessById Method
    Descrīption
    Closes a process according to its Process ID (PID).
    根据进程的Process ID(PID)关闭进程.
    Syntax
    object.CloseProcessById (wdProcessId)
    Argument Descrīption
    object A test object of type SystemUtil.
    SystemUtil类型的测试对象.
    wdProcessId Required. An ULong object.
    The Process ID (PID) of the process you want to close.
    Tip: You can find the PID of an application by viewing the value in the
    Processes tab of the Windows Task Manager, or you can retrieve the value
    using the process id property. For example:
    Window("MyAppName").GetROProperty("process id")
    必须.一个ULong对象.
    你想关闭的进程的PID.
    Tip:在Windows的任务窗口的进程标签页可以查看到程序的PID值,你也可以通
    过获取Runtime窗口对象的Process id属性值来获得PID信息.例如:
    Window("MyAppName").GetRoProperty("process id")
    Return Value
    A Boolean value.
    一个Boolean值.
    True--The specified process was successfully closed.
    True――指定的进程已被正常关闭.
    False--The specified process was not closed.
    False――指定的进程未被关闭.
    Remarks
    Note: QuickTest initially tries to close the process by sending a WM_CLOSE message to the process window. If
    the process is still open after 5 seconds, QuickTest terminates the process.
    注意:执行该语句时,QuickTest首先向进程窗口发送一个WM_CLOSE信息,偿试关闭该进程;如果该进程在5秒钟后存未
    关闭,则QuickTest强制结束进程.
    Example
    Close an Application According to its Process ID
    下面的例子通过GetRoProperty方法获取Notepad窗口的PID值,然后使用CloseProcessById方法关闭Notepad程序.
    Sub CloseProcessById_Example()
    PID = Window("Notepad").GetROProperty("process id")
    SystemUtil.CloseProcessById (PID)
    End Sub
    CloseProcessByName Method
    Descrīption
    Closes a process according to its name.
    Syntax
    object.CloseProcessByName (bsProcessName)
    Argument Descrīption
    object A test object of type SystemUtil.
    SystemUtil类型的测试对象.
    bsProcessName Required. A String value.
    The name of the process you want to close.
    必须.一个字符串值.
    你想关闭的进程的名称.
    Return Value
    A Long value. The number of instances of the application that are closed when the statement runs.
    一个Long值.所关闭的程序的实例数.
    Remarks
    Note: QuickTest initially tries to close the process by sending a WM_CLOSE message to the process window. If
    the process is still open after 5 seconds, QuickTest terminates the process.
    注意:执行该语句时,QuickTest首先向进程窗口发送一个WM_CLOSE信息,偿试关闭该进程;如果该进程在5秒钟后存未
    关闭,则QuickTest强制结束进程.
    Example
    Close All Instances of a Specified Application
    本例通过CloseProcessByName方法,关闭所有已打开的Notepad实例.如果当前所有的Notepad实例都是由下面的Run语句打开的,则CloseProcessByName方法语句会关闭这些实例,并且MsgBox窗口显示的数值应为"3".
    Sub CloseProcessByName_Example()
    SystemUtil.Run "Notepad.exe"
    SystemUtil.Run "Notepad.exe"
    SystemUtil.Run "Notepad.exe"
    MsgBox SystemUtil.CloseProcessByName("Notepad.exe")
    End Sub
    CloseProcessByWndTitle Method
    Descrīption
    Closes all processes that are owners of windows with the specified title.
    关闭指定窗口标题的所有进程.
    Syntax
    object.CloseProcessByWndTitle (bsTitle, [bRegExp])
    Argument Descrīption
    object A test object of type SystemUtil.
    SystemUtil类型的测试对象.
    bsTitle Required. A String value.
    The title of the window owned by the process you want to close.
    必须.一个字符串值.
    你想关闭的进程的窗口的标题.
    bRegExp Optional. A Boolean value.
    Indicates whether the bsTitle argument is treated as a regular
    expression. Default = False.
    可选.一个Boolean值.
    检查BsTitle参数是否是一个正则表达式.默认值为False.
    Return Value
    A Long value. The number of instances of the application that are closed when the statement runs.
    一个Long值.被语句所关闭的程序的实例数.
    Remarks
    Note: QuickTest initially tries to close the process by sending a WM_CLOSE message to the process window. If
    the process is still open after 5 seconds, QuickTest terminates the process.
    注意:执行该语句时,QuickTest首先向进程窗口发送一个WM_CLOSE信息,偿试关闭该进程;如果该进程在5秒钟后存未关闭,则QuickTest强制结束进程.
    Example
    Close All Windows with a Title Containing a Specified String
    本例关闭了所有的标题包含有"Notepad"字样的顶层窗口.因为bRegExp参数值为"ture",表示bsTitle值是一个正则表达式.
    Sub CloseProcessByWndTitle_Example1()
    SystemUtil.CloseProcessByWndTitle "Notepad", True
    End Sub
    Close All Windows in which the Exact Title Matches a Specified String
    本例关闭所有窗口标题为"Untitled-Notepad"的顶层窗口.因为bRegExp参数值为"false",表示bsTitle参数值是一个文字串,因此窗口标题必须与参数值精确匹配.
    Sub CloseProcessByWndTitle_Example2()
    SystemUtil.CloseProcessByWndTitle "Untitled - Notepad"
    End Sub
    Run Method
    Descrīption
    Runs a file or application.
    运行一个文件或应用程序.
    Syntax
    object.Run file, [params], [dir], [op], [mode]
    Argument Descrīption
    object A test object of type SystemUtil.
    SystemUtil类型的测试对象.
    file Required. A String value.
    The name of the file you want to run.
    必须.一个字符串值.
    你想要运行的文件的名称.
    params Optional. A String value.
    If the specified file argument is an executable file, use the params argument to specify
    any parameters to be passed to the application.
    可选.一个字符串值.
    如果file参数中指定的是一个可执行文件,则可以使用params参数来指定该可执行文件的运行参数.
    dir Optional. A String value.
    The default directory of the application or file.
    可选.一个字符串值.
    应用程序或文件的默认目录.
    op Optional. A String value. The action to be performed. If this argument is blank ( ""), the
    open operation is performed.
    The following operations can be specified for the op argument:
    可选.一个字符串值.将要被执行的动作.如果该参数值为空(""),则默认操作为open操作.
    下面是op参数中可以指定的操作:
    Operation Descrīption
    open
    Opens the file specified by the FileName parameter. The file can be an
    executable file, a document file, or a folder. Non-executable files are
    open in the associated application.
    打开file参数中所指定的文件.文件有可能是一个可执行文件,一个文档文件,或
    一个文件夹.非可执行文件会被相应的应用程序打开.
    edit
    Launches an editor and opens the document for editing. If the FileName
    argument does not specify an editable document file, the statement
    fails.
    将文件在文件编辑器中打开以进行编辑.如果file参数中指定的文件不可编辑,则
    本语句失败.
    explore
    Explores the folder specified by the FileName argument.
    打开由file参数中所指定的文件夹.
    find
    Initiates a search starting from the specified folder path.
    在file参数所指定的文件夹中进行搜索.
    print
    Prints the document file specified by the FileName argument. If the
    specified file is not a printable document file, the statement fails.
    打印在file参数中所指定的文档.如果参数中所指定的文档不可打印,则语句失败.
    mode Optional. An Integer value.
    Specifies how the application is displayed when it opens. You can specify one of the modes
    in the table below.
    Default = 1
    可选.一个Integer值. 指定应用程序打开时的显示模式.可以指定以下几种打开模式.默认模式是1.
    Mode Descrīption
    0
    Hides the window and activates another window.
    隐藏程序窗口,激活另外一下窗口.
    1
    Activates and displays the window. If the window is minimized or maximized,
    the system restores it to its original size and position. Specify this flag when
    displaying the window for the first time.
    激活并显示程序窗口.如果窗口是最大化或最小化状态,则将它恢复为默认窗口大小并放
    置于默认位置.
    2
    Activates the window and displays it as a minimized window.
    激活窗口并最小化.
    3
    Activates the window and displays it as a maximized window.
    激活窗口并最大化.
    4
    Displays the window in its most recent size and position. The active window
    remains active.
    按照窗口最近一次的显示大小及位置显示该窗口,但不激活该窗口.原来活动的窗口仍保
    持活动状态.
    5
    Activates the window and displays it in its current size and position.
    激活窗口,保持当前的大小及位置不变.
    6
    Minimizes the specified window and activates the next top-level window in the
    Z order.
    最小化指定的窗口,并激活它的下一个top-level窗口(按Z顺序找到它的下一下top-level
    窗口).
    7
    Displays the window as a minimized window. The active window remains active.
    显示窗口并最小化,但不激活该窗口.原来活动的窗口仍保持活动状态.
    8
    Displays the window in its current state. The active window remains active.
    显示窗口,大小及位置保存不变.原来活动的窗口仍保持活动状态.
    9
    Activates and displays the window. If the window is minimized or maximized,
    the system restores it to its original size and position. Specify this flag when
    restoring a minimized window.
    激活并显示窗口.如果窗口处于最大化或最小化状态,则将它恢复为原始大小与位置.
    10
    Sets the show-state based on the state of the program that started the
    application.
    Return Value
    None
    Remarks
    When specifying a non-executable file, the file opens in the associated application.
    当指定的是一个非可执行文件时,文件被相应的应用程序打开.
    Note: A SystemUtil.Run statement is automatically added to your test when you run an application from the Start menu or the Run dialog box while recording a test.
    注意:在录制脚本的过程中,如果你从系统的"开始"菜单或"运行"对话框中运行应用程序时,SystemUtil.Run语句会自动添加到脚本中.
    Tip: You can also use this method to perform operations on the specified file, similar to the usage of the Windows ShellExecute command.
    Tip:你也可以使用本方法对指定的文件进行操作,该功能类似于使用Windows ShellExecute命令.
    Example
    Open a Text File in the Default Text Application (Notepad)
    本例使用Run方法打开了一个名为type.txt的文件(txt类型文件的默认打开程序是Notpad),然后向文件中输入"happy days"文字,然后使用快捷键保存该文件,然后关闭NotePad应用程序.
    Sub CloseDescendentProcesses_Example()
    SystemUtil.Run "C:\type.txt", "", "", ""
    Window("Text:=type.txt - Notepad").Type "happy days"
    Window("Text:=type.txt - Notepad").Type micAltDwn & "F" & micAltUp
    Window("Text:=type.txt - Notepad").Type micLShiftDwn & "S" & micLShiftUp
    Window("Text:=type.txt - Notepad").Close
    End Sub
  • QTP学习 - 录制运行设置(四)

    2008-06-16 12:06:15

    5. 录制模式选择

    分为三种录制模式

    ·       正常录制模式

    ·       模拟录制模式

    ·       低级录制模式

    正常录制

    QTP默认为正常录制模式,此类模式继承了对象模型的所有优点,他通过识别程序中的对象来代替以前屏幕坐标的形式。但是正常模式并不能保证程序中所有的对象因此还需要其它两种模式来补充

    模拟录制

    模拟录制模式(Analog Recording)

        此类模式录制了所有键盘和鼠标的操作,这类方式适用于不能录制对象模式时适用。

        请注意这种方式是不能由QTP进行编辑的。

        记录鼠标的轨迹,键盘的动作,:鼠标拖动,点击等等

    Note:

         选择模拟录制模式,如果在回放时,改变了屏幕的分辨率或者窗口/屏幕的位置, 回放就会失败

    低级录制

    低级录制模式(Low Level Recording)

        此类方式录制的对象都以WindowWinObject的形式存在。

        在低级录制模式下,QTP能够按照屏幕上的x坐标和y坐标

        录制该对象,将所有父类对象录制为Windows测试对象,

        将所有的其他对象录制为WinObject测试对象。它们在ActiveScreen中显示为标准Windows对象

        这类方式适用于QTP不能正常识别对象时应用

    主要是记录坐标的位置,可以对QTP不支持的对象的记录.

     

  • QTP学习 - 录制运行设置(三)

    2008-06-16 12:03:25

    4.    Test Settings

    Properties

    包含测试脚本信息,如作者,使用了哪些插件,脚本路径….

    Run

    1.       运行global Data Table的方式: 第一行,所有行,指定行

    2.       设置运行时出现错误时的处理方式:

    1. 弹出信息对话框

    2. 执行下一个action

    3. 停止运行

    4. 执行下一步

    3.       Object synchronization timeout[ ]: 设置执行步骤等待对象同步的最长时间.

    Note: 若是和web对象一起工作时,QTP等待的最长时间为 这个时间加上Browser navigation timeout (Web选项页)设置的时间

     

    Resources

    添加一些之前设置的公共函数(Tools-options里和Test settings里保存的公共函数.)

    Parameters

    浏览,增加,修改,删除 参数.

    Environment

    环境变量设置(内部,外部)

    Web

    Browser navigation timeout[ ]: 设置运行测试步骤之前等待网页加载时间的最大值

     

    Recovery

    场景恢复

    运行测试过程中,出现意外的处理方式.

    可以通过 Resource -> Recovery scenario manager…向导设置,如运行时弹出窗口,程序crash….

     

    以上讲了设置的几个时间:

    1.       Add[ ] seconds to page load time: 增加运行时页面加载时间(解决运行时页面加载时间超过录制时的加载时间而出错的问题)

    2.       Object synchronization timeout[ ]: 设置执行步骤等待对象同步的最长时间.( 若是和web对象一起工作时,QTP等待的最长时间为 这个时间加上Browser navigation timeout设置的时间)

    3.       Browser navigation timeout[ ]: 设置运行测试步骤之前等待网页加载时间的最大值

     

  • QTP学习 - 录制运行设置(二)

    2008-06-16 12:01:52

    2.    Record and Run Settings

    Web

    比较常用的设置:可以指定打开浏览器的类型.

     

    Windows Application

    指定录制应用程序

    【注】Applications opened by Quick Test 开启录制之后打开的应用程序。

          Applications opened via the Desktop 录制通过桌面,开始菜单,快速启动,windows资源管理器打开的应用程序(包括开启录制之前和之后打开的)

    3.    Tools-> Options

    General

    General选项里面要注意的设置有三点:

    1.       Disable recognition of virtual objects while recording

    录制时禁用虚拟对象识别

    QTP在创建好虚拟对象后,如果选择了此项,则在录制到要用虚拟对象的时候,录制结束后,代码中会直接把要用虚拟对象的地方省略

    2.       Automatically update test and component steps when you rename test objects.

    当重命名测试库中对象后自动更新测试和组件步骤

    3.       Generate scrīpt

    导出此对话框(包括其他选项页)的设置为scrīpt,保存为共用函数(通过Test settingresources选项页调用)

     

    Folders

    通过此页可以设置当前测试脚本路径.

    Active Screen

    相当于快速预览窗口

    对测试结果分析很有帮助的一个设置,注意此项设置记录的信息越多就越容易编辑录制之后的测试。但是,保存更多的信息将会增加录制时间和磁盘空间

    此选项页中的设置

    1.       自定义捕获级别  默认为局部捕获,可以根据自己的测试改变设置,如想提高录制运行速度且需要观察Active screen,则可以设置为None.

    2.       高级设置 可以设置在AS屏是否运行脚本,且加载什么控件.

        Load ActiveX controls 

    加载之解决当前安全设置禁止运行该页中的ActiveX控件.因此,该页可能无法正常打开问题

    Load images

    加载之后解决图片显示问题

    Load Java applets

    加载之后解决java applets 问题

     

    Run

    必须要安装Microsoft scrīpt Debugger才能使用

    设置以下常用选项:

    1.       运行模式(Normalfast)  Normal可以设置运行步骤间隔时间且会有当前运行步骤提示.

                           Fast不能设置步骤运行时间间隔也没有步骤运行提示

    【注】当从QC运行测试时,即使选择了Normal,也会自动设为Fast模式

    2.       涉及和QC的链接

    3.       设置运行测试是否自动显示测试结果

    4.       设置测试结果中保留运行时的截图和录像(所有步骤/错误/警告)

    Windows Applications

    很少用的设置

    Web

    加载web控件时可用.

    1.       设置不录制的地址

    2.       设置忽略QC的连接

    3.       Add[ ] seconds to page load time: 增加页面加载时间

    4.       坏链接测试(指向本地主机),要想设置指向所有主机链接的测试,通过本页高级现象设置自动添加链接检查检查点测试.

    5.       Page and Fame option 此项尽量保持默认设置(有些外部插件会自动修改设置为最优,如果使用了外部插件,建议保持默认设置,不要使用reset按钮 )

    6.       高级设置

    添加Accessibility检查点  在这里设置之后,才可以在脚本里编辑添加Accessibility检查点

    添加自动检查点    设置此项之后,录制时自动在测试的每个页面添加已设置的检查.

  • QTP学习 - 录制运行设置(一)

    2008-06-16 11:48:26

    1.    录制前IE设置

    l  确保你的IE运行正常

    依次点击菜单 查看 --> 工具栏,一定要将上网助手等插件卸载掉,如3721和一些拦截广告的软件

     

    l  修改浏览器的设定,不让浏览器帮你自动输入帐号和密码,确保所有操作都会录制下来。

    取消[自动完成]的设定

        1. 开启IE浏览器,点选[工具]-[Internet选项]-[内容]   

        2. 点选 个人信息中的[自动完成],进入自动完成设置.

        3. 取消[表单上的用户名和密码]选项

        4. 点选确定,关闭对话框

     

     

    l  ActiveX控件设置 IE-> 工具-> Internet 选项->  安全->  自定义级别

     

    l  解决录制过程中显示弹出窗口问题 IE-> 工具->Internet 选项->安全->自定义级别

我的栏目

数据统计

  • 访问量: 5022
  • 日志数: 8
  • 建立时间: 2008-06-16
  • 更新时间: 2008-07-20

RSS订阅