• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

揭秘QTP的DeviceReplay对象

发布: 2008-6-05 10:07 | 作者: 陈能技 | 来源: CSDN | 查看: 346次 | 进入软件测试论坛讨论

领测软件测试网

PressKeyMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">方法

描述

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

语法

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" )

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

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/

42/4<1234>

关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网