如何在JAVA中触发一个KeyPressed事件进行单元测试 [英] How to trigger a KeyPressed event for unit testing in JAVA

查看:601
本文介绍了如何在JAVA中触发一个KeyPressed事件进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,所以我想单元测试一些GUI元素,我有一个JButton,文本为Create,助记符设置在'C'或代码67上。我可以编写一个单元测试,我可以调用它。 doClick函数并声明该按钮应该执行的操作。然而,我现在想编写一个单元测试来测试同一个按钮的助记符部分是否与按钮单击相同。我已经搜索了几天,并尝试了很多东西。

我尝试过使用awt库中的Robot:

  robot.keyPress(KeyEvent.VK_C); 
robot.keyRelease(KeyEvent.VK_C);

没有事件被触发,最近我尝试了这个EventQueue例子:

  final Toolkit toolkit = Toolkit.getDefaultToolkit(); 
final EventQueue eventQueue = toolkit.getSystemEventQueue();
gPane = new SwingBuilder();
panel = new MainMenuPanel();
panel.displayMainMenu(gPane);

@Test
void ShouldMakeRaceGenderPanelVisibleKeyPress(){
eventQueue.postEvent(new KeyEvent(panel.gPane.mainM,KeyEvent.KEY_PRESSED,System.currentTimeMillis(),0,KeyEvent。 VK_C).getKeyCode());

assertThat panel.gPane.mainM.isVisible(),is(false);
assertThat panel.gPane.raceGender.isVisible(),是(true);
}

我知道GUI测试可能很棘手,并且有工具可以模拟用户交互,但真的很难模拟或触发一个KeyEvent?



欢迎任何帮助,我们也欢迎任何推荐的UI测试工具。 / p>

谢谢

注意:这是Java和Groovy的组合,所以我使用SwingBuilder构建我的用户界面

解决方案

您声明: $ b


我有一个JButton,它的文本是Create,助记符在'C'或代码67上设置。...

但是,我现在想写一个单元测试来测试助记符部分该按钮的作用与按钮点击完全相同。 ...



我试过从awt库使用Robot:



  robot.keyPress(KeyEvent.VK_C); 
robot.keyRelease(KeyEvent.VK_C);

也许这个问题是由于你没有考虑到JButton助记符是alt键组合,所以你的机器人代码可能会反映出这一点:

  robot.keyPress(KeyEvent.VK_ALT); 
robot.keyPress(KeyEvent.VK_C);

//可能需要一个小的Thread.sleep(...)在这里?

robot.keyRelease(KeyEvent.VK_C);
robot.keyRelease(KeyEvent.VK_ALT);


Ok, so I'm trying to Unit test some GUI elements I have a JButton with text of "Create" and the mnemonic set on 'C' or code 67. I can write a unit test that I can call the .doClick function on and assert that what that button is supposed to do, happens.

However, I now want to write a unit test to test that the mnemonic portion of that same button works identically to the button click. I've searched on and off for a couple of days now and have tried many things.

I've tried using Robot from the awt library:

robot.keyPress(KeyEvent.VK_C);
robot.keyRelease(KeyEvent.VK_C);

To which no event is being triggered and most recently I've tried this EventQueue example:

final Toolkit toolkit = Toolkit.getDefaultToolkit();
final EventQueue eventQueue = toolkit.getSystemEventQueue();
gPane = new SwingBuilder();
panel = new MainMenuPanel();
panel.displayMainMenu(gPane); 

@Test
void ShouldMakeRaceGenderPanelVisibleKeyPress(){
eventQueue.postEvent( new KeyEvent(panel.gPane.mainM, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, KeyEvent.VK_C ).getKeyCode());

    assertThat panel.gPane.mainM.isVisible(), is(false);
    assertThat panel.gPane.raceGender.isVisible(), is(true);
}

I know that GUI testing can be tricky and there are tools out there to simulate user interaction, but is it really that hard to simulate or trigger just a single KeyEvent?

Any assistance is welcome, and any UI testing tools recommended is welcome as well.

Thanks

Note: This is a combination of Java and Groovy, so I'm using SwingBuilder to build my UI's

解决方案

You state:

I have a JButton with text of "Create" and the mnemonic set on 'C' or code 67. ...
However, I now want to write a unit test to test that the mnemonic portion of that same button works identically to the button click. ...

I've tried using Robot from the awt library:

robot.keyPress(KeyEvent.VK_C);
robot.keyRelease(KeyEvent.VK_C);

Perhaps the problem is due to your not taking into account that JButton mnemonics are alt-key combinations, and so your robot code would probably have to reflect this:

robot.keyPress(KeyEvent.VK_ALT);
robot.keyPress(KeyEvent.VK_C);

// possibly want a small Thread.sleep(...) here?

robot.keyRelease(KeyEvent.VK_C);
robot.keyRelease(KeyEvent.VK_ALT);

这篇关于如何在JAVA中触发一个KeyPressed事件进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆