How to send a key command to a Web object
How to send a key command to a Web object
Some Web objects will perform actions when certain key commands, such as ALT+RETURN, are entered. These Web objects do not have a type method associated with them that can be used to replay these keys. How can the key combination be replayed?
Solution: Use the Windows Scripting SendKeys method
1. Create a WScript.Shell object.
2. Activate the browser in which you want to execute the keys.
3. Use the SendKeys method to type the key combination.
Example:
‘ This code executes the CTRL+F key combination (search) on a browser.
Set WshShell = CreateObject(“WScript.Shell”)
WshShell.AppActivate “Put the label of the browser” ‘ Activate the browser window
wait(3)
WshShell.SendKeys “^f” ‘ The caret (^) represents the CTRL key.
wait(2)
object.SendKeys(string)
object |
A WshShell object. |
string |
The string value indicating the keystroke(s) you want to send. |
The SendKeys method will send keystroke(s) to the active window. To send a single character (for example, x), use “x” as the string argument. To send multiple characters (for example, abc), use “abc” as the string argument. You can also send special characters such as SHIFT, CTRL, and ALT, which are represented by the plus sign (+), the caret (^), and the percent sign (%), respectively.
For more information on the SendKeys method and other special keys, such as the backspace or a function key, please refer to the MSDN SendKeys Method page.
====================================================================================================
‘ Name: KeyboardShortCut
‘
‘ Input:
‘ ByRef Key As Variant – The keyboard key to be typed.
‘ ByRef Pane As Variant – The window/pane to type in.
‘ Purpose: To type keyboard input onto the open window.
‘====================================================================================================
Function KeyboardShortCut (Key)
dim obj
Set obj = Window(“Window Name” ).WinObject(“Object”)
obj.type Key
Environment(“LastResult”) = “Success”
End Function
Solution2: Pressing Function keys should be recorded automatically
Directly recording on the application and pressing the Function Keys (F1, F2, etc.) should generate code for replay.
Example:
‘Here is an example recorded against Notepad:
Window(“Notepad”).Activate
Window(“Notepad”).WinEditor(“Edit”).Type micF5
If the above does not work, then you can use DeviceReplay to simulate pressing keyboard keys.
Example:
‘Here is an example that does the same thing above, but used DeviceReplay.
Set obj = CreateObject(“Mercury.DeviceReplay”)
Window(“Notepad”).Activate
obj.PressKey 63
Note:
The PressKey method uses the appropriate ASCII or IBM Scan Code value for the key. “63” is the IBM Scan Code value for F5.
1 Comment »
Leave a reply to Preeti Cancel reply
-
Archives
- July 2011 (1)
- March 2011 (1)
- November 2010 (1)
- November 2008 (4)
- October 2008 (2)
- September 2008 (2)
- June 2008 (1)
- March 2008 (4)
- February 2008 (44)
-
Categories
- Automated Test Script Creation Process
- Automating MS Word
- Automating QC
- Automation Object Model For QTP
- Backward compatability in QTP
- Browser Scripts
- COM and QTP
- compare 2 text files
- Descriptive programming
- Descriptive Programming and Child Objetcs
- Dictonary Object
- DotNetFactory
- Excel Automation
- Extra Topics
- Files and Folders
- General Standards to be followed in Test scripts
- New Features In QTP9.5
- QC-OTA
- QTP Additional Faqs
- QTP Faqs
- QTP Naming Convention
- QTP Tips and Faqs
- Running stored procedures from QTP
- Send keyboard keys
- Uncategorized
- Uninstall a software using QTP
- Uninstall a software using vbscript
- Update data in a database
- VBScript and IE Automation
- VBScript Faqs and Useful resources
- What’s New in Quick Test Pro 9.0
- Whats New in QTP 9.5
- XML Scripting
-
RSS
Entries RSS
Comments RSS
Thanks for the information. I am using the send key method but neither it is throwing any error nor pressing “Tab” key. Could you please help.
My requirement is to set a value to web element and then press tab key.
I have set the value and now using below code:
Set WshShell = CreateObject(“WScript.Shell”)
WshShell.AppActivate “page name”
WshShell.SendKeys “{TAB}”
Comment by Preeti | August 30, 2012 |