- 論壇徽章:
- 0
|
該版本是windows 下實(shí)現(xiàn),屬于轉(zhuǎn)載。
我會有自己的Linux 下Visual keypad 的實(shí)現(xiàn)分享,敬請關(guān)注。
揭秘QTP的DeviceReplay對象
原文:The Undocumented DeviceReplay(http://www.advancedqtp.com/2008/03/undocumented-devicereplay/www.advancedqtp.com)
不知道為什么HP的幫助文檔中沒有提供關(guān)于DeviceReplay的強(qiáng)大功能的信息描述。你可以在Java插件中卻可以找到DeviceReplay的屬性,但是對于那些不使用Java插件的人可能會覺得這個(gè)對象僅在Java程序的測試中可用。
為什么要用DeviceReplay?
有些時(shí)候我們需要針對界面做一些指定的動作,例如右鍵單擊一個(gè)對象,使用功能鍵(Fx)來激活某些熱鍵的功能,這時(shí)候就可以使用DeviceReplay對象,或者在Object.Set和Object.Type方法不生效時(shí)使用DeviceReplay。
并且DeviceReplay在輸入特殊符號以及不同語言的文字時(shí)會很有用,因?yàn)椴恍枰惭b指定的字體或改變鍵盤布局,這對于測試多語言環(huán)境的應(yīng)用程序會非常有用。
在鼠標(biāo)操作方面,我發(fā)現(xiàn)DragDrop方法非常有用,可以使用它來執(zhí)行拖拽的操作,把一個(gè)Item從一個(gè)Frame拖動到另外一個(gè)Frame,或者在應(yīng)用程序之間拖動。
Mercury.DeviceReplay對象
Mercury.DeviceReplay對象用于模擬鼠標(biāo)單擊和移動,還有鍵盤輸入等操作。要使用DeviceReplay,你必須確保被測試的應(yīng)用程序(AUT)是處于激活狀態(tài)的窗口。如果你想對某個(gè)對象執(zhí)行一項(xiàng)操作,則該對象必須擁有焦點(diǎn)(focus)。對于Windows應(yīng)用程序,可以使用Activate方法:
Window( "W" ).Activate micLeftBtn
如果想把焦點(diǎn)設(shè)置到某個(gè)指定的對象上,通常使用Click方法可以完成。
對于Web環(huán)境的應(yīng)用程序,Activate方法不被支持,因此可以使用下面的技巧來完成:
hwnd = Browser( "B" ).GetROProperty( "hwnd" )
Window( "hwnd:=" & hwnd ).Activate micLeftBtn
通?梢允褂肍ireEvent “onfocusin”或object.focus,例如WebEdit(“WE”).Object.focus或WebEdit(“WE”)。FireEvent “onfocusin”。
在調(diào)用DeviceReplay對象的方法之前,你需要首先創(chuàng)建DeviceReplay對象:
Set deviceReplay = CreateObject( "Mercury.DeviceReplay" )
Microsoft.VisualBasic.Devices.Keyboard類
為什么我要在介紹DeviceReplay對象之前介紹這個(gè).NET的類呢?DeviceReplay是一個(gè)強(qiáng)大的未被文檔化的對象,但是有一定的局限性。其中一個(gè)局限就是不能判斷一個(gè)Control鍵是否已經(jīng)被按下。在輸入一個(gè)大寫字母之前,我們需要知道CAPS-LOCK鍵是否已經(jīng)按下。在使用數(shù)字鍵盤之前我們需要檢查NUM-LOCK鍵是否已經(jīng)被按下。否則我們在切換鍵盤輸入狀態(tài)時(shí)可能得到的并不是我們想要的狀態(tài)。
Devices.Keyboard類提供了屬性,可用于獲取當(dāng)前的鍵盤狀態(tài),例如當(dāng)前什么鍵被按下了,并且提供一個(gè)方法用于向激活的窗口發(fā)送鍵盤敲擊事件。
幾個(gè)有用的屬性包括:
AltKeyDown - 判斷ALT鍵是否處于按下狀態(tài)。
CapsLock - 判斷CAPS LOCK鍵是否處于打開狀態(tài)。
CtrlKeyDown - 判斷CTRL 鍵是否處于按下狀態(tài)。
NumLock - 判斷NUM LOCK鍵是否處于打開狀態(tài)。
ScrollLock - 判斷SCROLL LOCK鍵是否處于打開狀態(tài)。
ShiftKeyDown - 判斷SHIFT鍵是否處于按下狀態(tài)。
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時(shí)數(shù)據(jù)類型必須被轉(zhuǎn)換
System.Windows.Forms.Control 類
DeviceReplay的另外一個(gè)局限是不能獲取當(dāng)前鼠標(biāo)(光標(biāo))在屏幕的位置。而System.Windows.Forms.Control這個(gè)類定義了那些擁有視覺表現(xiàn)的控件的基類。
通過MousePosition屬性可以獲取當(dāng)前鼠標(biāo)光標(biāo)在屏幕坐標(biāo)的位置。訪問MousePosition屬性時(shí),可以返回代表鼠標(biāo)光標(biāo)位置的Point數(shù)據(jù)。
我的鼠標(biāo)在哪?
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方法
描述
向激活的窗口發(fā)送一個(gè)或多個(gè)鍵盤按鍵,就像敲擊鍵盤一樣。
語法
object.SendString( str )
參數(shù)
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方法
描述
模擬一個(gè)按鍵的按下并保持(相當(dāng)于Win32的KEY_DOWN事件)。
語法
object.KeyDown( key )
參數(shù)
object : Mercury.DeviceReplay對象。
key : 按鍵的數(shù)值碼。可查閱后面的“Key Codes 參考”。
返回值
無。
例子
下面的例子會激活記事本(notepad)程序并使用大寫和小寫的方式輸入字符串。注意在發(fā)送第一個(gè)字符串時(shí),SHIFT鍵保持被按下的狀態(tài):
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后應(yīng)該有相應(yīng)的KeyUp方法的調(diào)用。
KeyDown方法就像人工按下一個(gè)按鍵并保持按下的狀態(tài)。
KeyUp方法
描述
模擬通過鍵盤釋放某個(gè)按下的按鍵。
語法
object.KeyUp( key )
參數(shù)
object : Mercury.DeviceReplay對象。
key : 按鍵的數(shù)值碼?刹殚喓竺娴摹癒ey Codes 參考”。
返回值
無。
例子
下面的例子會激活并并使用熱鍵CTRL+O來打開記事本(notepad)的菜單,然后用ESC鍵關(guān)閉對話框。
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方法應(yīng)該與KeyDown方法配對使用。
多個(gè)KeyUp不會對應(yīng)用程序造成影響。
如果需要組合熱鍵,僅需要像人工執(zhí)行的方式一樣即可。
PressKey方法
描述
模擬通過鍵盤按下一個(gè)按鍵并立即釋放。
語法
object.PressKey( key )
參數(shù)
object : Mercury.DeviceReplay對象。
key : 按鍵的數(shù)值碼?刹殚喓竺娴摹癒ey Codes 參考”。
返回值
無。
例子
下面的例子會激活記事本并使用熱鍵CTRL+O來模擬選擇文件打開菜單,然后用ESCAPE按鍵關(guān)閉對話框。
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方法
描述
模擬通過鍵盤多次按下一個(gè)按鍵并立即釋放。
語法
object.PressNKey( key, N )
參數(shù)
object : Mercury.DeviceReplay對象。
key : 按鍵的數(shù)值碼?刹殚喓竺娴摹癒ey Codes 參考”。
N:重復(fù)的次數(shù)。
返回值
無。
例子
例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 )
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" )
Call PrintRow( "South Dakota", "SD", "Pierre" )
Call PrintRow( "Tennessee", "TN", "Nashville" )
Call PrintRow( "Texas", "TX", "Austin" )
Call PrintRow( "Utah", "UT", "Salt Lake City" )
Call PrintRow( "Vermont", "VT", "Montpelier" )
Call PrintRow( "Virginia", "VA", "Richmond" )
Call PrintRow( "Washington", "WA", "Olympia" )
Call PrintRow( "West Virginia", "WV", "Charleston" )
Call PrintRow( "Wisconsin", "WI", "Madison" )
Call PrintRow( "Wyoming", "WY", "Cheyenne" )
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方法
描述
用于執(zhí)行從一點(diǎn)拖動到另外一點(diǎn)的操作。
語法
object.DragAndDrop( dragX, dragY, dropX, dropY, Button )
參數(shù)
object : Mercury.DeviceReplay對象。
dragX :起點(diǎn)坐標(biāo)的X軸的值。
dragY :起點(diǎn)坐標(biāo)的Y軸的值。
dropX :終點(diǎn)坐標(biāo)的X軸的值。
dropY :終點(diǎn)坐標(biāo)的Y軸的值。
Button :可能的值包括
LEFT_MOUSE_BUTTON = 0
MIDDLE_MOUSE_BUTTON = 1
RIGHT_MOUSE_BUTTON = 2
返回值
無。
提示
可以組合使用MouseDown、MouseMove和MouseUp方法。
MouseClick方法
描述
在指定的屏幕位置執(zhí)行鼠標(biāo)左鍵或右鍵的單擊操作。
語法
object.MouseClick( x, y, Button )
參數(shù)
object : Mercury.DeviceReplay對象。
x :屏幕坐標(biāo)X軸的值。
y :屏幕坐標(biāo)Y軸的值。
Button :可能的值包括
LEFT_MOUSE_BUTTON = 0
MIDDLE_MOUSE_BUTTON = 1
RIGHT_MOUSE_BUTTON = 2
返回值
無。
例子
下面的例子在執(zhí)行之前需要做一定的準(zhǔn)備工作。例子的目的是在www.advancedqtp.com網(wǎng)站上執(zhí)行DragAndDrop操作,如果在錄制時(shí)執(zhí)行拖拽操作,則不會被錄制下來。因此這個(gè)例子是支持某些操作的例子。這個(gè)例子在IE環(huán)境下測試通過。
打開IE瀏覽器并導(dǎo)航到
www.advancedqtp.com
。這個(gè)例子會交換dbxhandle項(xiàng),這些對象可以被拖拽以便滿足個(gè)性化顯示的要求。
![]()
打開QTP(加載Web插件),新建一個(gè)測試,打開對象庫(object repository)并添加瀏覽器中的頁面對象到本地對象庫(local object repository)中。
![]()
重命名對象…
![]()
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方法
描述
在指定的屏幕位置中執(zhí)行鼠標(biāo)左鍵或右鍵的雙擊事件。
語法
object.MouseDblClick( x, y, Button )
參數(shù)
object : Mercury.DeviceReplay對象。
x :屏幕坐標(biāo)X軸的值。
y :屏幕坐標(biāo)Y軸的值。
Button :可能的值包括
LEFT_MOUSE_BUTTON = 0
MIDDLE_MOUSE_BUTTON = 1
RIGHT_MOUSE_BUTTON = 2
返回值
無
MouseDown方法
描述
在屏幕指定位置按下鼠標(biāo)左鍵或右鍵,并保持按下狀態(tài)。
語法
object.MouseDown( x, y, Button )
參數(shù)
object : Mercury.DeviceReplay對象。
x :屏幕坐標(biāo)X軸的值。
y :屏幕坐標(biāo)Y軸的值。
Button :可能的值包括
LEFT_MOUSE_BUTTON = 0
MIDDLE_MOUSE_BUTTON = 1
RIGHT_MOUSE_BUTTON = 2
返回值
無
提示
應(yīng)該在MouseDown后使用對應(yīng)的MouseUp方法。
MouseUp方法
描述
用于釋放之前執(zhí)行的MouseDown方法所按下的鼠標(biāo)按鍵。
語法
object.MouseDown( x, y, Button )
參數(shù)
object : Mercury.DeviceReplay對象。
x :屏幕坐標(biāo)X軸的值。
y :屏幕坐標(biāo)Y軸的值。
Button :可能的值包括
LEFT_MOUSE_BUTTON = 0
MIDDLE_MOUSE_BUTTON = 1
RIGHT_MOUSE_BUTTON = 2
返回值
無
提示
應(yīng)該讓MouseUp和MouseDowun方法配對使用。
MouseMove方法
描述
用于釋放之前執(zhí)行的MouseDown方法所按下的鼠標(biāo)按鍵。(譯者注:這里懷疑是作者筆誤,應(yīng)該是:用于模擬鼠標(biāo)移動。)
語法
object.MouseDown( x, y ) (譯者注:這里懷疑是作者筆誤,應(yīng)該是:object.MouseMove( x, y )。)
參數(shù)
object : Mercury.DeviceReplay對象。
x :屏幕坐標(biāo)X軸的值。
y :屏幕坐標(biāo)Y軸的值。
返回值
無
提示
調(diào)試腳本查看在運(yùn)行時(shí)獲取到的坐標(biāo)位置。
在執(zhí)行鍵盤操作之前移動鼠標(biāo)到指定的位置并設(shè)置焦點(diǎn)。
本文來自ChinaUnix博客,如果查看原文請點(diǎn):http://blog.chinaunix.net/u2/75558/showart_1815131.html |
|