Developer testing of GUIs, like those written in Swing for example, have always been a challenge. Regardless of how a particular GUI is coded, ascertaining testing plug-in points and determining how to structure a particular test case presents barriers that often force one to postpone testing until a more manual effort can be conducted. As I mentioned a few months ago, there are a number of frameworks available that facilitate testing various GUI frameworks; however, of late, I have found that one particular framework for testing Java GUIs stands out– testng-abbot.

While the framework is fairly new and lacking of a lot of documentation, the features found in the latest version make testing a GUI (either AWT or Swing) quite easy. For example, testing the following scenario takes one test case and an associated fixture. Below is a sample Swing GUI that excepts two parameters representing a Widget’s name and weight.

In a sunny day scenario, if a user enters a valid weight like that shown below, a successful creation message is shown.

Pressing the Create button yields the following snap shot.

If the weight isn’t a valid integer the GUI will deny the submission. Otherwise, the submission works and a successful creation message is displayed.

Testing this three step scenario pragmatically isn’t as hard as it seems. First, a valid instance of the GUI must be created.

@BeforeMethod
private void init() {
 fixture = new AbbotFixture();
 fixture.showWindow(new WidgetFrame(),
    new Dimension(300, 200), true);
} 

Next, both text fields must be populated with valid data. This is easily achieved with testng-abbot through fixture helpers. In my case, I use two TextComponentFixture objects.

TextComponentFixture text1 =
   new TextComponentFixture(this.fixture, "widgetName");
text1.enterText("Test Name");

TextComponentFixture text2 =
   new TextComponentFixture(this.fixture, "widgetWeight");
text2.enterText("1245");

Lastly, the Create button must be hit, which is also done with the help of testng-abbot fixtures– in this case, the ButtonFixture object.

ButtonFixture bfix =
  new ButtonFixture(this.fixture, "widgetCreate");
bfix.click();

Now all that’s left is to verify that a successful creation message is displayed, which involves getting a handle the a JLabel object and verifying a particular String is present.

LabelFixture fix =
  new LabelFixture(this.fixture, "statusLabel");
fix.shouldHaveThisText("Widget weight isn't valid");

As you can infer, the shouldHaveThisText method acts as a logical assert and verifies if the particular text is present– if it’s not, then a failure event is triggered.

Putting it all together in with TestNG yields the following simple test case:

public class WidgetFrameTest {
 private AbbotFixture fixture;

 @BeforeMethod
 private void init() {
  fixture = new AbbotFixture();
  fixture.showWindow(new WidgetFrame(),
     new Dimension(300, 200), true);
 }

 @Test
 public void assertSuccess() {
  TextComponentFixture text1 =
    new TextComponentFixture(this.fixture, "widgetName");
  text1.enterText("Test Name");

  TextComponentFixture text2 =
    new TextComponentFixture(this.fixture, "widgetWeight");
  text2.enterText("XTT");

  ButtonFixture bfix = new ButtonFixture(this.fixture, "widgetCreate");
  bfix.click();

  LabelFixture fix = new LabelFixture(this.fixture, "statusLabel");
  fix.shouldHaveThisText("Widget successfully created");
 }      

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

Developer testing of GUIs is by no means a walk in the park; however, with frameworks like testng-abbot, the job is quickly becoming much much easier. What are you waiting for? Start testing those GUIs today!