Tricentis Tosca 16.0 Released on Feb-2023 ----- UFT has been upgraded from UFT 15.0.1 to UFT One 15.0.2, Beginning at November 2020.

Wednesday, 20 January 2016

UFT/QTP Input Methods

There are three ways to use keyboard input in Unified Functional Testing: 
1.Type
2.SendKeys 
3.Device Replay.
Why, we might ask, use the Type or Sendkeys method at all?  While we wouldn’t want to automate a whole script using SendKeys only, sometimes you may, on occasion, need to automate a section of an application where there is no object recognition.For example, some unsupported grids or tree views in UFT can be navigated using a combination of the HOME, END and arrow keys. 
1. UFT TYPE METHOD
Most objects support the TYPE method.  Type will enter the specified string into an object or perform a certain keyboard combination against an application.  For example:
To tab off an object you would use the following syntax:
SwfObject(“swfname:=Blank”).Type micTab

To enter text:
SwfObject(“swfname:=Blank”).Type “This is my string”
To send an enter keystroke:
SwfObject(“swfname:=Blank”).Type “ “

You can also send a combination of keystrokes at one time. The following holds down the CTRLand the Shift Keys, then presses the “L” key and releases the CTRL and Shift keys:
SwfObject("swfname:=Blank").Type micCtrlDwn + micShiftDwn + "L" + micShiftUp + micCtrlUp
NOTE: If you send a down keystroke, be sure to follow it with its corresponding up stroke, as seen in the above example.  Problems can arise by sending a micCtrlDwn and forgetting to release it using micCtrlUp. So, rule of thumb — if you are pressing a key, make sure to also release it.
Also Occasionally you may have issues with event not being  triggered when using one of these methods.

2. VBScript SendKeys Method ( Also check out: UFT, VBscript SendKeys )
There are instances in which UFT Type method does not trigger certain events, or is unable to mimic certain keystrokes.  In these cases, VBScript SendKeys method can be used. To use the SendKeys you need to first set the WshShell object:
Dim mySendKeys
set mySendKeys = CreateObject("WScript.shell") mySendKeys.SendKeys(“{TAB}”)
Set myDeviceReplay = CreateObject("Mercury.DeviceReplay")
myDeviceReplay.PressKey 15
To send text you would use:
The functions that can be used with the Device Replay object are (all coordinates are relative to the top left corner of the screen):
Function
Description
MouseMove x, y      
Move the mouse to the screen coordinate (x,y).
MouseClick x, y,  button                         
Move the mouse to the screen coordinate (x,y) and click the button
(0=left; 1=middle; 2=right).
MouseDblClick x, y, button
Move the mouse to the screen coordinate (x,y) and double-click the button
(0=left; 1=middle; 2=right).
DragAndDrop x, y, dropx, dropy, button
Drag the mouse from screen coordinate (x,y) to (dropx,dropy) with the button
(0=left; 1=middle; 2=right) pressed.
PressKey key
Press a key using the ASCII code of the key.
For example, Chr(13), vbCR and vbTab.
MouseDown x, y, button
Press the mouse button on screen coordinate (x,y).
MouseUp x, y, button
Release the mouse button on screen coordinate (x,y).
KeyDown key
Press a key using the ASCII code of the key.
For example, Chr(13), vbCR and vbTab.
KeyUp key
Release a key using the ASCII code of the key.
For example, Chr(13), vbCR and vbTab.
SendString string
Type a string.

To send the text you would use:
mySendKeys.SendKeys(“This is my string”)
To send an enter keystroke:
mySendKeys.SendKeys(“~”)

Note: Unlike the UFT TYPE method, you will need to use curly braces for arguments like a TAB or Up arrow key strokes. Also — SendKeys has the following special characters:  Alt(%), Ctrl(^), Shift(+) and Enter(~) keys.

So, to send a CTRL and press the A key you would send:
mySendKeys.SendKeys("^A")

If you need to perform the same keystroke multiple times, you can create a compound string argument. This will allow you to perform a specific keystroke and repeat it any number of times. To select, say, the 10th row in a grid control you might use:

mySendKeys.SendKeys(“{DOWN 10}”)
This will send the down key ten times. (For a more detailed explanation of SendKeys check out VBScript Programmer’s  reference)

Important – You’ll need to make sure that the application or object you wish to receive the keystroke has focus before sending the keystroke.

*Common issues with this method:
(Sometimes multiple keystrokes will not work. If this is the case, try executing each one in a separate line.)

3. Device Replay
This is an undocumented and unsupported UFT method, but can be used as a last resort. To employ this method, you’ll need to create a Device Replay object.(Check out the Device Replay Chart of Codes)
To tab off an object:
Dim myDeviceReplay
*Remember that Device Replay uses ASCII characters
myDeviceReplay.SendString “This is my string”

To send an enter keystroke:
myDeviceReplay.PressKey 28 ‘ASCII code for enter 
================================================
In following table you will find all keys and their argument that you may need.

Name of Key
Argument
BACKSPACE
{BACKSPACE}, {BS}, or {BKSP}
BREAK
{BREAK}
CAPS LOCK {CAPSLOCK}
{CAPSLOCK}
DEL or DELETE
{DEL}
DOWN ARROW
{DOWN}
END
{END}
ENTER
{ENTER} or ~
ESC
{ESC}
HELP
{HELP}
HOME
{HOME}
INS or INSERT
{INSERT} or {INS}
LEFT ARROW
{LEFT}
NUM LOCK
{NUMLOCK}
PAGE DOWN
{PGDN}
PAGE UP
{PGUP}
PRINT SCREEN
{PRTSC}
RIGHT ARROW
{RIGHT}
SCROLL LOCK
{SCROLLLOCK}
TAB
{TAB}
UP ARROW
{UP}
Y 10 times
{Y 10}
Space
” “
plus sign      
 “+”
caret            
“^”
percent sign
“%”
tilde      
“~”