如何使用 Java 读取/绘制/与另一个 Java 窗口/小程序交互
我有兴趣编写一个执行以下操作的 Java 程序。
- 附加到当前正在运行的 Swing 窗口或小程序,在另一个 JVM 中运行
- 或者,加载 Java 应用程序以便能够执行上述操作
- 从窗口读取颜色
- 将鼠标和键盘事件发送到窗口
我希望制作某种直接与 GUI 交互的测试和自动化工具。
我正在寻找有关如何完成上述所有步骤的一些建议。提前致谢。
I am interested in writing a Java program which does the following.
- Attach to a currently running Swing window or applet, running in another JVM
- Alternatively, load a Java application so as to be able to do the above
- Read colors from the window
- Send mouse and keyboard events to the window
I am hoping to make some sort of testing and automation tool that interacts with GUIs directly.
I am looking for some advice on how to accomplish any and all of the steps above. Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
捕获另一个进程的输出不会那么容易 - 如果您的测试应用程序在同一个 java VM 中直接启动 swing 应用程序,这将是最简单的。
然后,您可以在 JFrame 上调用 Paint(Graphics g),向组件传递离屏图形 (BufferedImage - 详细信息请参见。)您可以通过 EventQueue.postEvent(AWTEvent) - 这可用于模拟 AWT 输入。
但是,您调查过现有的测试框架吗? FEST 有一个专门用于操作和验证 UI 的测试框架。还有abbot,较老,受到一些人的青睐。还有一些测试框架专注于功能和状态,而不是屏幕抓取和输入事件。这些并不是更好/更差,而是互补的。
状态 UI 测试包括 SwingUnit 和 UISpec4J。
那里有很多好的框架,因此在构建另一个框架之前做一些研究可能是值得的!
编辑:
要启动应用程序,而不是运行
您
像这样运行并实现 TestWrapper
}
启动应用程序后,您可以等待应用程序启动,然后轮询
Window.getOwnerlessWindows
查找应用创建的任何顶级窗口。更直接的方法是安装自己的 RepainManager - 您可以委托给现有的。所有窗口绘制操作都会调用此方法,因此您可以直接进入窗口层次结构的中心,
您还可以注册以侦听 AWT EventQueue 上的所有事件。这还可以让您了解应用程序中发生的情况,从中您可以确定创建了哪些窗口、焦点等。
It's not going to be so easy to capture the output of a another process - it will be simplest if your test app launches directly the swing app, in the same java VM.
You can then call paint(Graphics g) on the JFrame, passing the component an off screen graphics (BufferedImage - details here.) You can send input events to the AWT event queue via EventQueue.postEvent(AWTEvent) - this can be used to simulate AWT input.
However, have you surveyed the existing test frameworks out there? FEST has a test framework specifically for manipulating and verifying UI. There is also abbot, older by favored by some. There are also test frameworks that concentrate on function and state rather than screen grabs and input events. These are not better/worse, but complementary.
State UI testing includes SwingUnit, and UISpec4J.
There are a lot of good frameworks out there, so it might pay to do a little research before building yet another one!
EDIT:
To launch an application, instead of running
You run
and implement TestWrapper like this
}
Once you have launched the app, you can wait for the app to start, and poll
Window.getOwnerlessWindows
to find any top-level windows the app creates.A more direct approach is to install your own RepainManager - you canjust delegate to the existing one. This is invoked for all window drawing operations, an so you get right in the heart of the window hierarchy,
You can also register to listen to all events on the AWT EventQueue. That will also give you an inside view on what is happening in the app, and from that you can determine which windows are created, in focus etc..
如果您可以控制“已测试”应用程序,我认为您最好的选择可能是创建一个服务器,允许测试程序代理对内部
java.awt.Robot
的调用。If you have control over the 'tested' application, I think your best bet might be to create a server which allowed the tester program to proxy calls to an internal
java.awt.Robot
.您需要在目标应用程序中编写并支持特定协议(您自己发明的)。
您可以使用 RMI、Sockets 或其他形式的 RCP(Protobuf、JSON、WebService 等)来完成此操作。
要点是,您必须在应用程序中打开一个端口,并且能够理解来自外部源的指令。
如果您正在寻找一种测试方法,我听说 Abbot 是一个不错的选择。看看吧。
如果您想操作一个应用程序(您没有源代码),这可能会更复杂。
You'll need to write and support an specific protocol ( of your own invention ) in the target app.
You can do it with RMI, Sockets or some other form of RCP ( Protobuf, JSON, WebService etc. )
The point is, you have to open a port in the app and be able to understand instructions from an external source.
If what you're looking a a way to test it, I've heard Abbot is a good option. Take a look.
If you want to manipulate an application ( of which you don't have the source ) that may be more complicated.