javascript 如何在 UFT 中模拟键盘输入事件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17885009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you simulate a keyboard enter event in UFT
提问by Diggs
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?
我有一个正在使用 HP 的 UFT 软件进行测试的 Web 应用程序。在我的应用程序中,有一个带有 onkeydown 属性的文本字段。当在文本字段中按下某个键时,会调用一个函数,该函数根据按下的键触发不同的操作。我对回车键感兴趣。当按下回车键时,在表单中创建行。如何模拟在字段内按下的回车键?
I have tried
我努力了
field1.Set Chr(13)
field1.Set Chr(13)
field1.FireEvent "onkeydown"
field1.FireEvent "onkeydown"
but it doesn't trigger the event.
但它不会触发事件。
I am trying aviod using the SendKeys command.
我正在使用 SendKeys 命令尝试 aviod。
回答by Motti
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.
如果您使用设备重放模式(如本答案所述)并发送一个vbCRLF
您的应用程序将能够看到回车键。
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:
这适用于以下示例页面(在 IE 上):
<!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>
回答by Viraj John Maria
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