Java FEST:等待GUI加载后再执行任何操作 公共作废设置()之前的@{ Robot Robot=BasicRobot.robotWithCurrentAwtHierarchy(); ApplicationLauncher.application(“myApp”).start(); 暂停。暂停(5,时间单位。秒); frame=WindowFinder.findFrame(“frame0”)。使用(机器人); JTableFixture table=frame.table(新的GenericTypeMatcher(JTable.class)){ @覆盖受保护的布尔isMatching(JTable表){ 返回(myTreeTable的表实例); } }); }
这段代码运行良好。如果我们删除5秒暂停,则找不到该表,因为应用程序需要几秒钟才能加载它 我想知道是否有更干净的方法。我尝试在ApplicationLauncher之后使用robot.waitForIdle()(我猜一旦EDT为空,所有内容都已加载),但它就是不起作用 我知道pause可以使用一些条件作为何时停止的事件,但我不知道如何编写它,因为JavaDoc和official doc很差Java FEST:等待GUI加载后再执行任何操作 公共作废设置()之前的@{ Robot Robot=BasicRobot.robotWithCurrentAwtHierarchy(); ApplicationLauncher.application(“myApp”).start(); 暂停。暂停(5,时间单位。秒); frame=WindowFinder.findFrame(“frame0”)。使用(机器人); JTableFixture table=frame.table(新的GenericTypeMatcher(JTable.class)){ @覆盖受保护的布尔isMatching(JTable表){ 返回(myTreeTable的表实例); } }); },java,swing,testing,robot,fest,Java,Swing,Testing,Robot,Fest,这段代码运行良好。如果我们删除5秒暂停,则找不到该表,因为应用程序需要几秒钟才能加载它 我想知道是否有更干净的方法。我尝试在ApplicationLauncher之后使用robot.waitForIdle()(我猜一旦EDT为空,所有内容都已加载),但它就是不起作用 我知道pause可以使用一些条件作为何时停止的事件,但我不知道如何编写它,因为JavaDoc和official doc很差 Pause.Pause(WaitForComponentToShowCondition.untilIsSh
- Pause.Pause(WaitForComponentToShowCondition.untilIsShowing(frame.component()):我需要一个组件,如果我传递包装器框架,它将不工作。我不能通过这张桌子,因为这正是我等待得到的
- 我明白我应该使用ComponentFoundCondition,但我不明白!我厌倦了:
@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); } }); }
ComponentMatcher matcher=新的GenericTypeMatcher(JTable.class){ @覆盖受保护的布尔isMatching(JTable表){ 返回(myTreeTable的表实例); } }; Pause.Pause(新组件foundcondition(“DebugMsg”,frame.robot.finder(),matcher));
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));
final ComponentMatcher matcher=newtypematcher(myTreeTable.class);
暂停。暂停(新条件(“等待myTreeTable”){
@凌驾
公共布尔测试(){
收集列表=
window.robot.finder().findAll(window.target,matcher);
返回列表.size()>0;
}
}, 5000);
以下是按名称查找的备选方案:
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 name matcher=new ComponentMatcher(){
@凌驾
公共布尔匹配(组件c){
返回“ComponentName”.equals(c.getName())和&c.isShowing();
}
};
暂停。暂停(新条件(“等待”){
@凌驾
公共布尔测试(){
收集列表=
window.robot.finder().findAll(window.target,nameMatcher);
返回列表.size()>0;
}
}, 5000);
奇怪的是,对于这个目标,已经存在一个条件子类ComponentFoundCondition。因此,答案的第一部分应该等于Pause.Pause(新组件foundcondition(“Waiting formyTreeTable to load…”,frame.robot.finder(),matcher,frame.target),50000);但似乎条件永远不会满足,你最终会超时好吧,我发现了问题。发现多个事件被视为“无”,即失败。这意味着,如果您确定只有一个myTreeTable,则可以使用匹配更好的Pause.Pause(新组件foundCondition(“Waiting formyTreeTable to load…”,frame.robot.finder(),matcher,frame.target),50000);如果不是,那么最好像您那样继承该条件,并将size()>0 so size()=2放入其validOne问题:我假设此代码必须在EDT中运行,因为访问组件通常不是线程安全的,对吗?因此,如果我们在测试代码中使用它,我们必须将访问包装在GuiQuery
?@nikow中,我假设finder正在EDT上执行它的方法。如果这是真的,那么就没有必要包装这些操作。不幸的是,我手头没有环境来测试这个假设。还有可能有用的EdtSafeCondition
。是的,这很有意义,我发现ComponentMatcher
用@RunsInEDT注释。谢谢
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);