Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0

FEST-Swing provides base TestCase classes that takes care of most of the plumbing involved when writing a GUI test. As usual, this feature supports both TestNG (via FestSwingTestngTestCase) and JUnit (via FestSwingJUnitTestCase.)

In general, the provided TestCases do the following:

  1. Install FailOnThreadViolationRepaintManager to check that all access to Swing components is performed in the EDT
  2. Creates a new Robot, using a new component hierarchy
  3. Cleans up resources: releases the semaphore that ensures sequential test execution
Warning
titleWarning

When using a base TestCase, do not create a new Robot. The base TestCase creates one for you. If there is more than one Robot in your test, only the first one will have access to the screen, while the rest will block till they get the 'screen lock'. A Robot can be created manually or indirectly using the constructors FrameFixture(Frame) or DialogFixture(Dialog). Please use the overloaded versions that take a Robot as parameter, passing the already created Robot (by calling the method robot().)

As reference, the following code listing shows a GUI test that is not using extending a TestCase:

Code Block
java
java
import org.testng.annotations.*;
import org.fest.swing.fixture.FrameFixture;

public class FirstGUITest {

  private FrameFixture window;

  @BeforeClass public void setUpOnce() {
    FailOnThreadViolationRepaintManager.install();
  }

  @BeforeMethod public void setUp() {
    MyFrame frame = GuiActionRunner.execute(new GuiQuery<MyFrame>() {
        protected MyFrame executeInEDT() {
          return new MyFrame();  
        }
    });
    window = new FrameFixture(frame);
    window.show(); // shows the frame to test
  }

  @AfterMethod public void tearDown() {
    window.cleanUp();
  }

  @Test public void shouldCopyTextInLabelWhenClickingButton() {
    window.textBox("textToCopy").enterText("Some random text");
    window.button("copyButton").click();
    window.label("copiedText").requireText("Some random text");
  }
}

The following code listing shows the same test, extending FestSwingTestngTestCase:

Code Block
java
java
import org.testng.annotations.*;
import org.fest.swing.fixture.FrameFixture;
import org.fest.swing.testng.testcase.FestSwingTestngTestCase;

public class FirstGUITest extends FestSwingTestngTestCase {

  private FrameFixture window;

  protected void onSetUp() {
    MyFrame frame = GuiActionRunner.execute(new GuiQuery<MyFrame>() {
        protected MyFrame executeInEDT() {
          return new MyFrame();  
        }
    });
    // IMPORTANT: note the call to 'robot()'
    // we must use the Robot from FestSwingTestngTestCase
    window = new FrameFixture(robot(), frame);
    window.show(); // shows the frame to test
  }

  @Test public void shouldCopyTextInLabelWhenClickingButton() {
    window.textBox("textToCopy").enterText("Some random text");
    window.button("copyButton").click();
    window.label("copiedText").requireText("Some random text");
  }
}

See also: