gpt4 book ai didi

java - 制作节 :s component lookup mechanism wait for a component to exist

转载 作者:太空宇宙 更新时间:2023-11-04 08:24:05 24 4
gpt4 key购买 nike

Possible Duplicate:
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"));
}
}

会发生什么?标签组件的构造被委托(delegate)给一个缓慢的工作线程。因此,当 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 应该支持这样做。也许事实并非如此?难道不能等待组件吗?

最佳答案

有一种方法可以编写暂停和等待的条件。这是您需要的 while(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->frame).table(matcher) 不接受 TypeMatcher,而只是接受 GenericMatcher,所以无论如何我们都应该创建 GenericMatcher

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

关于java - 制作节 :s component lookup mechanism wait for a component to exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8957334/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com