title: Making FEST:s component lookup mechanism wait for a component to exist
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/8803471/making-fest-to-wait-for-the-application-to-load">Making FEST to wait for the application to load</a>  </p>
  </blockquote>
  
  
  
  <p>NOTE: This question is basically identical to <a href="https://stackoverflow.com/q/8803471/151344">this question</a>. Since there were no answers to that question, I decided to extend the example from there into a runnable SSCE, and provide some additional information, hoping to get some help.</p>
  
  <p>So, the question is about how you should handle component lookups when the sought component might not yet exist. Look at this simple one label GUI.</p>
  
  <pre><code>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&lt;String, Void&gt;() {
              @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();
      }
  }
  </code></pre>
  
  <p>And this test case:</p>
  
  <pre><code>public class TestOfDelayedComponent extends FestSwingJUnitTestCase {
  
      FrameFixture frameWrapper;
  
      @Before
      public void onSetUp() {
          MyFrame frame = GuiActionRunner.execute(new GuiQuery&lt;MyFrame&gt;() {
              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"));
      }
  }
  </code></pre>
  
  <p>What happens? The construction of the label component is delegated to a slow worker thread. So the label will not appear right away when the GUI appears. When the test case is run, the label has not appeared, so when executing the component lookup at <code>frameWrapper.label("theLabelName")</code>, a ComponentLookupException is thrown.</p>
  
  <p>The question is <strong>how do I prevent this exception from being thrown?</strong> If it was a top level component, I could do <code>WindowFinder.findFrame("title").withTimeout(10000)</code> to get a FrameFinder object which finds can find frames even if there is a delay before they appear. What I want is something similar to that, but for other types of components, such as e.g. a JLabel.</p>
  
  <hr>
  
  <p>NOTE: Surely, it wouldn't be all that difficult to implement the functionality by yourself. It would be rather simple to do:</p>
  
  <pre><code>while(noComponentFound and notReachedTimeout){
      look for component using FEST
      sleep for a short delay
  }
  </code></pre>
  
  <p>However, it would be nice to not be forced to clutter the test scripts with such loops. It feels as if waiting for components is not a too unusual task in test scripts. So, in my opinion, there ought to be support for doing this in FEST. Maybe this is not the case? Is it not possible to wait for components?</p>