GUI Testing Pizza Order Intl with UISpec4J

GUI Testing Pizza Order Intl with UISpec4J Overview The pizza order international application lets a user select a locale from a combo box. Based on t...
Author: Conrad Roberts
42 downloads 0 Views 143KB Size
GUI Testing Pizza Order Intl with UISpec4J Overview The pizza order international application lets a user select a locale from a combo box. Based on the locale selected, the toppings (Onions, Mushroom, Peppers, Olives, Tomatoes, and Pepperoni) check box labels change to the corresponding language. In addition, the submit button label changes as well.

When it comes to testing any application that requires interactions through a graphical user interface, manual testing is tedious and time consuming. The human tester is required to carry out the testing activities on the UI itself. Moving the mouse, filling in forms, etc. is slow and error prone. Regression testing in this fashion is impractical and often ignored. The solution to such a problem is using an automated GUI testing framework. One such testing framework for Java Swing applications is UISpec4J.

Setup This tutorial assumes you have completed the Subversion Tutorial and have created a NetBeans project for the Pizza Order International application. Launch NetBeans and open the Pizza Order project. Download the UISpec4J jar file (for Java 1.7).

In NetBeans, right-click on PizzaOrderIntl and choose Properties. Selected the "Libraries" category, and click "Add JAR/Folder". Navigate to where you downloaded the jar file and select it. Click OK.

Creating a Test Case Right-click on the PizzaOrderIntl node in the project window and choose "New" and "JUnit Test". In the "Class Name:" field, enter "PizzaOrderIntlGUITest" In the "Package:" field, enter "pizzaorderintl". Click Finish. A skeleton test will appear in the edit window. In the edit window change "extends TestCase" to "extends UISpecTestCase". Add two import statements: import org.uispec4j.*; import org.uispec4j.interception.*;

Testing: Ordering a pizza in English Locale The UISpec4J framework has classes which mimic Swing components. For example, the Window class is a wrapper around JFrame, JDialog, and JInternalFrame. In addition, the Window can be used to get UI components such as menu bars, buttons, or combo boxes.

Create a new method, testPizzaGUI. public void testPizzaGUI() {         // Create an instance of the class to be tested.    GUI view = new GUI();                 // Get the main window of the running PizzaIntlApp         Window window = new Window(view);

Obtaining Combo Box Component(s) From the window object, one can examine components. In order to get the ComboBox for the locale, one should make the following call. After obtaining the combo box, the following test case will select the English locale.         ComboBox langComboBox = window.getComboBox();         //check to see if ComboBox contains "en / English"         assertTrue(langComboBox.contains("en / English"));         //select "en / English"         langComboBox.select("en / English"); Now let’s create a list of toppings that we want on our pizza and then check the corresponding check boxes in the UI. To select a check box, one must first get a check box object from the window. This is done by calling getCheckBox with the name of the label displayed by the check box. Then, to select the box, one must call select on the CheckBox object. In the following lines of code, we will check onions, peppers, and tomatoes.         final List toppings = Arrays.asList("Onions",  "Olives","Peppers", "Tomatoes");

        //FOR each topping in toppings         for (String topping : toppings)         {             window.getCheckBox(topping).select();         } In the following like of code we will assert if the toppings we like on our pizza are selected. This check is done by calling isSelected method on the check box         //FOR each topping in toppings         for (String topping : toppings)         {             assertTrue(window.getCheckBox(topping).isSelected());         }

        final List notSelectedToppings =  Arrays.asList("Mushrooms",                 "Pepperoni"); The following line of code will check if the toppings that we didn’t like on our pizza are not selected. //FOR each non­selected topping          for (String topping : notSelectedToppings)         {             //verify the non­selected toppings are not checked             assertFalse(window.getCheckBox(topping).isSelected());         } Obtaining Button Component(s) Since we know that the UI only contains one button, we can call getButton method on the window to return us a button object or we can pass in the name of the button in the window we would like to get.         //get the submit button         Button submit = window.getButton("Submit"); Handling Popup windows or dialogs Once the Submit button is clicked, we expect a message window to appear confirming the toppings we ordered. We would like to have our test verify that the proper message text was displayed, then click the OK button to close the window. If we are expecting only static text to appear in the dialog, we can take advantage of the framework's "BasicHandler".  // This is a shortcut to test static messages  String message = "You ordered a pizza";  WindowInterceptor.init(submit.triggerClick()).process(BasicHandler.init().  assertContainsText(message).triggerButtonClick("OK")).run();

This doesn't check to see if the proper toppings were listed. To accomplish thise we have to build our own WindowInterceptor object. First, init() is used to initialize the interceptor with a Trigger. A Trigger is a piece of action or code that causes the popup window to be shown. In this case, clicking on the “Submit” button shows a dialog window listing the toppings we selected on our pizza. Second, when the trigger event occur a process method gets invoked. This method is given a WindowHandler interface that represents the window that was shown. Inside this method, a user might want to check the components included in the pop up window, the title of the window, and finally closing the window using the OK button. Finally, in order to run the interception, run method needs to be called on the WindowInterceptor object. In the following code we will trigger the submit button. Then in the pizza order app process method, we will check if the window contains the “You ordered a pizza with these toppings:" message and the list of toppings we selected.     //get the popped up window         WindowInterceptor orderPopup = WindowInterceptor.init(submit.                 triggerClick());

        //create process handler         orderPopup.process(new WindowHandler()         {             @Override             public Trigger process(Window window) throws Exception             {                 //SET message to toppings order message                 String message = "You ordered a pizza with these toppings:";                 //check if the window contains the message                 assertTrue(window.containsLabel(message));

                //Test each dynamically selected topping                 for (String selectedTopping : toppings)                 {                     assertTrue(window.containsLabel(selectedTopping));                 }

                //trigger click the OK button to close the window                 return window.getButton("OK").triggerClick();             }         });         orderPopup.run();

Running the Test Case Expand the Test Packages node in the project window. Right-click on the PizzaOrderIntlAppTest.java file and choose "Test File". NetBeans will execute the test and display the results in a window. You should get a green bar and a "test passed" message. Download complete source: http://users.csc.calpoly.edu/~jdalbey/309/Assign/PizzaOrderIntlGUITest.java

Document History Revised/simplified by eliminating unnecessary adapter J. Dalbey Feb 2016 minor edits

J. Dalbey

Dec 2011

Version 1 Gagandeep Singh Dec 2011