Testing GUI applications with TestNG
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!

January 28th, 2007 at 8:29 pm
Thanks a lot for mentioning TestNG-Abbot!
The project originally started as a bridge between TestNG and Abbot…lately we have been “exploring” ways to make tests more “readable” (and easier to maintain) with the use of chained methods/fluent interfaces
I know our documentation sucks…actually, we don’t have any!
We will be working on that pretty soon…
Although it is not real documentation, I have written some entry blogs about the project ( http://www.jroller.com/page/alexRuiz ).
Cheers,
Alex
January 30th, 2007 at 6:59 am
[…] Go… […]
February 11th, 2007 at 9:50 pm
[…] Become a GUI testing cleric with TestNG-Abbot in 4 steps As a deacon of developer testing, I found my programming pilgrimage into the realm of TestNG-Abbot quite hip (and you thought I’d use another ecclesiastical word– this is the disco blog, man!). As the current documentation is rather sparse, here are four easy steps to TestNG-Abbot bliss-ness: […]
February 28th, 2007 at 6:06 am
There is also a good article on this topic on the IBM developers site: http://www-128.ibm.com/developerworks/java/library/j-cq02277/index.html?ca=drs-