我正在使用机器人编写一些集成测试。我让机器人打开一个菜单,它应该从菜单中选择一个选项;除了回车键似乎被忽略了。菜单将打开并选择正确的菜单项,但如果我按回车键,则不会执行任何操作。如果我手动按下相应的键,它就会执行预期的操作。如果我在非 Java 应用程序上运行机器人,其他应用程序将正确响应输入事件。所以我已经验证我是否正确发送了 Enter 事件并且 java 菜单应该响应它;但有些东西不起作用。
我还尝试用空格事件替换输入事件(菜单也应该响应),但也没有得到响应。
我正在使用最新的 sun JDK 在 redhat linux 上运行。我很确定这不是我的代码的明显问题,因为我发现这篇文章描述了遇到完全相同问题的人,但它没有解决方案: http://www.velocityreviews.com/forums/t666100-robot-and-awt-on-linux.html
private void requestTest(String testName){
if(testName==currentTest)
return;
//overwrite config file with new data
currentTest=testName;
overwriteFile(configFile, getCurrentConfigFile());
//close current graph
pressKeyCombo(KeyEvent.VK_CONTROL, KeyEvent.VK_F4);
//open File menu
pressKeyCombo(KeyEvent.VK_ALT, KeyEvent.VK_F);
//select the 'load defaults' option
pressKey(KeyEvent.VK_DOWN);
pressKey(KeyEvent.VK_DOWN);
pressKey(KeyEvent.VK_DOWN);
pressKey(KeyEvent.VK_SPACE);
}
I'm writing some integration tests using a robot. I have the robot opening a menu and it should be selecting one of the options form the menu; except the enter key seems to be ignored. The menu will open and the correct menu item is selected, but no action has been taken as it should if I hit enter. If I manually press the appropriate keys it does what is expected. If I run the robot on a non-java application the other application will respond to the enter event correctly. So I've verified that I'm sending an enter event correctly and that java menu should respond to it; but something isn't working.
I've also tried replacing the enter event with a space event (with the menu should also respond to) and got no response either.
I'm running on redhat linux with the latest sun JDK. I'm pretty sure it's not an obvious problem with my code since I've found this post describing someone who ran into the exact same problem, but it has no solution: http://www.velocityreviews.com/forums/t666100-robot-and-awt-on-linux.html
private void requestTest(String testName){
if(testName==currentTest)
return;
//overwrite config file with new data
currentTest=testName;
overwriteFile(configFile, getCurrentConfigFile());
//close current graph
pressKeyCombo(KeyEvent.VK_CONTROL, KeyEvent.VK_F4);
//open File menu
pressKeyCombo(KeyEvent.VK_ALT, KeyEvent.VK_F);
//select the 'load defaults' option
pressKey(KeyEvent.VK_DOWN);
pressKey(KeyEvent.VK_DOWN);
pressKey(KeyEvent.VK_DOWN);
pressKey(KeyEvent.VK_SPACE);
}
发布评论
评论(3)
好吧,我陷入了按 Enter 键的同一问题。我也是,当我尝试使用 Enter 操作本机操作系统文件选择器对话框时,没有任何解释,但它不起作用。但似乎可以通过创建另一个调用 Enter 事件的 Robot 对象来解决该问题。因此,请让我至少帮助您或其他前来寻求咨询的孤独冲浪者。 ;-)
这确实是一个非常小的例子,但我希望上面代码中的解释能有所帮助。事实上,第二个 Robot 对象为我执行了 Enter 事件。
此致,
塞莫
Well, I got stuck on the same issue of pressing the Enter key. Me too, have no explanation when I tried to operate a native OS File Chooser dialogue using Enter in the end and it did not work. But it seems, that the issue can be solved by creating another Robot object where you call the Enter event. So please let me at least assist you or other lone surfers coming across to seek counsel. ;-)
This is indeed a very small example, but I hope the explanations in the code above will assist. Indeed, the second Robot object did the Enter event for me.
Best regards,
Semo
我猜你的问题是你只在按键向上时触发事件时才使用 VK_DOWN 。我的意思是你应该模拟按键输入事件,即向下然后向上。尝试一下,我希望这会起作用。
I guess that your problem is that you are using VK_DOWN only while the event is triggered when key is UP. I mean that you should simulate key typing event, i.e. DOWN and then UP. Try it, I hope this will work.
我终于解决了这个问题。我的机器人代码与按钮效果一起在事件调度线程中运行。这意味着在我的机器人完成执行之前,没有任何按钮可以响应我的机器人的动作。
我不完全确定为什么这会阻止我的机器人在这种情况下正常工作。我发现,如果我删除 ctrl-F4 命令,机器人将响应输入命令,但似乎这两个命令都应该在机器人返回时正确排队并执行。我假设排队的事件数量有限制,或者我不知何故最终在两个事件之间出现了数据争用。不管怎样,通过将我的机器人移动到一个单独的线程中,我可以获得所需的行为。
顺便说一句,我无法创建 SSCCE 的原因是因为我尝试将机器人合并到一个按钮中,并且尝试让机器人同时执行多个事件;但我没有尝试使用按钮生成执行多个事件的机器人。为了重现这个问题,我最终会得到与原始代码一样复杂的代码。
I finally solved this. I had my robot code running in the event-dispatching thread along with the button effects. This meant that none of the buttons could respond to my robot's action until my robot completed execution.
I'm not entirely certain why this would prevent my robot from working correctly in this case. I discovered that if I removed the ctrl-F4 command that the robot would respond to the enter command, but it seems as if both commands should have been queued and executed correctly the moment the robot returned. I assume either there is a limit to the number of events queued or that I somehow ended up with a datarace between the two events. Either way by moving my robot into a separate thread I get the desired behavior.
incidentally the reason I couldn't create a SSCCE was because I tried to incorporate the robot into a button and I tried having the robot do multiple events at once; but I didn't try a button spawning a robot doing multiple events. To recreate this issue I would have ended up with code just as complex as my original code.