Possible duplicate:
Creating FEST to wait for application loading
NOTE. This question is basically identical to this question . Since there were no answers to this question, I decided to extend the example from there into the executable SSCE and provide additional information, hoping to get some help.
So the question is how should you handle component searches when the component you are looking for does not exist yet. Take a look at this simple shortcut 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(); } }
And this test case:
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")); } }
What's happening? The design of the label component is delegated to the slow worker thread. Thus, the shortcut will not appear immediately after the appearance of the GUI. When the test case was started, the label did not appear, therefore, when performing a component search in frameWrapper.label("theLabelName") , a ComponentLookupException exception is thrown.
Question: how can I eliminate this exception? If it was a top-level component, I could do WindowFinder.findFrame("title").withTimeout(10000) to get the FrameFinder object that it finds, can find the frames, even if it's a delay before they appear. What I want looks like this, but for other types of components, such as, for example, JLabel.
NOTE. Of course, not all will be difficult to implement this functionality. That would be pretty simple:
while(noComponentFound and notReachedTimeout){ look for component using FEST sleep for a short delay }
However, it would be nice if you were not forced to clutter up test scripts with such cycles. It seems that waiting for components is not too unusual a task in test cases. Therefore, in my opinion, in FEST there should be support for this. Maybe this is not so? Is it impossible to wait for the components?