junit-testing gui components [autosaved]shsaad/seng426/resources/lab... · 2011-06-15 · design...
TRANSCRIPT
![Page 1: JUnit-Testing GUI Components [Autosaved]shsaad/seng426/resources/Lab... · 2011-06-15 · Design Test Cases with JUint For each GUI component define a new test method in MainFrameTest.java](https://reader033.vdocument.in/reader033/viewer/2022050313/5f756d66d5cd9b07ee618196/html5/thumbnails/1.jpg)
JUnit-Testing GUI Components
![Page 2: JUnit-Testing GUI Components [Autosaved]shsaad/seng426/resources/Lab... · 2011-06-15 · Design Test Cases with JUint For each GUI component define a new test method in MainFrameTest.java](https://reader033.vdocument.in/reader033/viewer/2022050313/5f756d66d5cd9b07ee618196/html5/thumbnails/2.jpg)
Agenda
Test GUI ComponentsSimple GUI ApplicationTest Cases Design.Test Cases Implementation with JUnit.Test Cases Execution.
Project Part 3
![Page 3: JUnit-Testing GUI Components [Autosaved]shsaad/seng426/resources/Lab... · 2011-06-15 · Design Test Cases with JUint For each GUI component define a new test method in MainFrameTest.java](https://reader033.vdocument.in/reader033/viewer/2022050313/5f756d66d5cd9b07ee618196/html5/thumbnails/3.jpg)
Simple GUI Application
In our application we will cover testing different GUI components, such as
JTextFieldJButtonJDialog.JMenu.
![Page 4: JUnit-Testing GUI Components [Autosaved]shsaad/seng426/resources/Lab... · 2011-06-15 · Design Test Cases with JUint For each GUI component define a new test method in MainFrameTest.java](https://reader033.vdocument.in/reader033/viewer/2022050313/5f756d66d5cd9b07ee618196/html5/thumbnails/4.jpg)
Application Overview
The application consist of one java class, named MainFrame.javaThe application has a text field. When a string is typed, it adds ? to the end.When the show button is clicked, a dialogbox displays the text + (...It works!).The application also has a menu forchanging the text color.
![Page 5: JUnit-Testing GUI Components [Autosaved]shsaad/seng426/resources/Lab... · 2011-06-15 · Design Test Cases with JUint For each GUI component define a new test method in MainFrameTest.java](https://reader033.vdocument.in/reader033/viewer/2022050313/5f756d66d5cd9b07ee618196/html5/thumbnails/5.jpg)
Application GUI
![Page 6: JUnit-Testing GUI Components [Autosaved]shsaad/seng426/resources/Lab... · 2011-06-15 · Design Test Cases with JUint For each GUI component define a new test method in MainFrameTest.java](https://reader033.vdocument.in/reader033/viewer/2022050313/5f756d66d5cd9b07ee618196/html5/thumbnails/6.jpg)
MainFrame.java
![Page 7: JUnit-Testing GUI Components [Autosaved]shsaad/seng426/resources/Lab... · 2011-06-15 · Design Test Cases with JUint For each GUI component define a new test method in MainFrameTest.java](https://reader033.vdocument.in/reader033/viewer/2022050313/5f756d66d5cd9b07ee618196/html5/thumbnails/7.jpg)
GUI Functions
![Page 8: JUnit-Testing GUI Components [Autosaved]shsaad/seng426/resources/Lab... · 2011-06-15 · Design Test Cases with JUint For each GUI component define a new test method in MainFrameTest.java](https://reader033.vdocument.in/reader033/viewer/2022050313/5f756d66d5cd9b07ee618196/html5/thumbnails/8.jpg)
Prepare for Testing
Our Test Cases should cover:JTextField.JDialog BoxJButton.JMenu.
Create test class for MainFrame using JUnit plugin in Netbeans.
![Page 9: JUnit-Testing GUI Components [Autosaved]shsaad/seng426/resources/Lab... · 2011-06-15 · Design Test Cases with JUint For each GUI component define a new test method in MainFrameTest.java](https://reader033.vdocument.in/reader033/viewer/2022050313/5f756d66d5cd9b07ee618196/html5/thumbnails/9.jpg)
MainFrameTest.java
![Page 10: JUnit-Testing GUI Components [Autosaved]shsaad/seng426/resources/Lab... · 2011-06-15 · Design Test Cases with JUint For each GUI component define a new test method in MainFrameTest.java](https://reader033.vdocument.in/reader033/viewer/2022050313/5f756d66d5cd9b07ee618196/html5/thumbnails/10.jpg)
Problems
JUnit can not generate test functions to GUI components.GUI functions are private so there is no direct path to access them.
![Page 11: JUnit-Testing GUI Components [Autosaved]shsaad/seng426/resources/Lab... · 2011-06-15 · Design Test Cases with JUint For each GUI component define a new test method in MainFrameTest.java](https://reader033.vdocument.in/reader033/viewer/2022050313/5f756d66d5cd9b07ee618196/html5/thumbnails/11.jpg)
Solution?
There are many ways to access Swingcomponents:
1. Application code has getXxx() methods toreturn each component of interest.
2. Test code invokes events on a screen,mimicking a human operator. Events aretypically mouse moves/clicks and key typing.
3. Test code traverses the component tree andfinds a component of a specific signature(class, location, order, text contents, etc.).
![Page 12: JUnit-Testing GUI Components [Autosaved]shsaad/seng426/resources/Lab... · 2011-06-15 · Design Test Cases with JUint For each GUI component define a new test method in MainFrameTest.java](https://reader033.vdocument.in/reader033/viewer/2022050313/5f756d66d5cd9b07ee618196/html5/thumbnails/12.jpg)
Traverses GUI
To allows the test code to traversesthe GUI component tree.
Name each component that your testcode will request access to it usingsetName() method.Write the appropriated code to traversethe GUI components and provide anaccess to these components
![Page 13: JUnit-Testing GUI Components [Autosaved]shsaad/seng426/resources/Lab... · 2011-06-15 · Design Test Cases with JUint For each GUI component define a new test method in MainFrameTest.java](https://reader033.vdocument.in/reader033/viewer/2022050313/5f756d66d5cd9b07ee618196/html5/thumbnails/13.jpg)
Naming the GUI Components
We need to add the following function to the MainFrame.java classWe call this function from the class construction.
![Page 14: JUnit-Testing GUI Components [Autosaved]shsaad/seng426/resources/Lab... · 2011-06-15 · Design Test Cases with JUint For each GUI component define a new test method in MainFrameTest.java](https://reader033.vdocument.in/reader033/viewer/2022050313/5f756d66d5cd9b07ee618196/html5/thumbnails/14.jpg)
Create Traverse Class
Component traversal code isencapsulated into a utility class,TestUtils.The TestUtils class contains thefollowing static methods:
getChildNamed()getChildIndexed()getChildIndexedInternal()
![Page 15: JUnit-Testing GUI Components [Autosaved]shsaad/seng426/resources/Lab... · 2011-06-15 · Design Test Cases with JUint For each GUI component define a new test method in MainFrameTest.java](https://reader033.vdocument.in/reader033/viewer/2022050313/5f756d66d5cd9b07ee618196/html5/thumbnails/15.jpg)
TestUtils.java
![Page 16: JUnit-Testing GUI Components [Autosaved]shsaad/seng426/resources/Lab... · 2011-06-15 · Design Test Cases with JUint For each GUI component define a new test method in MainFrameTest.java](https://reader033.vdocument.in/reader033/viewer/2022050313/5f756d66d5cd9b07ee618196/html5/thumbnails/16.jpg)
getChildNamed ( )
![Page 17: JUnit-Testing GUI Components [Autosaved]shsaad/seng426/resources/Lab... · 2011-06-15 · Design Test Cases with JUint For each GUI component define a new test method in MainFrameTest.java](https://reader033.vdocument.in/reader033/viewer/2022050313/5f756d66d5cd9b07ee618196/html5/thumbnails/17.jpg)
getChildIndexed()
![Page 18: JUnit-Testing GUI Components [Autosaved]shsaad/seng426/resources/Lab... · 2011-06-15 · Design Test Cases with JUint For each GUI component define a new test method in MainFrameTest.java](https://reader033.vdocument.in/reader033/viewer/2022050313/5f756d66d5cd9b07ee618196/html5/thumbnails/18.jpg)
getChildIndexedInternal()
![Page 19: JUnit-Testing GUI Components [Autosaved]shsaad/seng426/resources/Lab... · 2011-06-15 · Design Test Cases with JUint For each GUI component define a new test method in MainFrameTest.java](https://reader033.vdocument.in/reader033/viewer/2022050313/5f756d66d5cd9b07ee618196/html5/thumbnails/19.jpg)
Design Test Cases with JUint
For each GUI component define a new test method in MainFrameTest.java class with the following signature:
public void testYourGUICompnentName()Define appropriate variables to implement your test scenario.Use the TestUtils class to obtains access to the GUI components Use reference to control your GUI components.Use you GUI component reference and swings/awt APIs to change your GUI component behaviors.Perform action to trigger the action listener of your component using postActionEvent() method.
![Page 20: JUnit-Testing GUI Components [Autosaved]shsaad/seng426/resources/Lab... · 2011-06-15 · Design Test Cases with JUint For each GUI component define a new test method in MainFrameTest.java](https://reader033.vdocument.in/reader033/viewer/2022050313/5f756d66d5cd9b07ee618196/html5/thumbnails/20.jpg)
Testing the JTextFiled
Define your test method
Define variables for your test Cases
![Page 21: JUnit-Testing GUI Components [Autosaved]shsaad/seng426/resources/Lab... · 2011-06-15 · Design Test Cases with JUint For each GUI component define a new test method in MainFrameTest.java](https://reader033.vdocument.in/reader033/viewer/2022050313/5f756d66d5cd9b07ee618196/html5/thumbnails/21.jpg)
Testing the JTextField continue1
Begin your testing scenario
Use the traversal code to access the GUI component
![Page 22: JUnit-Testing GUI Components [Autosaved]shsaad/seng426/resources/Lab... · 2011-06-15 · Design Test Cases with JUint For each GUI component define a new test method in MainFrameTest.java](https://reader033.vdocument.in/reader033/viewer/2022050313/5f756d66d5cd9b07ee618196/html5/thumbnails/22.jpg)
Testing the JTextField continue2
Use the reference of the GUIcomponent to modify it.
This method for automation purpose only you don’t have to use it
Post an action to the GUI component
This method for automation purpose only you don’t have to use it
![Page 23: JUnit-Testing GUI Components [Autosaved]shsaad/seng426/resources/Lab... · 2011-06-15 · Design Test Cases with JUint For each GUI component define a new test method in MainFrameTest.java](https://reader033.vdocument.in/reader033/viewer/2022050313/5f756d66d5cd9b07ee618196/html5/thumbnails/23.jpg)
Testing the JTextField continue3
Verify your test case
![Page 24: JUnit-Testing GUI Components [Autosaved]shsaad/seng426/resources/Lab... · 2011-06-15 · Design Test Cases with JUint For each GUI component define a new test method in MainFrameTest.java](https://reader033.vdocument.in/reader033/viewer/2022050313/5f756d66d5cd9b07ee618196/html5/thumbnails/24.jpg)
testInputJTextField()
![Page 25: JUnit-Testing GUI Components [Autosaved]shsaad/seng426/resources/Lab... · 2011-06-15 · Design Test Cases with JUint For each GUI component define a new test method in MainFrameTest.java](https://reader033.vdocument.in/reader033/viewer/2022050313/5f756d66d5cd9b07ee618196/html5/thumbnails/25.jpg)
Execute the Test Cases
You can execute your test cases by:1. Right click on test Suite class and select Run
File.2. Right click on the test class and select Run File.3. Right click on the Project Name and select Test.4. Press Alt+F6 or Shift+F6
![Page 26: JUnit-Testing GUI Components [Autosaved]shsaad/seng426/resources/Lab... · 2011-06-15 · Design Test Cases with JUint For each GUI component define a new test method in MainFrameTest.java](https://reader033.vdocument.in/reader033/viewer/2022050313/5f756d66d5cd9b07ee618196/html5/thumbnails/26.jpg)
Project Part 3:Test Preparation
RequirementsTest Plan.Test Design.Test Implementation.