制作FEST:s组件查找机制等待组件存在

时间:2012-01-21 22:38:57

标签: java fest

  

可能重复:
  Making FEST to wait for the application to load

注意:此问题与this question基本相同。由于这个问题没有答案,我决定将示例从那里扩展为可运行的SSCE,并提供一些额外的信息,希望得到一些帮助。

因此,问题在于当所寻找的组件可能尚不存在时,您应该如何处理组件查找。看看这个简单的标签GUI。

public class MyFrame extends JFrame {
    JLabel theLabel;

    public MyFrame() {
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        theLabel = new JLabel();
        theLabel.setName("theLabelName");
        computeLabelContentOnWorkerThread();
    }

    private void computeLabelContentOnWorkerThread() {
        new SwingWorker<String, Void>() {
            @Override
            protected String doInBackground() throws Exception {
                Thread.sleep(5000);
                return "Info from slow database connection";
            }

            @Override
            protected void done() {
                try {
                    theLabel.setText(get());
                    add(theLabel);
                    pack();
                    setVisible(true);
                } catch (InterruptedException ignore) {
                } catch (ExecutionException ignore) {
                }
            }
        }.execute();
    }
}

这个测试用例:

public class TestOfDelayedComponent extends FestSwingJUnitTestCase {

    FrameFixture frameWrapper;

    @Before
    public void onSetUp() {
        MyFrame frame = GuiActionRunner.execute(new GuiQuery<MyFrame>() {
            protected MyFrame executeInEDT() {
                return new MyFrame();
            }
        });
        frameWrapper = new FrameFixture(robot(), frame);
        frameWrapper.show();
    }

    @Test
    public void testLabelContent() {
        String labelContent = frameWrapper.label("theLabelName").text();
        assertTrue(labelContent.equals("Info from slow database connection"));
    }
}

会发生什么?标签组件的构造委托给慢速工作线程。因此,当GUI出现时,标签不会立即出现。运行测试用例时,标签没有出现,因此在frameWrapper.label("theLabelName")执行组件查找时,会抛出ComponentLookupException。

问题是如何防止抛出此异常?如果它是顶级组件,我可以WindowFinder.findFrame("title").withTimeout(10000)获取一个找到甚至可以找到帧的FrameFinder对象如果出现之前有延迟。我想要的是类似的东西,但对于其他类型的组件,例如一个JLabel。


注意:当然,自己实现这些功能并不困难。这样做很简单:

while(noComponentFound and notReachedTimeout){
    look for component using FEST
    sleep for a short delay
}

但是,不要强迫这些循环使测试脚本混乱。感觉好像等待组件在测试脚本中不是太不寻常的任务。因此,在我看来,在FEST中应该支持这样做。也许情况并非如此?是否无法等待组件?

2 个答案:

答案 0 :(得分:1)

有一种方法可以写暂停和等待的条件。以下是您需要的示例(noComponentFound和notReachedTimeout)。这可以通过Pause.pause(new ComponentFoundCondition(...),timeout_milis)来完成。例如:

    frame = WindowFinder.findFrame("frame0").using(robot); 
   //Wait for the event of loading tables in the GUI before we fail looking for them concurrently
    final GenericTypeMatcher<JTable> matcher = new GenericTypeMatcher<JTable>(JTable.class) {
          @Override protected boolean isMatching(JTable table){ return (table instanceof myTable && table.getColumnCount()<20); }  //Condition has to be totally identitary for one particular component. If not, we will never reach condition and so a timeout will be thrown in next line
    };                                                                                                                                     

    Pause.pause(new ComponentFoundCondition("Waiting for myTable to load before I look for it...", frame.robot.finder(), matcher, frame.target), 50000); //frame.target argument can be omitted. We also put an associated 50 sec timeout even if the condition is never satisfied
    fixedTable = frame.table(matcher); //Look finally for the table now we are sure its loaded

您可以使用不同的匹配器。例如,如果框架下只有一种类型的表myTable,那将非常简单:

    final ComponentMatcher matcher = new TypeMatcher(myTable.class); // We could use an easier TypeMatcher, because we would not need to add any additional distinction apart from the class type 
    Pause.pause(new Condition("Waiting for myTable to load...") { // If we are totally sure that there will be only one occurrence, we can use here the simplest form like above new ComponentFoundCondition("DebugMsg", frame.robot.finder(), matcher, frame.target)
        @Override public boolean test() { return !frame.robot.finder().findAll(frame.target, matcher).size().isEmpty(); } // If not we need a custom condition that checks for >0 instead of =1 results like ComponentNotFound.
     }, 50000); 

问题也在于(component-&gt; frame).table(matcher)不接受TypeMatcher,只接受GenericMatcher,所以我们毕竟应该创建GenericMatcher

如果你找不到任何东西,那么总是可以选择性地修复Pause.pause(5,TimeUnit.SECONDS);

答案 1 :(得分:0)

我不使用Fest,但是Pause.pause在这个页面看起来很有趣: http://www.pushing-pixels.org/2009/09/23/using-fest-swing-to-test-flamingo-components.html