揭祕QTP的DeviceReplay對象

注:本文最早發表在《51測試天地》第九期

揭祕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 "<country-region><place><span lang="EN-US">United States of America</span></place></country-region>"

deviceReplay.PressKey VK_RETURN

deviceReplay.PressNKeys VK_TAB, 3

deviceReplay.PressNKeys VK_MULTIPLY, Len( "<country-region><place><span lang="EN-US">United States of America</span></place></country-region>" )

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( "<state><place><span lang="EN-US">Alabama</span></place></state>", "AL", "Montgomery" )

Call PrintRow( "<state><span lang="EN-US">Alaska</span></state>", "AK", "<city><place><span lang="EN-US">Juneau</span></place></city>" )

Call PrintRow( "<state><span lang="EN-US">Arizona</span></state>", "AZ", "<city><place><span lang="EN-US">Phoenix</span></place></city>" )

Call PrintRow( "<state><span lang="EN-US">Arkansas</span></state>", "AR", "<city><place><span lang="EN-US">Little Rock</span></place></city>" )

Call PrintRow( "<state><span lang="EN-US">California</span></state>", "CA", "<city><place><span lang="EN-US">Sacramento</span></place></city>" )

Call PrintRow( "<state><span lang="EN-US">Colorado</span></state>", "CO", "<city><place><span lang="EN-US">Denver</span></place></city>" )

Call PrintRow( "<state><span lang="EN-US">Connecticut</span></state>", "CT", "<city><place><span lang="EN-US">Hartford</span></place></city>" )

Call PrintRow( "<state><span lang="EN-US">Delaware</span></state>", "DE", "<city><place><span lang="EN-US">Dover</span></place></city>" )

Call PrintRow( "<state><span lang="EN-US">Florida</span></state>", "FL", "<city><place><span lang="EN-US">Tallahassee</span></place></city>" )

Call PrintRow( "Georgia", "GA", "<city><place><span lang="EN-US">Atlanta</span></place></city>" )

Call PrintRow( "<state><span lang="EN-US">Hawaii</span></state>", "HA", "<city><place><span lang="EN-US">Honolulu</span></place></city>" )

Call PrintRow( "<state><span lang="EN-US">Idaho</span></state>", "ID", "<city><place><span lang="EN-US">Boise</span></place></city>" )

Call PrintRow( "<state><span lang="EN-US">Illinois</span></state>", "IL", "<city><place><span lang="EN-US">Springfield</span></place></city>" )

Call PrintRow( "Indiana", "IN", "<city><place><span lang="EN-US">Indianapolis</span></place></city>" )

Call PrintRow( "<state><span lang="EN-US">Iowa</span></state>", "IA", "<city><place><span lang="EN-US">Des Moines</span></place></city>" )

Call PrintRow( "<state><span lang="EN-US">Kansas</span></state>", "KS", "<city><place><span lang="EN-US">Topeka</span></place></city>" )

Call PrintRow( "<state><span lang="EN-US">Kentucky</span></state>", "KY", "<city><place><span lang="EN-US">Frankfort</span></place></city>" )

Call PrintRow( "<state><span lang="EN-US">Louisiana</span></state>", "LA", "<city><place><span lang="EN-US">Baton Rouge</span></place></city>" )

Call PrintRow( "<state><place><span lang="EN-US">Maine</span></place></state>", "ME", "Augusta" )

Call PrintRow( "<state><span lang="EN-US">Maryland</span></state>", "MD", "<city><place><span lang="EN-US">Annapolis</span></place></city>" )

Call PrintRow( "<state><span lang="EN-US">Massachusetts</span></state>", "MA", "<city><place><span lang="EN-US">Boston</span></place></city>" )

Call PrintRow( "<state><place><span lang="EN-US">Michigan</span></place></state>", "MI", "Lansing" )

Call PrintRow( "<state><span lang="EN-US">Minnesota</span></state>", "MN", "<city><place><span lang="EN-US">Saint Paul</span></place></city>" )

Call PrintRow( "<state><place><span lang="EN-US">Mississippi</span></place></state>", "MS", "Jackson" )

Call PrintRow( "<state><span lang="EN-US">Missouri</span></state>", "MO", "<city><place><span lang="EN-US">Jefferson City</span></place></city>" )

Call PrintRow( "<state><place><span lang="EN-US">Montana</span></place></state>", "MT", "Helena" )

Call PrintRow( "<state><place><span lang="EN-US">Nebraska</span></place></state>", "NE", "Lincoln" )

Call PrintRow( "<state><span lang="EN-US">Nevada</span></state>", "NV", "<city><place><span lang="EN-US">Carson City</span></place></city>" )

Call PrintRow( "<state><span lang="EN-US">New Hampshire</span></state>", "NH", "<city><place><span lang="EN-US">Concord</span></place></city>" )

Call PrintRow( "<state><span lang="EN-US">New Jersey</span></state>", "NJ", "<city><place><span lang="EN-US">Trenton</span></place></city>" )

Call PrintRow( "<state><span lang="EN-US">New Mexico</span></state>", "NM", "<city><place><span lang="EN-US">Santa Fe</span></place></city>" )

Call PrintRow( "<state><span lang="EN-US">New York</span></state>", "NY", "<city><place><span lang="EN-US">Albany</span></place></city>" )

Call PrintRow( "<state><place><span lang="EN-US">North Carolina</span></place></state>", "NC", "Raleigh" )

Call PrintRow( "<state><place><span lang="EN-US">North Dakota</span></place></state>", "ND", "Bismarck" )

Call PrintRow( "<state><span lang="EN-US">Ohio</span></state>", "OH", "<city><place><span lang="EN-US">Columbus</span></place></city>" )

Call PrintRow( "<state><span lang="EN-US">Oklahoma</span></state>", "OK", "<city><place><span lang="EN-US">Oklahoma City</span></place></city>" )

Call PrintRow( "<state><span lang="EN-US">Oregon</span></state>", "OR", "<city><place><span lang="EN-US">Salem</span></place></city>" )

Call PrintRow( "<state><span lang="EN-US">Pennsylvania</span></state>", "PA", "<city><place><span lang="EN-US">Harrisburg</span></place></city>" )

Call PrintRow( "<state><span lang="EN-US">Rhode Island</span></state>", "RI", "<city><place><span lang="EN-US">Providence</span></place></city>" )

Call PrintRow( "<state><span lang="EN-US">South Carolina</span></state>", "SC", "<city><place><span lang="EN-US">Columbia</span></place></city>" )

Call PrintRow( "<state><place><span lang="EN-US">South Dakota</span></place></state>", "SD", "Pierre" )

Call PrintRow( "<state><span lang="EN-US">Tennessee</span></state>", "TN", "<city><place><span lang="EN-US">Nashville</span></place></city>" )

Call PrintRow( "<state><place><span lang="EN-US">Texas</span></place></state>", "TX", "Austin" )

Call PrintRow( "<state><span lang="EN-US">Utah</span></state>", "UT", "<city><place><span lang="EN-US">Salt Lake City</span></place></city>" )

Call PrintRow( "<state><span lang="EN-US">Vermont</span></state>", "VT", "<city><place><span lang="EN-US">Montpelier</span></place></city>" )

Call PrintRow( "Virginia", "VA", "<city><place><span lang="EN-US">Richmond</span></place></city>" )

Call PrintRow( "Washington", "WA", "<city><place><span lang="EN-US">Olympia</span></place></city>" )

Call PrintRow( "<state><span lang="EN-US">West Virginia</span></state>", "WV", "<city><place><span lang="EN-US">Charleston</span></place></city>" )

Call PrintRow( "<state><place><span lang="EN-US">Wisconsin</span></place></state>", "WI", "Madison" )

Call PrintRow( "<state><span lang="EN-US">Wyoming</span></state>", "WY", "<city><place><span lang="EN-US">Cheyenne</span></place></city>" )

Set deviceReplay = Nothing

2 – 拉丁文和字符

Option Explicit

Const VK_NUMPAD0 = 82

Const VK_NUMPAD1 = 79

Const VK_NUMPAD2 = 80

Const VK_NUMPAD3 = 81

Const VK_NUMPAD4 = 75

Const VK_NUMPAD5 = 76

Const VK_NUMPAD6 = 77

Const VK_NUMPAD7 = 71

Const VK_NUMPAD8 = 72

Const VK_NUMPAD9 = 73

Const VK_MENU = 56

Const VK_SHIFT = 42

Const VK_RETURN = 28

Const VK_F = 33

Const VK_O = 24

Const VK_TAB = 15

Const VK_F5 = 63

Const VK_NUMLOCK = 69

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 PrintCharacter( ByVal code )

Dim i, digit

deviceReplay.KeyDown VK_MENU

For i = 1 To Len( code )

digit = Mid( code, i, 1 )

Execute "deviceReplay.PressKey VK_NUMPAD" & digit

Next

deviceReplay.KeyUp VK_MENU

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

' ** a grave character

deviceReplay.SendString "A grave: "

Call PrintCharacter( "0192" )

' ** O circumflex character

deviceReplay.SendString "O circumflex: "

Call PrintCharacter( "0212" )

' ** s caron character

deviceReplay.SendString "s caron: "

Call PrintCharacter( "0154" )

' ** n tilde character

deviceReplay.SendString "n tilde: "

Call PrintCharacter( "164" )

' ** Y umlaut character

deviceReplay.SendString "Y umlaut: "

Call PrintCharacter( "0159" )

' ** c cedila character

deviceReplay.SendString "c cedila: "

Call PrintCharacter( "0231" )

' ** O with accent character

deviceReplay.SendString "O with accent: "

Call PrintCharacter( "0211" )

' ** Inverted question mark character

deviceReplay.SendString "Inverted question mark: "

Call PrintCharacter( "168" )

' ** Euro character

deviceReplay.SendString "Euro: "

Call PrintCharacter( "0128" )

' ** i with accent character

deviceReplay.SendString "i with accent : "

Call PrintCharacter( "0237" )

' ** Male Sign character

deviceReplay.SendString "Male Sign: "

Call PrintCharacter( "11" )

' ** AE ligature character

deviceReplay.SendString "AE ligature: "

Call PrintCharacter( "0198" )

' ** aa character

deviceReplay.SendString "aa: "

Call PrintCharacter( "0197" )

' ** oethel character

deviceReplay.SendString "oethel: "

Call PrintCharacter( "0156" )

' ** Eth character

deviceReplay.SendString "Eth: "

Call PrintCharacter( "0208" )

' ** Uppercase Sigma character

deviceReplay.SendString "Uppercase Sigma: "

Call PrintCharacter( "228" )

Set deviceReplay = Nothing

DragAndDrop方法

描述

用於執行從一點拖動到另外一點的操作。

語法

object.DragAndDrop( dragX, dragY, dropX, dropY, Button )

參數

object Mercury.DeviceReplay對象。

dragX :起點座標的X軸的值。

dragY :起點座標的Y軸的值。

dropX :終點座標的X軸的值。

dropY :終點座標的Y軸的值。

Button :可能的值包括

LEFT_MOUSE_BUTTON = 0

MIDDLE_MOUSE_BUTTON = 1

RIGHT_MOUSE_BUTTON = 2

返回值

無。

提示

可以組合使用MouseDownMouseMoveMouseUp方法。

MouseClick方法

描述

在指定的屏幕位置執行鼠標左鍵或右鍵的單擊操作。

語法

object.MouseClick( x, y, Button )

參數

object Mercury.DeviceReplay對象。

x :屏幕座標X軸的值。

y :屏幕座標Y軸的值。

Button :可能的值包括

LEFT_MOUSE_BUTTON = 0

MIDDLE_MOUSE_BUTTON = 1

RIGHT_MOUSE_BUTTON = 2

返回值

無。

例子

下面的例子在執行之前需要做一定的準備工作。例子的目的是在www.advancedqtp.com網站上執行DragAndDrop操作,如果在錄製時執行拖拽操作,則不會被錄製下來。因此這個例子是支持某些操作的例子。這個例子在IE環境下測試通過。

打開IE瀏覽器並導航到www.advancedqtp.com。這個例子會交換dbxhandle項,這些對象可以被拖拽以便滿足個性化顯示的要求。

<shapetype id="_x0000_t75" stroked="f" filled="f" path="[email protected]@5[email protected]@[email protected]@[email protected]@5xe" o:preferrelative="t" o:spt="75" coordsize="21600,21600"><stroke joinstyle="miter"></stroke><formulas><f eqn="if lineDrawn pixelLineWidth 0"></f><f eqn="sum @0 1 0"></f><f eqn="sum 0 0 @1"></f><f eqn="prod @2 1 2"></f><f eqn="prod @3 21600 pixelWidth"></f><f eqn="prod @3 21600 pixelHeight"></f><f eqn="sum @0 0 1"></f><f eqn="prod @6 1 2"></f><f eqn="prod @7 21600 pixelWidth"></f><f eqn="sum @8 21600 0"></f><f eqn="prod @7 21600 pixelHeight"></f><f eqn="sum @10 21600 0"></f></formulas><path o:connecttype="rect" gradientshapeok="t" o:extrusionok="f"></path><lock aspectratio="t" v:ext="edit"></lock></shapetype><shape id="_x0000_i1025" style="WIDTH: 203.25pt; HEIGHT: 423.75pt" type="#_x0000_t75"><imagedata o:title="" src="file:///D:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtml1/01/clip_image001.emz"></imagedata></shape>

打開QTP(加載Web插件),新建一個測試,打開對象庫(object repository)並添加瀏覽器中的頁面對象到本地對象庫(local object repository)中。

<shape id="_x0000_i1026" style="WIDTH: 414.75pt; HEIGHT: 300.75pt" type="#_x0000_t75"><imagedata o:title="" src="file:///D:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtml1/01/clip_image003.emz"></imagedata></shape>

重命名對象

<shape id="_x0000_i1027" style="WIDTH: 414.75pt; HEIGHT: 300.75pt" type="#_x0000_t75"><imagedata o:title="" src="file:///D:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtml1/01/clip_image005.emz"></imagedata></shape>

Option Explicit

Const LEFT_MOUSE_BUTTON = 0

Dim oWebElemDesc1, oWebElemDesc2

Dim oWebElem1, oWebElem2

Dim devRep

Dim nX1, nX2, nY1, nY2, nH1, nH2, hwnd

Dim point1, point2

' ** This class holds a point coordinate

Class Point

Private mX, mY

Property Let X( ByVal value )

mX = value

End Property

Property Get X()

X = mX

End Property

Property Let Y( ByVal value )

mY = value

End Property

Property Get Y()

Y = mY

End Property

End Class

' ** Retrieving the handle of the browser

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

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

' ** Create a description for 'Program Professionally'

Set oWebElemDesc1 = Description.Create()

oWebElemDesc1( "micclass" ).Value = "WebElement"

oWebElemDesc1( "html tag" ).Value = "H3"

oWebElemDesc1( "innertext" ).Value = "Program Professionally"

oWebElemDesc1( "class" ).Value = "dbx-handle dbx-handle-cursor"

' ** Create a description for 'Links'

Set oWebElementDesc2 = Description.Create()

oWebElemDesc2( "micclass" ).Value = "WebElement"

oWebElemDesc2( "html tag" ).Value = "H3"

oWebElemDesc2( "innertext" ).Value = "Links"

oWebElemDesc2( "class" ).Value = "dbx-handle dbx-handle-cursor"

' ** Searching for the elements

With Browser( "QTP" ).Page( "QTP" )

If .ChildObjects( oWebElemDesc1 ).Count = 1 Then

Set oWebElem1 = .WebElement( oWebElemDesc1 )

If .ChildObjects( oWebElemDesc2 ).Count = 1 Then

Set oWebElem2 = .WebElement( oWebElemDesc2 )

Else

Print "Web Element 'Program Professionally' was not found."

ExitTest( micFail )

End If

Else

Print "Web Element 'Program Professionally' was not found."

ExitTest( micFail )

End If

End With

' ** Retrieve elements dimensions

nX1 = oWebElem1.GetROProperty( "abs_x" )

nH1 = oWebElem1.GetROProperty( "height" )

nY1 = oWebElem1.GetROProperty( "abs_y" )

nX2 = oWebElem2.GetROProperty( "abs_x" )

nH2 = oWebElem2.GetROProperty( "height" )

nY2 = oWebElem2.GetROProperty( "abs_y" )

Set point1 = New Point

point1.X = nX1 + 10

point1.Y = nY1 + nH1 - 10

Set point2 = New Point

' ** Dragging up

If nY1 > nY2 Then

point2.X = nX2 + 20

point2.Y = nY2 + nH2 - 20

Else

' ** Dragging down

point2.X = nX2 + 20

point2.Y = nY2 + nH2 + 20

End If

Set devRep = CreateObject( "Mercury.DeviceReplay" )

devRep.DragAndDrop point1.X, point1.Y, _

point2.X, point2.Y, LEFT_MOUSE_BUTTON

MouseDbClick方法

描述

在指定的屏幕位置中執行鼠標左鍵或右鍵的雙擊事件。

語法

object.MouseDblClick( x, y, Button )

參數

object Mercury.DeviceReplay對象。

x :屏幕座標X軸的值。

y :屏幕座標Y軸的值。

Button :可能的值包括

LEFT_MOUSE_BUTTON = 0

MIDDLE_MOUSE_BUTTON = 1

RIGHT_MOUSE_BUTTON = 2

返回值

MouseDown方法

描述

在屏幕指定位置按下鼠標左鍵或右鍵,並保持按下狀態。

語法

object.MouseDown( x, y, Button )

參數

object Mercury.DeviceReplay對象。

x :屏幕座標X軸的值。

y :屏幕座標Y軸的值。

Button :可能的值包括

LEFT_MOUSE_BUTTON = 0

MIDDLE_MOUSE_BUTTON = 1

RIGHT_MOUSE_BUTTON = 2

返回值

提示

應該在MouseDown後使用對應的MouseUp方法。

MouseUp方法

描述

用於釋放之前執行的MouseDown方法所按下的鼠標按鍵。

語法

object.MouseDown( x, y, Button )

參數

object Mercury.DeviceReplay對象。

x :屏幕座標X軸的值。

y :屏幕座標Y軸的值。

Button :可能的值包括

LEFT_MOUSE_BUTTON = 0

MIDDLE_MOUSE_BUTTON = 1

RIGHT_MOUSE_BUTTON = 2

返回值

提示

應該讓MouseUpMouseDowun方法配對使用。

MouseMove方法

描述

用於釋放之前執行的MouseDown方法所按下的鼠標按鍵。(譯者注:這裏懷疑是作者筆誤,應該是:用於模擬鼠標移動。

語法

object.MouseDown( x, y ) 譯者注:這裏懷疑是作者筆誤,應該是:object.MouseMove( x, y )

參數

object Mercury.DeviceReplay對象。

x :屏幕座標X軸的值。

y :屏幕座標Y軸的值。

返回值

提示

調試腳本查看在運行時獲取到的座標位置。

在執行鍵盤操作之前移動鼠標到指定的位置並設置焦點。

SetSynchronizationTimeout方法

描述

設置一個新的同步超時的時間值。

語法

object.MouseDown( x, y ) 譯者注:這裏懷疑是作者筆誤,應該是:object. SetSynchronizationTimeoutnSyncTimeout , is_sec)。

參數

object Mercury.DeviceReplay對象。

nSyncTimeout 同步超時的時間值。

is_sec 指定設置的時間值是否以秒爲單位。

返回值

提示

建議不要修改這個值。

Key Codes參考

<shape id="_x0000_i1028" style="WIDTH: 415.5pt; HEIGHT: 183.75pt" type="#_x0000_t75"><imagedata o:title="" src="file:///D:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/msohtml1/01/clip_image007.emz"></imagedata></shape>

常量

#

描述

VK_ESCAPE

1

主要用於取消命令操作,或者從一個處理過程中退出。

VK_1

2

數字鍵1,與VK_SHIFT鍵組合將輸入某個特殊字符(取決於鍵盤佈局)。

VK_2

3

數字鍵2,與VK_SHIFT鍵組合將輸入某個特殊字符(取決於鍵盤佈局)

VK_3

4

數字鍵3,與VK_SHIFT鍵組合將輸入某個特殊字符(取決於鍵盤佈局)。

VK_4

5

數字鍵4,與VK_SHIFT鍵組合將輸入某個特殊字符(取決於鍵盤佈局)

VK_5

6

數字鍵5,與VK_SHIFT鍵組合將輸入某個特殊字符(取決於鍵盤佈局)。

VK_6

7

數字鍵6,與VK_SHIFT鍵組合將輸入某個特殊字符(取決於鍵盤佈局)。

VK_7

8

數字鍵7,與VK_SHIFT鍵組合將輸入某個特殊字符(取決於鍵盤佈局)。

VK_8

9

數字鍵8,與VK_SHIFT鍵組合將輸入某個特殊字符(取決於鍵盤佈局)。

VK_9

10

數字鍵9,與VK_SHIFT鍵組合將輸入某個特殊字符(取決於鍵盤佈局)。

VK_0

11

數字鍵0,與VK_SHIFT鍵組合將輸入某個特殊字符(取決於鍵盤佈局)。

VK_MINUS

12

減號(-)或下劃線(_),(取決於鍵盤佈局)。

VK_EQUAL

13

等號(=)或加號(+),(取決於鍵盤佈局)。

VK_BACKSPACE

14

用於刪除光標左邊的字符,可通過組合單擊和拖拽操作來高亮顯示文本,然後單擊BACKSPACE鍵來刪除所選文本。

VK_TAB

15

TAB按鍵可用於在文檔中設置TAB位置,移動光標到指定位置,與SHIFT鍵組合可以跳轉到前一個TAB位置。

VK_Q

16

SHIFT按鍵組合或按下CAPS-LOCK表示大寫字母Q

VK_W

17

SHIFT按鍵組合或按下CAPS-LOCK表示大寫字母W

VK_E

18

SHIFT按鍵組合或按下CAPS-LOCK表示大寫字母E

VK_R

19

SHIFT按鍵組合或按下CAPS-LOCK表示大寫字母R

VK_TAB

20

用於在文檔中設置TAB位置,移動光標到指定位置,與SHIFT鍵組合可以跳轉到前一個TAB位置。

VK_Y

21

SHIFT按鍵組合或按下CAPS-LOCK表示大寫字母Y

VK_U

22

SHIFT按鍵組合或按下CAPS-LOCK表示大寫字母U

VK_I

23

SHIFT按鍵組合或按下CAPS-LOCK表示大寫字母I

VK_O

24

SHIFT按鍵組合或按下CAPS-LOCK表示大寫字母O

VK_P

25

SHIFT按鍵組合或按下CAPS-LOCK表示大寫字母P

VK_OBRACKET

26

左中括號[或左大括號{,取決於鍵盤佈局。

VK_CBRACKET

27

右中括號[或右大括號{,取決於鍵盤佈局。

VK_RETURN

28

回車鍵用於把光標設置到下一行的開頭。也被用於完成一個命令並告訴計算機執行下一條命令。

VK_CONTROL

29

CTRL鍵,用於與其它按鍵組合來執行特定的命令,例如CRL/ALT/DEL執行系統重啓命令。

VK_A

30

SHIFT按鍵組合或按下CAPS-LOCK表示大寫字母A

VK_S

31

SHIFT按鍵組合或按下CAPS-LOCK表示大寫字母S

VK_D

32

SHIFT按鍵組合或按下CAPS-LOCK表示大寫字母D

VK_F

33

SHIFT按鍵組合或按下CAPS-LOCK表示大寫字母F

VK_G

34

SHIFT按鍵組合或按下CAPS-LOCK表示大寫字母G

VK_H

35

SHIFT按鍵組合或按下CAPS-LOCK表示大寫字母H

VK_J

36

SHIFT按鍵組合或按下CAPS-LOCK表示大寫字母J

VK_K

37

SHIFT按鍵組合或按下CAPS-LOCK表示大寫字母K

VK_L

38

SHIFT按鍵組合或按下CAPS-LOCK表示大寫字母L

VK_SEMICOLON

39

分號(;)或冒號(:),取決於鍵盤佈局。

VK_SINGLE_QUOTE

40

單引號(‘)或雙引號(「) ,取決於鍵盤佈局。

VK_APOSTROPHE

41

波浪號(~)或(`),取決於鍵盤佈局。

VK_SHIFT

42

把小寫字母換成大寫字母,把數字換成特殊字符。

VK_PIPE

43

斜線(/)或豎線(|),取決於鍵盤佈局。

VK_Z

44

SHIFT按鍵組合或按下CAPS-LOCK表示大寫字母Z

VK_X

45

SHIFT按鍵組合或按下CAPS-LOCK表示大寫字母X

VK_C

46

SHIFT按鍵組合或按下CAPS-LOCK表示大寫字母C

VK_V

47

SHIFT按鍵組合或按下CAPS-LOCK表示大寫字母V

VK_B

48

SHIFT按鍵組合或按下CAPS-LOCK表示大寫字母B

VK_N

49

SHIFT按鍵組合或按下CAPS-LOCK表示大寫字母N

VK_M

50

SHIFT按鍵組合或按下CAPS-LOCK表示大寫字母M

VK_COMMA

51

逗號(,)或左尖括號(<),取決於鍵盤佈局。

VK_DOT

52

點號(.)或右尖括號(>),取決於鍵盤佈局。

VK_SLASH

53

反斜槓號(/)或問號(?),取決於鍵盤佈局。

VK_RSHIFT

54

把小寫字母換成大寫字母,把數字換成特殊字符。

VK_MULTIPLY

55

乘號(*

VK_MENU

56

用於與其它鍵組合輸入關鍵字。也用於組合數字鍵盤上的按鍵來輸入ASCII碼。

VK_SPACE

57

空格鍵

VK_CAPITAL

58

CAPS LOCK

VK_F1

59

DOS時代,F1通常用於調出幫助菜單,顯示幫助信息。在Windows操作系統中也作爲顯示幫助信息的熱鍵。

VK_F2

60

Windows中可用於重命名、高亮顯示文件夾或文件並輸入新的名字。

VK_F3

61

Windows中可用於查找文件。

VK_F4

62

IE中可以按F4打開地址欄,組合ALTF4將關閉窗口。

VK_F5

63

刷新按鈕。

VK_F6

64

把光標移動到程序的其它可停留的位置。

VK_F7

65

WindowsF7沒有任何功能,在某些程序中可能會用上,可以打開程序的幫助文檔查看F7的功能。

VK_F8

66

在計算機啓動過程中可以使用F8來進入安全模式。

VK_F9

67

WindowsF9沒有任何功能,在某些程序中可能會用上,可以打開程序的幫助文檔查看F7的功能。

VK_F10

68

在很多程序中F10被用於激活菜單欄,可以使用F10來定位到第一個菜單選擇項,然後用方向鍵來移動並選擇菜單。組合SHIFT鍵與F10將打開快捷菜單,相當於右鍵彈出菜單。

VK_NUMLOCK

69

允許數字鍵盤中的數字按鍵可用,而不是光標控制鍵可用。按下一次NUMLOCK鍵,則激活NUMLOCK,並且鍵盤板上的燈會亮;再按一次NUMLOCK鍵則回到正常模式。

VK_SCROLL

70

在某些程序中能控制光標控制鍵。很多應用程序忽略這個設置。按下一次ScrollLock鍵將激活ScrollLock模式,並且鍵盤板上相應的燈會亮;再按一次ScrollLock鍵則回到正常模式。

VK_HOME

71

把光標移動到當前行的開頭。

VK_NUMPAD7

71

只有VK_NUMLOCK被按下時才生效。

VK_UP

72

向上鍵

VK_NUMPAD8

72

只有VK_NUMLOCK被按下時才生效。

VK_PRIOR

73

把光標移動到上一頁。

VK_NUMPAD9

73

只有VK_NUMLOCK被按下時才生效。

VK_SUBTRACT

74

減號鍵。

VK_LEFT

75

向左鍵。

VK_NUMPAD4

75

只有VK_NUMLOCK被按下時才生效。

VK_CLEAR

76

VK_NUMPAD5

76

只有VK_NUMLOCK被按下時才生效。

VK_RIGHT

77

向右鍵。

VK_NUMPAD6

77

只有VK_NUMLOCK被按下時才生效。

VK_ADD

78

加號鍵。

VK_END

79

移動光標到當前行的最後。

VK_NUMPAD1

79

只有VK_NUMLOCK被按下時才生效。

VK_DOWN

80

向下鍵。

VK_NUMPAD2

80

只有VK_NUMLOCK被按下時才生效。

VK_NEXT

81

移動光標到下一頁。

VK_NUMPAD3

81

只有VK_NUMLOCK被按下時才生效。

VK_INSERT

82

在一行文本中插入字符,每插入一個字符,剩下的文字向右移動一個光標位置。

VK_NUMPAD0

82

只有VK_NUMLOCK被按下時才生效。

VK_DELETE

83

在一行文本中刪除一個字符,每刪除一個字符,剩下的文字向左移動一個光標位置。

VK_SNAPSHOT

84

打印屏幕。

VK_BACKSLASH

86

斜槓號(/)。

VK_F11

87

IE中按下F11會把窗口打開到全屏狀態,並且把工具欄隱藏以便查看更多的信息,再次按下F11則回到正常狀態。

VK_F12

88

WindowsF12沒有任何功能,在某些程序中可能會用上,可以打開程序的幫助文檔查看F7的功能。

VK_NUMPAD5

89

VK_ZOOM

98

VK_HELP

99

VK_F13

100

IE中按下F11會把窗口打開到全屏狀態,並且把工具欄隱藏以便查看更多的信息,再次按下F11則回到正常狀態。

VK_F12

88

WindowsF12沒有任何功能,在某些程序中可能會用上,可以打開程序的幫助文檔查看F7的功能。

VK_NUMPAD5

89

VK_ZOOM

98

VK_HELP

99

VK_F13

100

VK_F14

101

VK_F15

102

VK_F16

103

VK_F17

104

相關文章
相關標籤/搜索