Stay Connected

HP Virtualization Journal

Subscribe to HP Virtualization Journal: eMailAlertsEmail Alerts newslettersWeekly Newsletters
Get HP Virtualization Journal: homepageHomepage mobileMobile rssRSS facebookFacebook twitterTwitter linkedinLinkedIn


HP Virtualization Authors: Dana Gardner, Greg Schulz, Yeshim Deniz, Cloud Best Practices Network, Wayne Ariola

Related Topics: RIA Developer's Journal, Java EE Journal, JavaBlackBelt, Sun Developer Journal, HP Virtualization Journal, Java Developer Magazine

Article

An Introduction to Abbot

A Friendly JUnit Extension for GUI Testing

Scenario Two: GUI Is Ready to Be Coded
In this scenario, the GUI tasks have been identified and the specification is ready to be coded. A good way to begin coding is to write the tests that need to be passed before the GUI is created. The XML-based scripts can be used to create different test cases; however, to demonstrate the full power of Abbot, this section will discuss how the JUnit test cases can be written for the GUI.

  • The Abbot Java API will be used to write traditional JUnit test cases. The salient features of the API that help in creating test cases are:

  • Standard JUnit test case structure: Every test case is a subclass of junit.framework.TestCase. The test case has a setUp() and tearDown(), and the tests are structurally similar to JUnit.
  • Provides the ability to simulate user events: Swing GUI testing is different from testing standard Java classes. For standard Java classes, the JUnit framework can run in a separate thread and invoke the methods on the Java classes. However, JUnit doesn't provide a framework that meets the testing needs of Swing applications. For Swing applications, the tests need to wait for the GUI to run and then execute the different user-generated events. Abbot provides the ComponentTester class. This allows the test case to invoke the GUI and generate user events on the AWT thread of the application (simulating a user interaction), without having to explicitly perform any synchronization. The flow of the test code is a simple series of user actions and GUI state checks.
  • Provides component-specific testers: Provides classes to simulate the various events that are specific to different Swing components. For example, JMenuItemTester would enable the simulation of various user-generated events within the test for each of the JMenuItems in the GUI. These classes are available in the abbot.tester package.

    The Celsius-to-Fahrenheit converter example presented in the previous section is used to walk through the steps involved in creating a test case using the Abbot Java API. In this test case, the input of a negative number to the Celsius input will be tested.

    Like JUnit test cases, the CelsiusConverterTest class will subclass the junit.Framework.TestCase class and the constructor will initialize the name of the test.

    public class CelsiusConverterTest
    extends TestCase {
    public CelsiusConverterTest
    (String name) { super(name); }
    }

    The setUp() and tearDown() methods can be used to initialize the frame in which the GUI will run. The setUp() method may be used to populate any arrays or other defaults that are required by the GUI to be fully functional.

    private ComponentTester tester;
    protected void setUp() {
    tester =
    ComponentTester.getTester
    (CelsiusConverter.class);
    // wait for the window to be displayed
    tester.waitForFrameShowing
    ("Convert Celsius to Fahrenheit")
    }

    To write the test, we create a method called testNegativeNumberInput(). This test method needs to get a reference to each of the GUI components that are being tested. One way Abbot gets a reference to a GUI component is by looking up a component of the appropriate class. This allows Abbot to continue to refer to different GUI components even if the layout changes.

    ComponentReference ref =
    new ComponentReference
    ("tempCelsius", JTextField.class);
    Component tempCelsius =
    getFinder().findComponent(ref);

    The next step is to initialize the ComponentTester so that the user interaction event of typing in a negative number can be generated

    tester = ComponentTester.
    getTester(tempCelsius);
    tester.actionKeyString(-45);

    Once the sequence of user interactions is in place, we would need to verify if the result is correct by adding assertions. The reference to the label is obtained and then the value of the label is tested using assetEquals()

    JLabel fahrenheitLabel =
    (JLabel)getFinder().findComponent(ref);
    assertEquals("-49 Fahrenheit",
    "-49 Fahrenheit",
    fahrenheitLabel.getText());

    To run the test, the TestHelper class available in the test package in Abbot will be handy. The TestHelper class also provides automatic test suite generation functionality.

    public static void main(String[] args){
    TestHelper.runTests
    (args,CelsiusConverterTest.class);
    }

    This method can be used in conjunction with JUnit test cases for the application logic to create a homogeneous test suite. The decision points in the application logic can be tested using normal JUnit tests. Test cases generated using Abbot will test for different user interaction scenarios.

    Conclusion
    Writing functional GUI tests can be a tedious task. The concepts from both of the testing scenarios mentioned can be combined to produce a comprehensive suite of tests. Abbot as a framework provides both scripting functionality and a Java API. The framework easily integrates with the JUnit test harness and therefore, during application development, the functional GUI tests can become a part of the test suite. All these features of Abbot make it an effective framework for rapidly creating a comprehensive test framework.

    Resources

  • Abbot: http://abbot.sourceforge.net/
  • JUnit: www.junit.org
  • Abbot Java API: http://abbot.sourceforge.net/doc/api/index.html
  • Java Swing Tutorial: http://java.sun.com/docs/books/tutorial/uiswing
  • More Stories By Satadip Dutta

    Satadip Dutta is a software architect at Hewlett-Packard (http://devresource.hp.com) and has been programming in Java since 1997. His areas of interest include distributed software architecture, Web services, and user interface design. Satadip holds an MS in Computer Science from Virginia Tech.

    Comments (7)

    Share your thoughts on this story.

    Add your comment
    You must be signed in to add a comment. Sign-in | Register

    In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.