Tuesday, July 26, 2011
QTP Secret Code Chart Revealed For DeviceReplay PressKey, KeyDown, KeyUp and PressNKeys
9:32 PM
1 comment
QTP Secret Code Chart Revealed For DeviceReplay PressKey, KeyDown, KeyUp and PressNKeys
Why this post?
Let’s face it. If you’ve been doing test automation for any length of time you’ve faced situations in which you need to perform mouse or keyboard actions using screen coordinates.
When the Set and Type Methods Suck:
Even though most objects have Set and Type methods, these methods -- for whatever reason -- may not work in all situations. If they don’t then your next step may be to try VBSCript’s sendkey method. But even sendkeys sometimes do not have a way to perform certain actions, such as pressing the left alt key. Hence the need for QTP’s undocumented Device Replay features.
No ASCII No Problem:
A few months back, I posted an article outlining three different ways to use keyboard input in QTP. I recently came across some more Device Replay Key codes for the PressKey, Keydown, Keyup and PressNKeys that I’d like to share with you. These methods are particularly helpful in certain situations in which you need to perform an action using a code that does not have an ASCII equivalent.
Chart of the keyboard key codes for the PressKey, Keydown, Keyup and PressNKeys:
The number keys:
The keypad number keys:
The Function keys:
The Letter keys:
The Enter,Space,Shift,Ctrl and Alt keys:
The Weird/Other keys:
Examples:
'Device Replay Example using the calculator with the multiple key
Set myReplay = Createobject("Mercury. DeviceReplay")
SystemUtil.Run "calc.exe"
Window("regexpwndtitle:= Calculator").Activate
myReplay.PressKey 73'9
wait 1
myReplay.PressKey 55' Keypad Multiply
wait 1
myReplay.PressKey 81'3
wait 1
myReplay.PressKey 156'Enter
The end result should be 27
'Device Replay Example using notepad with the shift key
Set myReplay = Createobject("Mercury. DeviceReplay")
SystemUtil.Run "notepad.exe"
wait 1
Window("nativeclass:=Notepad") .Activate
wait 1
myReplay.KeyDown 42''Hold shift key down
wait 1
myReplay.PressKey 36'T
wait 1
myReplay.PressKey 24'H
wait 1
myReplay.PressKey 18'I
wait 2
myReplay.KeyUp 42'Press shift key up
wait 1
myReplay.PressKey 57 'space
wait 1
myReplay.PressKey 36' t
wait 1
myReplay.PressKey 24 'h
wait 1
myReplay.PressKey 18 ie
The example should type out: THIthi
Why this post?
Let’s face it. If you’ve been doing test automation for any length of time you’ve faced situations in which you need to perform mouse or keyboard actions using screen coordinates.
When the Set and Type Methods Suck:
Even though most objects have Set and Type methods, these methods -- for whatever reason -- may not work in all situations. If they don’t then your next step may be to try VBSCript’s sendkey method. But even sendkeys sometimes do not have a way to perform certain actions, such as pressing the left alt key. Hence the need for QTP’s undocumented Device Replay features.
No ASCII No Problem:
A few months back, I posted an article outlining three different ways to use keyboard input in QTP. I recently came across some more Device Replay Key codes for the PressKey, Keydown, Keyup and PressNKeys that I’d like to share with you. These methods are particularly helpful in certain situations in which you need to perform an action using a code that does not have an ASCII equivalent.
Chart of the keyboard key codes for the PressKey, Keydown, Keyup and PressNKeys:
The number keys:
Keystroke | Code to use |
1 | 1 |
2 | 2 |
3 | 3 |
4 | 4 |
5 | 5 |
6 | 6 |
7 | 7 |
8 | 8 |
9 | 9 |
0 | 0 |
Keystroke | Code to use |
1 | 79 |
2 | 80 |
3 | 81 |
4 | 75 |
5 | 76 |
6 | 77 |
7 | 71 |
8 | 72 |
9 | 73 |
0 | 82 |
Keystroke | Code to use |
F1 | 59 |
F2 | 60 |
F3 | 61 |
F4 | 62 |
F5 | 63 |
F6 | 64 |
F7 | 65 |
F8 | 66 |
F9 | 67 |
F10 | 68 |
F11 | 87 |
F12 | 88 |
Keystroke | Code to use |
A | 30 |
B | 48 |
C | 46 |
D | 32 |
E | 18 |
F | 33 |
G | 34 |
H | 35 |
I | 23 |
J | 36 |
K | 37 |
L | 38 |
M | 50 |
N | 49 |
O | 24 |
P | 25 |
Q | 16 |
R | 19 |
S | 31 |
T | 20 |
U | 22 |
V | 47 |
W | 17 |
X | 45 |
Y | 21 |
Z | 44 |
Keystroke | Code to use |
ENTER | 28 |
SPACE | 57 |
LEFT SHIFT | 42 |
LEFT CTRL | 29 |
LEFT ALT | 56 |
RIGHT SHIFT | 54 |
RIGHT CTRL | 157 |
RIGHT ALT | 184 |
; | 39 |
' | 40 |
, | 51 |
. | 52 |
/ | 53 |
Keystroke | Code to use |
- | 12 |
[ | 26 |
] | 27 |
` | 41 |
= | 13 |
BACKSPACE | 14 |
CAPSLOCK | 58 |
DELETE | 211 |
DOWN | 208 |
END | 207 |
HOME | 199 |
INSERT | 210 |
LEFT | 203 |
NUMLOCK | 69 |
KEYPAD DECIMAL | 83 |
KEYPAD DIVIDE | 181 |
KEYPAD ENTER | 156 |
KEYPAD MINUS | 74 |
KEYPAD MULTIPLY | 55 |
PAUSE | 116 |
PAGE DOWN | 209 |
PAGE UP | 201 |
RIGHT | 205 |
SCROLL LOCK | 70 |
TAB | 15 |
UP | 20O |
'Device Replay Example using the calculator with the multiple key
Set myReplay = Createobject("Mercury.
SystemUtil.Run "calc.exe"
Window("regexpwndtitle:=
myReplay.PressKey 73'9
wait 1
myReplay.PressKey 55' Keypad Multiply
wait 1
myReplay.PressKey 81'3
wait 1
myReplay.PressKey 156'Enter
The end result should be 27
'Device Replay Example using notepad with the shift key
Set myReplay = Createobject("Mercury.
SystemUtil.Run "notepad.exe"
wait 1
Window("nativeclass:=Notepad")
wait 1
myReplay.KeyDown 42''Hold shift key down
wait 1
myReplay.PressKey 36'T
wait 1
myReplay.PressKey 24'H
wait 1
myReplay.PressKey 18'I
wait 2
myReplay.KeyUp 42'Press shift key up
wait 1
myReplay.PressKey 57 'space
wait 1
myReplay.PressKey 36' t
wait 1
myReplay.PressKey 24 'h
wait 1
myReplay.PressKey 18 ie
The example should type out: THIthi
QTP ASCII CHR() CODE CHART
9:28 PM
No comments
QTP ASCII CHR() CODE CHART
ASCII
Entering/Reading ASCII codes - when using QTP - is sometimes needed to achieve certain functionality.
ASCII stand for American Standard Code for Information Interchange and it is a character encoding scheme based on the English alphabet.
QTP has two built in functions (ASC and Chr) that are helpful to know when you need to work with ASCII info.
ASC() Function
ASC() is a QTP function that returns the code of the character passed to it. For example if you wanted to know what the ASCII/ANSI code for the pound symbol #:
Msgbox Asc(“#”)
The above will return the code 35
Chr() Function
If you wanted to enter or search for the pound key in a string you would use the QTP Chr() function. This function will return the character associated with the code passed to it. For example:
Msgbox Chr(35)
This will return the pound sign.
Common ASCII codes for QTP
Here are some of the most common ASCII codes I often use in QTP:
Chart for all the QTP Chr() codes:
ASCII
Entering/Reading ASCII codes - when using QTP - is sometimes needed to achieve certain functionality.
ASCII stand for American Standard Code for Information Interchange and it is a character encoding scheme based on the English alphabet.
QTP has two built in functions (ASC and Chr) that are helpful to know when you need to work with ASCII info.
ASC() Function
ASC() is a QTP function that returns the code of the character passed to it. For example if you wanted to know what the ASCII/ANSI code for the pound symbol #:
Msgbox Asc(“#”)
The above will return the code 35
Chr() Function
If you wanted to enter or search for the pound key in a string you would use the QTP Chr() function. This function will return the character associated with the code passed to it. For example:
Msgbox Chr(35)
This will return the pound sign.
Common ASCII codes for QTP
Here are some of the most common ASCII codes I often use in QTP:
QTP CODE | SYMBOL | DESCRIPTION |
Chr(34) | " | Double Quotes |
Chr(10) | Line Feed | |
Chr(13) | Carriage Return | |
Chr(32) | Space |
QTP CODE | SYMBOL | DESCRIPTION |
Chr(0) | NUL | Null char |
Chr(1) | SOH | Start of Heading |
Chr(2) | STX | Start of Text |
Chr(3) | ETX | End of Text |
Chr(4) | EOT | End of Transmission |
Chr(5) | ENQ | Enquiry |
Chr(6) | ACK | Acknowledgment |
Chr(7) | BEL | Bell |
Chr(8) | BS | Back Space |
Chr(9) | HT | Horizontal Tab |
Chr(10) | LF | Line Feed |
Chr(11) | VT | Vertical Tab |
Chr(12) | FF | Form Feed |
Chr(13) | CR | Carriage Return |
Chr(14) | SO | Shift Out / X-On |
Chr(15) | SI | Shift In / X-Off |
Chr(16) | DLE | Data Line Escape |
Chr(17) | DC1 | Device Control 1 (oft. XON) |
Chr(18) | DC2 | Device Control 2 |
Chr(19) | DC3 | Device Control 3 (oft. XOFF) |
Chr(20) | DC4 | Device Control 4 |
Chr(21) | NAK | Negative Acknowledgement |
Chr(22) | SYN | Synchronous Idle |
Chr(23) | ETB | End of Transmit Block |
Chr(24) | CAN | Cancel |
Chr(25) | EM | End of Medium |
Chr(26) | SUB | Substitute |
Chr(27) | ESC | Escape |
Chr(28) | FS | File Separator |
Chr(29) | GS | Group Separator |
Chr(30) | RS | Record Separator |
Chr(31) | US | Unit Separator |
Chr(32) | Space | |
Chr(33) | ! | Exclamation mark |
Chr(34) | " | Double quotes (or speech marks) |
Chr(35) | # | Number |
Chr(36) | $ | Dollar |
Chr(37) | % | Procenttecken |
Chr(38) | & | Ampersand |
Chr(39) | ' | Single quote |
Chr(40) | ( | Open parenthesis (or open bracket) |
Chr(41) | ) | Close parenthesis (or close bracket) |
Chr(42) | * | Asterisk |
Chr(43) | + | Plus |
Chr(44) | , | Comma |
Chr(45) | - | Hyphen |
Chr(46) | . | Period, dot or full stop |
Chr(47) | / | Slash or divide |
Chr(48) | 0 | Zero |
Chr(49) | 1 | One |
Chr(50) | 2 | Two |
Chr(51) | 3 | Three |
Chr(52) | 4 | Four |
Chr(53) | 5 | Five |
Chr(54) | 6 | Six |
Chr(55) | 7 | Seven |
Chr(56) | 8 | Eight |
Chr(57) | 9 | Nine |
Chr(58) | : | Colon |
Chr(59) | ; | Semicolon |
Chr(60) | < | Less than (or open angled bracket) |
Chr(61) | = | Equals |
Chr(62) | > | Greater than (or close angled bracket) |
Chr(63) | ? | Question mark |
Chr(64) | @ | At symbol |
Chr(65) | A | Uppercase A |
Chr(66) | B | Uppercase B |
Chr(67) | C | Uppercase C |
Chr(68) | D | Uppercase D |
Chr(69) | E | Uppercase E |
Chr(70) | F | Uppercase F |
Chr(71) | G | Uppercase G |
Chr(72) | H | Uppercase H |
Chr(73) | I | Uppercase I |
Chr(74) | J | Uppercase J |
Chr(75) | K | Uppercase K |
Chr(76) | L | Uppercase L |
Chr(77) | M | Uppercase M |
Chr(78) | N | Uppercase N |
Chr(79) | O | Uppercase O |
Chr(80) | P | Uppercase P |
Chr(81) | Q | Uppercase Q |
Chr(82) | R | Uppercase R |
Chr(83) | S | Uppercase S |
Chr(84) | T | Uppercase T |
Chr(85) | U | Uppercase U |
Chr(86) | V | Uppercase V |
Chr(87) | W | Uppercase W |
Chr(88) | X | Uppercase X |
Chr(89) | Y | Uppercase Y |
Chr(90) | Z | Uppercase Z |
Chr(91) | [ | Opening bracket |
Chr(92) | \ | Backslash |
Chr(93) | ] | Closing bracket |
Chr(94) | ^ | Caret - circumflex |
Chr(95) | _ | Underscore |
Chr(96) | ` | Grave accent |
Chr(97) | a | Lowercase a |
Chr(98) | b | Lowercase b |
Chr(99) | c | Lowercase c |
Chr(100) | d | Lowercase d |
Chr(101) | e | Lowercase e |
Chr(102) | f | Lowercase f |
Chr(103) | g | Lowercase g |
Chr(104) | h | Lowercase h |
Chr(105) | i | Lowercase i |
Chr(106) | j | Lowercase j |
Chr(107) | k | Lowercase k |
Chr(108) | l | Lowercase l |
Chr(109) | m | Lowercase m |
Chr(110) | n | Lowercase n |
Chr(111) | o | Lowercase o |
Chr(112) | p | Lowercase p |
Chr(113) | q | Lowercase q |
Chr(114) | r | Lowercase r |
Chr(115) | s | Lowercase s |
Chr(116) | t | Lowercase t |
Chr(117) | u | Lowercase u |
Chr(118) | v | Lowercase v |
Chr(119) | w | Lowercase w |
Chr(120) | x | Lowercase x |
Chr(121) | y | Lowercase y |
Chr(122) | z | Lowercase z |
Chr(123) | { | Opening brace |
Chr(124) | | | Vertical bar |
Chr(125) | } | Closing brace |
Chr(126) | ~ | Equivalency sign - tilde |
Chr(127) | Delete |
QTP Scripting Examples
9:23 PM
No comments
QTP Scripting Examples
'Count Lines in a Text File
Dim objFso, objTextstream, LineCount
Set objFso=CreateObject("Scripting.FileSystemObject")
Set objTextstream=objFso.OpenTextFile ("C:\Documents and Settings\Administrator\Desktop\qtpthiru.txt")
While objTextstream.AtEndOfStream=False
LineCount=LineCount+1
objTextstream.ReadLine
Wend
Msgbox LineCount
objTextstream.Close
Set objTextstream=Nothing
Set objFso=Nothing
'Read a Text File from 4th line and perform Data Driven Testing for Login operation
Dim objFso, objTextstream, LineCount
Set objFso=CreateObject("Scripting.FileSystemObject")
Set objTextstream=objFso.OpenTextFile ("C:\Documents and Settings\Administrator\Desktop\qtpthiru.txt")
While objTextstream.AtEndOfStream=False
LineCount=LineCount+1
objTextstream.ReadLine
Wend
Msgbox LineCount
objTextstream.Close
Set objTextstream=Nothing
Set objFso=Nothing
'Read a Text File from 4th line and perform Data Driven Testing for Login operation
Dim objFso, objTextstream, LineCount
Set objFso=CreateObject("Scripting.FileSystemObject")
Set objTextstream=objFso.OpenTextFile ("C:\Documents andSettings\Administrator\Desktop\qtpthiru.txt")
While objTextstream.AtEndOfStream=False
LineCount=LineCount+1
myLine=objTextstream.ReadLine
If LineCount >= 4 Then
myField=Split(myLine,",")
SystemUtil.Run "C:\Program Files\HP\QuickTestProfessional\samples\flight\app\flight4a.exe","","C:\Program Files\HP\QuickTestProfessional\samples\flight\app\","open"
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set myField(0)
Dialog("Login").WinEdit("Password:").Set myField(1)
Wait 2
Dialog("Login").WinButton("OK").Click
Window("Flight Reservation").Close
End If
Wend
objTextstream.Close
Set objTextstream=Nothing
Set objFso=Nothing
'Read a Text File from 4th line to 6th Line and perform Data Driven Testing for Login operation
Dim objFso, objTextstream, LineCount
Set objFso=CreateObject("Scripting.FileSystemObject")
Set objTextstream=objFso.OpenTextFile ("C:\Documents and Settings\Administrator\Desktop\qtpthiru.txt")
While objTextstream.AtEndOfStream=False
LineCount=LineCount+1
myLine=objTextstream.ReadLine
If LineCount >= 4 and LineCount<=6 Then
myField=Split(myLine,",")
SystemUtil.Run "C:\Program Files\HP\QuickTestProfessional\samples\flight\app\flight4a.exe","","C:\Program Files\HP\QuickTestProfessional\samples\flight\app\","open"
Dialog("Login").Activate
Dialog("Login").WinEdit("Agent Name:").Set myField(0)
Dialog("Login").WinEdit("Password:").Set myField(1)
Wait 2
Dialog("Login").WinButton("OK").Click
Window("Flight Reservation").Close
End If
Wend
objTextstream.Close
Set objTextstream=Nothing
Set objFso=Nothing
'Open 1 to 10 Orders in Flight Reservation Application and export all fields to excel file (3rd sheet)
Dim objExcel, objWorkbook, objWorksheet
Set objExcel=CreateObject("Excel.Application")
Set objWorkbook=objExcel.Workbooks.Open("C:\Documents andSettings\Administrator\Desktop\qtpthiru.xls")
Set objWorksheet=objWorkbook.Worksheets(3)
objWorksheet.Cells(1,1)="OrderNo"
objWorksheet.Cells(1,2)="Date of Flight"
objWorksheet.Cells(1,3)="Fly From"
objWorksheet.Cells(1,4)="Fly To"
objWorksheet.Cells(1,5)="C_Name"
objWorksheet.Cells(1,6)="Tickets"
For i= 1 to 10 Step 1
Window("Flight Reservation").Activate
Window("Flight Reservation").WinButton("Button").Click
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON"
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set i
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
objWorksheet.Cells(i+1,1)=i
Wait 1
objWorksheet.Cells(i+1,2)= Window("Flight Reservation").WinObject("Date of Flight:").GetROProperty("text")
Wait 1
objWorksheet.Cells(i+1,3) = Window("Flight Reservation").WinComboBox("Fly From:").GetROProperty("text")
Wait 1
objWorksheet.Cells(i+1,4)= Window("Flight Reservation").WinComboBox("Fly To:").GetROProperty("text")
Wait 1
objWorksheet.Cells(i+1,5) = Window("Flight Reservation").WinEdit("Name:").GetROProperty("text")
Wait 1
objWorksheet.Cells(i+1,6) = Window("Flight Reservation").WinEdit("Tickets:").GetROProperty("text")
Next
objWorkbook.Save
objExcel.Quit
Set objWorksheet=Nothing
Set objWorkbook=Nothing
Set objExcel=Nothing
'Open 1 to 10 Orders in Flight Reservation Application and export all fields to excel file (3rd sheet)
Dim objExcel, objWorkbook, objWorksheet
Set objExcel=CreateObject("Excel.Application")
Set objWorkbook=objExcel.Workbooks.Open("C:\Documents andSettings\Administrator\Desktop\qtpthiru.xls")
Set objWorksheet=objWorkbook.Worksheets(3)
objWorksheet.Cells(1,1)="OrderNo"
objWorksheet.Cells(1,2)="Date of Flight"
objWorksheet.Cells(1,3)="Fly From"
objWorksheet.Cells(1,4)="Fly To"
objWorksheet.Cells(1,5)="C_Name"
objWorksheet.Cells(1,6)="Tickets"
For i= 1 to 10 Step 1
Window("Flight Reservation").Activate
Window("Flight Reservation").WinButton("Button").Click
Window("Flight Reservation").Dialog("Open Order").WinCheckBox("Order No.").Set "ON"
Window("Flight Reservation").Dialog("Open Order").WinEdit("Edit").Set i
Window("Flight Reservation").Dialog("Open Order").WinButton("OK").Click
objWorksheet.Cells(i+1,1)=i
Wait 1
objWorksheet.Cells(i+1,2)= Window("Flight Reservation").WinObject("Date of Flight:").GetROProperty("text")
Wait 1
objWorksheet.Cells(i+1,3) = Window("Flight Reservation").WinComboBox("Fly From:").GetROProperty("text")
Wait 1
objWorksheet.Cells(i+1,4)= Window("Flight Reservation").WinComboBox("Fly To:").GetROProperty("text")
Wait 1
objWorksheet.Cells(i+1,5) = Window("Flight Reservation").WinEdit("Name:").GetROProperty("text")
Wait 1
objWorksheet.Cells(i+1,6) = Window("Flight Reservation").WinEdit("Tickets:").GetROProperty("text")
Next
objWorkbook.Save
objExcel.Quit
Set objWorksheet=Nothing
Set objWorkbook=Nothing
Set objExcel=Nothing
Loading...