简体   繁体   English

测试用Java Swing编码的应用程序GUI:最佳方法?

[英]Test an application GUI coded with Java Swing : best approach?

I have to automatically generate test for a Java "drawing" application GUI (like ArgoUML/LaTeXDraw/etc). 我必须自动为Java“绘图”应用程序GUI(如ArgoUML / LaTeXDraw / etc)生成测试。 I need to verify that the tested application behave correctly. 我需要验证经过测试的应用程序的行为是否正确。 In my given example, I need to check that the Undo/Redo buttons works well = Undo put the canvas in the predecessing state and Redo put it again in the state it used to be. 在给定的示例中,我需要检查“撤消/重做”按钮是否工作正常=“撤消”将画布置于前移状态,而“重做”又将其置于以前的状态。

I have already an idea of how I will approach the problem but I would like feedback of experienced people: 我已经对如何解决这个问题有了一个想法,但是我希望有经验的人给予反馈:

  • Should I use the Robot class to simulate the inputs onto the GUI? 我应该使用Robot类来模拟GUI上的输入吗? Or is there an easier way? 还是有更简单的方法?
  • I need to capture the state of the program for later analysis to see if the state of the program is consistent, should I just screenshot the window or is there a way to iterate through all the JFrame's components and capture their properties (it should include canvas state)? 我需要捕获程序的状态以供以后分析,以查看程序的状态是否一致,是否应该对窗口进行屏幕截图或是否有办法遍历所有JFrame的组件并捕获其属性(它应该包含画布)州)?

  • More importantly, I do not know how to locate each button on the interface. 更重要的是,我不知道如何在界面上找到每个按钮。 What is the simplest way to tell the Robot that a given button is at a certain (x, y) position in the window? 告诉机器人给定的按钮在窗口中的某个(x,y)位置的最简单方法是什么? Should I search the pixels in a .jpeg version of the button? 我应该在按钮的.jpeg版本中搜索像素吗? Should I locate each button on the interface manually (ie create a class for each button containing x, y attributes)? 我是否应该手动在界面上找到每个按钮(即为每个包含x,y属性的按钮创建一个类)?

  • And finally, I have never "attached" a Robot to a given program. 最后,我从未将机器人绑定到给定程序。 Can I just load the .jar file of the software I want to test and tell Robot to operate on that window? 我可以只加载要测试的软件的.jar文件,并告诉Robot在该窗口上运行吗? Or do I need to work with all the source files from the original project? 还是我需要处理原始项目中的所有源文件?

Thank you for your time, if any part of my question seems not clear enough please tell me and I will try to explain it even more clearly. 谢谢您的宝贵时间,如果我的问题中的任何部分似乎不够清楚,请告诉我,我会尽力更清楚地解释一下。

PS: any advice concerning the work I need to do is welcome :) 附言:关于我需要做的工作的任何建议都欢迎:)

I've done what you've explained in the comment to the other answer. 我已经完成了您在其他答案的评论中解释的内容。

Robot can be useful for various parts of the process but, depending on how it was written, the desire to follow best testing practices, and overall needs it can get limited quick. Robot可以在过程的各个部分使用,但取决于编写方式,遵循最佳测试实践的愿望以及总体需求,它可能会很快受到限制。 If you didn't write it or best coding practices keep you from getting access to or directly testing aspects of the application then other means are necessary. 如果您未编写代码或最佳编码实践使您无法访问或直接测试应用程序的各个方面,则需要采取其他方法。

My experience led me to tunnel down the main entry point using reflection to test the actual state (like text values of various components). 根据我的经验,我使用反射来测试实际状态(例如各种组件的文本值),从而在主要入口点建立隧道。 Robot was only useful for my case to perform key events . Robot仅对我执行关键事件有用。

Ultimately, the answer(s) depend on many factors: 最终,答案取决于许多因素:

  1. Robot? 机器人? - Sure, use it as far as it suits your needs. -当然,请根据需要使用它。
  2. Do what's easiest while still meeting the requirements. 在满足要求的同时做最简单的事情。 If a screen shot will do, do it (with Robot ). 如果可以截屏,请执行此操作(使用Robot )。 If you need more information you can get the components of a JFrame or JPanel and save what ever properties offered. 如果您需要更多信息,则可以获取 JFrameJPanel 的组件并保存提供的属性。
  3. This can be done a number of ways though investigation of the UI. 可以通过调查UI的多种方式来完成此操作。 Finding the button and doing doClick directly on it, moving the mouse with Robot and doing a mouse press, etc. 寻找按钮和做doClick直接就可以了,移动鼠标Robot ,做一个按下鼠标等。
  4. When you instantiate a new Robot it will be linked to the main device. 实例化一个新的Robot ,它将被链接到主设备。 Nothing more is needed in simple scenarios than new Robot().keyPress(KeyEvent.VK_ENTER); 在简单的场景中,仅需要new Robot().keyPress(KeyEvent.VK_ENTER);

Given all that, there are pitfalls like focus. 考虑到所有这些,存在着诸如陷阱之类的陷阱。 It's best to start your simulation/test and not touch any peripheral of the machine. 最好开始模拟/测试,不要触摸机器的任何外围设备。 Also, depending on the running time of some methods you may want to Thread.sleep() (as we regrettably did) or use joined threads. 另外,根据某些方法的运行时间,您可能想要Thread.sleep() (我们很遗憾这样做)或使用连接的线程。

You did not add any code so I can just guess: 您没有添加任何代码,因此我只能猜测:

If your code follows the MV(V)C pattern you don't need to test though the GUI. 如果您的代码遵循MV(V)C模式,则无需通过GUI进行测试。 You can directly test the controller methods that handle the redo/undo and verify that they correctly change the model and communicate with the undo/redo stack (is is part of the model in my world...). 您可以直接测试处理重做/撤消的控制器方法,并验证它们是否正确更改了模型并与撤消/重做堆栈进行通信(这是我的世界中模型的一部分...)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM