Skip to end of metadata
Go to start of metadata

FEST-Swing provides support for finding GUI components after the execution of a long-duration task is complete. Currently, it provides the following finders:

All these finders are in the package org.fest.swing.finder.

Lookup by Name

A good example is the main window of an application being shown after the user's credentials have been successfully verified. The following are the typical steps to complete such scenario:

  1. User launches the application
  2. A login window appears
  3. User enters her username and password and clicks the "Login" button
  4. User is authenticated and authorized successfully
  5. The main window of the application is displayed

The "tricky" part here is step 4. Authentication/authorization can take some time (depending on network traffic, etc.) and we need to wait for the main window to appear in order to continue our test. It is possible to test this scenario with FEST:

The findFrame method (statically imported from org.fest.swing.finder.WindowFinder) can look up a Frame having "main" as its name with a default timeout of 5 seconds. This means that if in 5 seconds the frame we are looking for is not found, the test will fail.

We can also specify a custom value for the timeout. For example, we can set the timeout to 10 seconds in two ways:

Lookup by Type

We can also find components by type. In this example, we look up a showing a Frame of type MainFrame, using the default timeout of 5 seconds.

Lookup using a GenericTypeMatcher

All finders accept a GenericTypeMatcher to find components using custom search criteria.

In the following example, we look up a showing Frame with title "News":

Reusing Robots

In our examples, we have been using the following method call:

Although strange, this is necessary because, in a given test, only one instance of Robot can be running, to prevent GUI tests from blocking each other on the screen. In another words, in a test class you can only use one and only one instance of Robot.

Labels
  1. Mar 27, 2012

    Hi Experts,

    I have a swing application(LPDevOptionUI) where, in its constructor a JOptionPane is displayed with one OK button, i.e. it comes even before i perform LPDevOptionUI.setVisible();

    Now i need to find this JOptionPane and click OK button . For doing this i am using generic type matcher alongwith JOptionPaneFinder.findOptionPane(matcher).

    Here is the code explained:

    //-----------Launching application--------------

    application("LPDevOptionUI").start();

    //-----------Class Finding Joption Pane and clicking button-------------

    @RunWith(GUITestRunner.class)
    public class GuiStartTestCases extends FestSwingJUnitTestCase {
    JOptionPaneFixture optionPane1;

    //---------Finding JoptionPane-------------

    protected void onSetUp() {

    GenericTypeMatcher<JOptionPane> matcher = new GenericTypeMatcher<JOptionPane>(JOptionPane.class) {
    protected boolean isMatching(JOptionPane jOptionPane) {
    return "Please wait, Initialising".equalsIgnoreCase((String)jOptionPane.getMessage());
    }
    };
    optionPane1 = JOptionPaneFinder.findOptionPane(matcher).withTimeout(10000).using(this.robot());
    }

    //------------Clicking Button ------------------
    public void clickInitialisationInfo() {
    final JButtonFixture button = optionPane1.button("OK");
    GuiActionRunner.execute(new GuiTask() {
    public void executeInEDT() {
    button.click();
    }
    });

    }
    }

    PROBLEM : 

    I get following error even before JoptionPane is actually displayed:

    Exception in thread "main" java.lang.NullPointerException
    at org.fest.swing.finder.ComponentFinderTemplate.findComponentWith(ComponentFinderTemplate.java:115)
    at org.fest.swing.finder.JOptionPaneFinder.using(JOptionPaneFinder.java:117)
    at GuiStartTestCases.onSetUp(GuiStartTestCases.java:49)

    How can i catch this JoptionPane, Please help.