Monday 24 December 2012

Sendkeys Method - Keyboard Oprations in QTP


Hello Friends!

In this post we'll learn how to perform keyboard operation in QTP using vbscript.

What's the need of Sendkeys?


Hmmm... ok.. Refer the blow code.

Set oShell = CreateObject("WScript.Shell")
SystemUtil.Run "D:\ProgramFiles\MercuryInteractive\QuickTestProfessional\samples\flight\app\flight4a.exe"
Dialog("text:=Login").WinEdit("attached text:=Agent Name:").Click
oShell.SendKeys "john"
Dialog("text:=Login").WinEdit("attached text:=Password:").Click
oShell.SendKeys "wrong "
Dialog("text:=Login").WinButton("text:=OK").Click

Copy this code in QTP and run it.

Okie.. Now you can see, because we have entered incorrect password, following error dialog will be displayed.






Now we need to close error dialog and enter correct password in the login window.

If do this using DP code, we need to get the properties of error window and write one more line of code.

Dialog("text:=Flight Reservations").WinButton("text:=OK").Click



Instead of addind more objects in our code and writing extra lines for this, we can simply press escape key (ESC) from keyboard and our job is done!!

How can we do that using QTP?


All you need to do is, create a Wscript object and then you can use Sendkeys for that purpose.

Set oShell = CreateObject("WScript.Shell")
oShell.SendKeys "{ESC}"

Done!!


This thing is very useful when application have no automation interface. And it's quite fast as well. Depends on the need.



Most keyboard characters are represented by a single keystroke. Some keyboard characters are made up of combinations of keystrokes (CTRL+SHIFT+HOME, for example).

Please note:-

- To send a single keyboard character, send the character itself as the string argument. For example,
- To send the letter x, send the string argument "x".
- To send a space, send the string " ".
- You cannot send the PRINT SCREEN key {PRTSC} to an application.



Examples:

  • oShell.SendKeys "{ESC}"    - press escape key
  • oShell.SendKeys "john"    - type john
  • oShell.SendKeys "+^{ESC}"    - press Ctrl + Shift + Esc keys (open Windows Task Manager)
  • oShell.SendKeys "%{F}"    - press Alt + F key (open file menu)


For details of all the characters, please refer the following table.



In case of any queries, please post your comments.

No comments:

Post a Comment