java - FEST:在执行任何操作之前等待 GUI 加载

标签 java swing testing robot fest

    @Before public void setUp() {
        Robot robot = BasicRobot.robotWithCurrentAwtHierarchy();
        ApplicationLauncher.application("myApp").start(); 

        Pause.pause(5, TimeUnit.SECONDS); 
        frame = WindowFinder.findFrame("frame0").using(robot);

        JTableFixture table = frame.table(new GenericTypeMatcher<JTable>(JTable.class) {
             @Override protected boolean isMatching(JTable table) {
                return (table instanceof myTreeTable); 
             }  
        });
    }

这段代码运行良好。如果我们删除 5 秒的暂停,则找不到该表,因为应用程序需要几秒钟的时间来加载它。

我想知道是否有更干净的方法。我在 ApplicationLauncher 之后尝试使用 robots.waitForIdle() (我猜一旦 EDT 为空,所有内容都会加载),但它不起作用。

我知道暂停可以使用一些条件作为何时停止的事件,但我不明白如何编写它,因为 JavaDoc 和官方文档很差。

  • Pause.pause(WaitForComponentToShowCondition.untilIsShowing(frame.component())) :我需要一个组件,如果我传递包装框架,它就不起作用。我无法通过 table ,因为这正是我等待得到的。
  • 我明白了,那么我可能应该使用 ComponentFoundCondition,但我不明白!我厌倦了:

           ComponentMatcher matcher = new GenericTypeMatcher<JTable>(JTable.class) {
               @Override protected boolean isMatching(JTable table) {
                 return (table instanceof myTreeTable); 
               }  
           };
    
           Pause.pause(new ComponentFoundCondition("DebugMsg", frame.robot.finder(), matcher)); 
    

有什么帮助吗?

最佳答案

您可以使用ComponentFinder来定位该组件。例如,根据问题中的匹配器:

final ComponentMatcher matcher = new TypeMatcher(myTreeTable.class);

Pause.pause(new Condition("Waiting for myTreeTable") {
    @Override
    public boolean test() {
        Collection<Component> list = 
                window.robot.finder().findAll(window.target, matcher);
        return list.size() > 0;
    }
 }, 5000); 

这是按名称查找的替代方案:

final ComponentMatcher nameMatcher = new ComponentMatcher(){
    @Override
    public boolean matches(Component c) {
        return "ComponentName".equals(c.getName()) && c.isShowing();
    }
};

Pause.pause(new Condition("Waiting") {
    @Override
    public boolean test() {
        Collection<Component> list = 
                window.robot.finder().findAll(window.target, nameMatcher);
        return list.size() > 0;
    }
 }, 5000);

关于java - FEST:在执行任何操作之前等待 GUI 加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11674383/

相关文章:

Java 垃圾收集器导致严重滞后

java - libgdx Screen - 如何通过字符串设置新屏幕

java - 如何在jsp中将本地视频放到我的本地主机上

java - 如何将 byteArray 转换为位图图像以在 Android 中的 imageview 中显示?

java BoxLayout面板的对齐方式

java - 鼠标点击时显示图像?

Java JTextfield 固定文本,允许在文本开头附加文本

unit-testing - 断言消息 : assume failure, 或假定成功

android - 如何使用 Espresso 测试由 TaskStackBuilder 生成的 PendingIntent

java - 有条件地跳过 TestNG 测试