可能重复:
使 FEST 等待应用程序加载
注意:这个问题与这个问题基本相同。由于该问题没有答案,我决定将示例从那里扩展到可运行的 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 中这样做。也许事实并非如此?难道不能等待组件吗?