How do you simulate a keyboard enter event in UFT

13,318

Solution 1

If you use device replay mode (as described in this answer) and send a vbCRLF your application will be able to see the enter key.

Setting.WebPackage("ReplayType") = 2 ''# Changes to device mode
Browser("Enter").Page("Enter").WebEdit("WebEdit").Set  "a" & vbCRLF

This works (on IE) for the following sample page:

<!DOCTYPE html>
<html>
<head>
<title>Enter</title>
<script>
function okd() {
if (event.keyCode == 13)
    alert("Got enter");
}
</script>
</head>
<body>
<textarea onkeydown="okd()"></textarea>
</body>

Solution 2

These are the some methods i have tried for simulate keyboard events and worked for me..

Set oShell = CreateObject("WScript.Shell")

oShell.SendKeys "{TAB}"           ' Click Tab Key
oShell.SendKeys "{ENTER}"         ' Click Enter\Return

Also i have used

Type micAltDwn + "RETURN" + micAltUp ' Click Tab Key
Type micAltDwn + "TAB" + micAltUp    ' Click Enter\Return

If u Want to enter characters

 oShell.SendKeys "{h}"           ' Click h Key
 oShell.SendKeys "{i}"           ' Click i Key

 Type micAltDwn + "h" + micAltUp    ' Click h Key
 Type micAltDwn + "i" + micAltUp    ' Click i Key
Share:
13,318
Diggs
Author by

Diggs

Updated on July 14, 2022

Comments

  • Diggs
    Diggs almost 2 years

    I have a web application that I am testing with HP's UFT software. Within my application, there is a text field with an onkeydown attribute. When a key is pressed within the text field, a function is called which triggers different actions depending on what key was pressed. I am interested in the enter key. When the enter key is pressed, rows are created within the form. How can I simulate the enter key being pressed within the field?

    I have tried

    field1.Set Chr(13)

    field1.FireEvent "onkeydown"

    but it doesn't trigger the event.

    I am trying aviod using the SendKeys command.

  • Diggs
    Diggs almost 11 years
    Perfect. It didn't occur to me that the decive replay worked the same way for keyboard events. It makes sense now. My testing scripts will be more robust with this change. Thanks!
  • shicky
    shicky over 9 years
    Are there implications to this? I've just used it to get around a nasty enter problem which I could not solve to save my life. Using this solves it but I wonder if I need to set the replayType back to 1 straight away after this works for me?
  • Motti
    Motti over 9 years
    When you use device mode it moves your mouse so if you're trying to use the computer for something else while running a test it will get in your way. Also if you're running the test on a remote machine with no display connected (e.g. the RDP window is minimized) then the device steps will not work. Other than that I don't know of any other implications. Arguably device mode is better for functional testing since it more accuratly mimics a real user's actions.