Recording and Using Our Test

The first test that we will investigate is TestTable.xml, where we test a JTable and ensure that it is laid out the way that we want it and that we can click into the appropriate column and row.

 <component class="javax.swing.JMenuItem" index="6" parent="JPopupMenu Instance" text="Exit" window="SwingSet" />
 <component class="javax.swing.JMenu" index="0" parent="JMenuBar Instance" text="File" window="SwingSet" />
<component class="demo.TablePanel" id="TablePanel Instance" index="16" parent="JTabbedPane Instance" window="SwingSet" />

Again we see a listing of all the components that we deal with in our test.

 <wait args="SwingSet" class="abbot.tester.ComponentTester" method="assertComponentShowing" />
 <wait args="Frame Creator" class="abbot.tester.ComponentTester" invert="true" method="assertComponentShowing" />
 <action args="Frame Creator,270,250" class="javax.swing.JInternalFrame" method="actionResize" />
 <action args="Frame Creator,360,10" class="javax.swing.JInternalFrame" method="actionMove" />
 <action args="SwingSet,117,109" class="java.awt.Window" method="actionMove" />
 <action args="SwingSet,790,550" class="java.awt.Frame" method="actionResize" />
 <action args="JTabbedPane Instance,&quot;TableView&quot;" class="javax.swing.JTabbedPane" method="actionSelectTab" />
 <action args="JTable Instance,&quot;Mike&quot;" class="javax.swing.JTable" method="actionSelectCell" />
 <action args="Exit" method="actionSelectMenuItem" />
 <terminate />

And we see a listing of our sequence actions. Initially we fire up the SwingSet demo and wait for that window to show (assertComponentShowing). Then we wait for all of the windows, frames, and internal frames to show as well. Finally, we access the TableView tab, then click in the first table cell which contains the string “Mike”. Next we want to add some assertions to ensure that Abbot will capture what we want to test for. To add an assertion to a script, open the Costello script editor and the TestTable.xml script file, and move down to select the SelectCell action. Now we are ready to add our first assertion.

Java Click To expand

Adding assertions is a little odd in Costello, so let’s walk through it step-by-step. If we are dealing with an existing script that we just loaded, we need to load our app first. The easiest way to do that is through Test->Load. Then we need to determine what we want to add the assertion to—in this case it’s the JTable that we’ve been dealing with. Once our test script is running we go ahead and bring the SwingSet app in front. To add a particular assertion—verify the row size, for instance—we place our mouse over one of the cells, hold down the shift key, and press F1. Going back to Costello we should see the component hierarchy has changed a bit, and the JTable instance is selected. Going over to the right we see a listing of all the properties for that instance. Scroll down on the right until we see columnCount; selecting that gives us the option of adding an assertEquals. We go ahead and do that for both columnCount and rowCount, with the following results:

<assert component="JTable Instance" method="getColumnCount" value="6" />
<assert component="JTable Instance" method="getRowCount" value="32" />

Not too bad. Where we run into an issue is in checking other items like the actual cell value. What we might need to do in that case is to use the JTable or it’s model to gain access to the operator and then to the value contained therein. The final test that we will investigate is to test launching a dialog box using the menu system. This is actually quite easy in Costello.

 <wait args="SwingSet" class="abbot.tester.ComponentTester" method="assertComponentShowing" />
 <wait args="Frame Creator" class="abbot.tester.ComponentTester" invert="true" method="assertComponentShowing" />
 <action args="About" method="actionSelectMenuItem" />
 <wait args="About Swing!" class="abbot.tester.ComponentTester" method="assertComponentShowing" />
 <action args="OK" class="javax.swing.AbstractButton" method="actionClick" />
 <wait args="About Swing!" class="abbot.tester.ComponentTester" invert="true" method="assertComponentShowing" />
 <action args="Exit" method="actionSelectMenuItem" />

As you can see, all we need to do is actionSelectMenuItem and select the About menu item. Once we have selected that we wait for the dialog box to show up, and then click on the OK button to close the window. Finally we call the Exit menu item. Now we could get a little more complex and add in some assertions like

<assert component="OK" method="getLabel" value="OK" />

which basically says: check this component (which we have referenced above) and make sure that it’s label says OK.

Cleaning Up

As you record scripts you may notice items like this:

<action args="Frame Creator,270,250" class="javax.swing.JInternalFrame" method="actionResize" />

If it isn’t something that is needed as part of the test (i.e., it was part of the recording but not something you need to test), you can safely remove it. To delete unintended results, either use your favorite text editor to delete the line, or remove them from inside the Costello script editor by using Edit->Cut. You may also wish to move actions around inside of the sequence; to do this inside of Costello, highlight the line you wish to move, and then select edit->move up or down. This reorders the sequence of events that your script runs.

Putting it All Together

Now that we have everything cleaned up and have tested it inside of Costello to make sure it’s running, we need to create a test suite to execute it all. Unlike the other frameworks we do not need to actually start the Swing app as Abbot takes care of this for us. So that just leaves us to finding the XML scripts and running them. As before, this is very easy:

public static Test suite() {
 return new ScriptTestSuite(AbbotTest.class, "src/xptoolkit/swingTesters") {
 public boolean accept(File file) {
 String name = file.getName();
 return name.endsWith("xml");

Here we go ahead and go through our source directory and look for all of our XML script files; this of course could be a bit more robust depending on your needs. Once we have located all of the files, we pass them into the ScriptTestSuite for execution. Abbot then runs through each test and returns with either success or errors in a standard JUnit report.

Now that you have gotten a taste of what Costello does and how Abbot works with the scripts it creates, we hope you have enough information (and inclination) to pick up that dusty Swing app and see what you can test on it.