Monday, June 4, 2012

Different Types of Keyboard Inputs : Type, SendKeys & Device Replay


1. 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 CTRL and the Shift Keys, then presses the “L” key and releases the CTRL and Shift keys:
SwfObject("swfname:=Blank").Type micCtrlDwn + micShiftDwn + "L" + micShiftUp + micCtrlUp

*Important to remember: 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.


2. VBScript SendKeys Method

There are instances in which QTP’s 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 mySendKey
set mySendKey = CreateObject("WScript.shell")
mySendKeys.SendKeys(“{TAB}”)

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

To send an enter keystroke:
mySendKeys.SendKeys(“~”)
*A few important tips: Unlike the QTP 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:
mySendKey.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:
mySendKey.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 QuickTest method, but can be used as a last resort. To employ this method, you’ll need to create a Device Replay object.
To tab off an object:
Dim myDeviceReplay

Set myDeviceReplay = CreateObject(“Mercury.DeviceReplay”)
myDeviceReplay.PressKey 15
*Remember that Device Replay uses ASCII characters
To send text you would use:

myDeviceReplay.SendString “This is my string”
To send an enter keystroke:
myDeviceReplay.PressKey 28 ‘ASCII code for enter
From The HP knowledge base:
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.

How to click a button in 7 ways



'1st method
Window("Flight Reservation").WinButton("Update Order").Click    'Common Method


'2nd method
Set wndObject=Window("Flight Reservation")    ' Assigning window object to an object variable
wndObject.WinButton("Update Order").Click        ' Following normal syntax ( click on a button)


' OR


Set btnObject=Window("Flight Reservation").WinButton("Update Order")    ' Assigning Button object to an object variable
btnObject.Click    ' Clicking on button using button object variable


'3rd method
With Window("Flight Reservation")        ' Using With statement
            .WinButton("Update Order").click
End with


'4th method
Window("text:=Flight Reservation").WinButton("text:=&Update Order").Click    ' Descriptive programming


'5th method
Set oDes=Description.Create            ' creating a description object
oDes("nativeclass").value="Button"    ' assigning description to the description object
oDes("text").value="&Update Order"
Window("text:=Flight Reservation").winbutton(oDes).click    ' clicking on button using the created description object


'6th method
Set oDes=Description.Create        '   creating a description object
set btnObjList=Window("text:=Flight Reservation").ChildObjects(oDes)    ' Flitering the objects
For objIndex=0 to btnObjList.count-1
        propVal=btnObjList(objIndex).getroproperty("text")    ' Get property value from object
        If propVal="&Update Order" Then        ' Compare property value
                btnObjList(objIndex).click        ' Click on identified object
                Exit for        ' Exit For loop after clicking on the button
        End If
Next


'7th method
Public const wndFlight="text:=Flight Reservation"    ' Assigning window object description to a constant
Public const btnUpdate="text:=&Update Order"      ' Assigning Button object description to a constant
Window(wndFlight).winbutton(btnUpdate).click        ' Click on a button using description constants

Different Types of Keyboard Inputs : Type, SendKeys & Device Replay

1. Most objects support the TYPE method.  Type will enter the specified string into an object or perform a certain keyboard combinat...