FEST: Wait for the GUI to load before doing anythi

2019-05-23 14:13发布

问题:

    @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); 
             }  
        });
    }

This code works well. If we remove the 5 seconds pause, then the table is not found because it takes some seconds to the app to load it.

I would like to know if there is a cleaner way of doing it. I tried with robot.waitForIdle() after ApplicationLauncher (I guess once EDT is empty, everything is loaded), but it just doesn´t work.

I know pause can use some conditions as an event on when to stop, but I don´t understand how to write it since JavaDoc and official doc is poor.

  • Pause.pause(WaitForComponentToShowCondition.untilIsShowing(frame.component())) : I need a component, if I pass the wrapper frame it does not work. And I cannot pass the table because thats precisely what I am waiting for to get.
  • I understand then I should probably work with ComponentFoundCondition but I dont get it! I tired with:

           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)); 
    

Any help?

回答1:

You could use ComponentFinder to locate the component. For example, based on the matcher in the question:

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); 

Here is an alternative with lookup by name:

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);