与可执行 JAR 交互以操作 Swing GUI

发布于 2024-10-08 11:09:12 字数 212 浏览 0 评论 0原文

我有一个可执行的 JAR,它会打开一个 Swing GUI,在选择框等中进行一些选择并按下“确定”按钮后,运行一个进程。我想自动化做出这些选择和运行流程的行为。

我无法访问源代码或任何文档,所以在我看来,唯一的方法就是以某种方式连接到 GUI 并模拟用户。最坏的情况是使用 java.awt.Robot 实际操作光标以进行所需的更改,但这非常混乱且容易出错。有没有更好的方法来与 GUI 交互?

I have a executable JAR which brings up a Swing GUI and, after some choices in select boxes and such and pressing an OK button, runs a process. I would like to automate the act of making these choices and running the process.

I don't have access to the source code or any documentation, so it seems to me like the only way to do this is to somehow connect to the GUI and simulate a user. A worst-case scenario is using a java.awt.Robot to actually manipulate the cursor to make the desired changes, but that is extremely messy and error-prone. Are there any better ways to interface with the GUI?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

向地狱狂奔 2024-10-15 11:09:12

在这个答案中,我假设您可以将 jar 加载到您自己的 main() 中并调用它们的 GUI 入口类。

Swing(如 AWT)让您可以轻松地审视 GUI。 GUI 可能是一棵由面板和子项组成的树。一旦您递归遍历树并找到您想要调用的小部件(这是困难的部分),您只需以编程方式单击按钮等即可。

[偏离正线……]
我经常使用这种技术对我自己的 GUI 进行单元测试。我确保在计划内省的组件上调用“setName()”,并通过 UI 进行深度优先递归,直到“getName()”产生我正在寻找的字符串。这让我可以重构 GUI,而无需对测试框架进行太多更改。同样的概念通常也适用于使用第三方 GUI,但可惜的是,他们很少使用 setName(),因此识别组件可能会更困难——您需要查看按钮文本等。

In this answer, I assume you can load the jar into your own main() and invoke their GUI entry class.

Swing (like AWT) lets you introspect a GUI quite easily. The GUI is probably a tree of panels and children. Once you recurse through the tree and find the widget you want to invoke (that's the hard part), you can just click buttons, etc programmatically.

[going off on a tangent...]
I use this technique routinely for unit testing my own GUIs. I make sure to call "setName()" on the components I plan to introspect and I depth-first recurse through my UI until "getName()" yields the string I'm looking for. This lets me refactor the GUI without needing to change the test scaffolding very much. The same concept can often apply to working with third-party GUIs, but alas they rarely use setName() so identifying components can be harder -- you need to look at the button text, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文