Diese Präsentation wurde erfolgreich gemeldet.
Wir verwenden Ihre LinkedIn Profilangaben und Informationen zu Ihren Aktivitäten, um Anzeigen zu personalisieren und Ihnen relevantere Inhalte anzuzeigen. Sie können Ihre Anzeigeneinstellungen jederzeit ändern.

Functional tests with the FEST framework

This article describes an easy and quick
way to write robust and compact tests for
Swing-rich applications using the open
source framework FEST.

  • Loggen Sie sich ein, um Kommentare anzuzeigen.

  • Gehören Sie zu den Ersten, denen das gefällt!

Functional tests with the FEST framework

  1. 1. ISSN 1866-5705 www.testingexperience.com free digital version print version 8,00 € printed in Germany 8 Standards – What about it? The Magazine for Professional Testers December, 2009© iStockphoto.com/belknap
  2. 2. © iStockphoto.com/aabejon Functional tests with the FEST framework by Dominik DaryThis article describes an easy and quick (e.g. FitNesse) is used for user acceptance tant for whom the application is designed.way to write robust and compact tests for tests in which the tests are written using If the application is developed for expertsSwing-rich applications using the open simple tables in html files. Each line with a lot of specialized views and withsource framework FEST. represents an interaction with the GUI a number of extended and self-developed through a command like “enter text”. The GUI components, a technical frameworkIn most projects a lot of time is used for testing columns are used for commands, con- is much more flexible.the application with unit tests and functional text, selector, arguments and comments.Graphical User Interface (GUI) tests, which • From a technical perspective, the usage GUI components are identified inside themakes the entire system safer and more robust. of a technical framework with a well- GUI-Dialog by their unique name. ThisMost of these functional tests are often done defined API offers the possibility to use simple approach can be used by testersmanually, which is very time consuming. code editors, in which the syntax is high- without IT background and adds a layer lighted and typing errors are displayed.Automated GUI tests can be used for verify- of abstraction onto writing automated These editors offer code completion,ing the main scenarios of the application. GUI tests. refactoring, debugging and integration oftesting is also essential during application • Technical Frameworks: These tests are various version control systems, so thatmaintenance; with automated tests, the impact written in traditional programming lan- the history of the test file can be comparedof refactorings can be tested within minutes. guages like Java or Ruby. Components to each version. Another advantage is theThe aspect of visualizing the GUI components are also identified by the name of the power of the programming language (e.g.is not the main intent of this test approach. component or more complex component all features of the Java language can beTest frameworks should provide a solid way lookups. The biggest disadvantage of this used) and the fact that existing utilityof finding GUI components, to get robust tests approach is the required basic know-how classes can be reused. The biggest disad-that are also able to run after layout changes. about software development and the time vantage is the necessary Java know-howAn intuitive and well-documented test frame- needed to learn the – in some cases cryp- of the testers.work is important for the readability of the tic – technical framework Application But what does a well-defined API mean? Ericwritten tests and allows the tester a more rapid Programming Interface (API). Evans and Martin Fowler invented the term oflearning. A very important criteria for selecting a test “fluent interface”, which is a design principleThose functional tests describe scenarios. In- framework is its maintainability and the dura- for an object-oriented API with the objectiveside a task manager application, a test scenario tion of the training period. Capture and replay to offer a more readable code that is only usedcan be e.g. the entering of new to do items. tools are at first glance an easy and fast way to within a limited scope (e.g. functional GUIThis process includes several steps, such as create test scripts. But these scripts are error- testing)1. A special characteristic of this designopening the new task form, entering some val- prone, especially if the layout of the applica- is the return value of a method call, which en-ues into the text fields and clicking the “cre- tion has changed. ables to build a method chain of a class.ate” button. Afterwards the created task must Keyword-driven testing enables domain ex- A sample code can look like this:be verified. This can be done using the GUI, perts to use this approach, because of thee.g. by loading the task form, or directly in the dialog.botton(“login”).click(); simplicity of the test case specification. Thesedatabase to assert the entered values are saved tests can be created with editors like Word, andcorrectly. In this code sample, the login button is resolved there’s a basic concept of modularization. Be- by its name and afterwards a click event is dis-The tools employed in GUI test automation low these frameworks, technical frameworks patched to it. This type of API is a so-calledcan be categorized as follows: like JFCUnit are used. fluent interface, which offers the developers• Capture and Replay: Testers click on If keyword-driven testing simplifies the usage the possibility to enhance an API to a domain- the test scenarios and thereafter the test of technical frameworks, why should a team specific language (DSL). A DSL is commonly framework captures all GUI interactions, decide to use a technical framework? described as a computer language targeted at a which are stored in a script that can be particular kind of problem and not intended to This question can be answered from a domain replayed automatically. view and a technical view:• Keyword-driven testing: This approach 1 http://www.martinfowler.com/bliki/Flu- • From a domain perspective, it is impor- entInterface.html82 The Magazine for Professional Testers www.testingexperience.com
  3. 3. solve problems outside of its domain.2 other modules can be Sample test code using the FEST API optionally used and //Fill in parameters FEST Framework are not described in window.textBox(“firstNumberInput”).enterText(“22”); Open Source Library for GUI Testing this article. window.textBox(“secondNumberInput”).enterText(“2”); • Supports functional Swing GUI test- (See Figure 1) ing //Select action • Website: http://fest.easytesting.org The Swing module is window.radioButton(“sumRadioButton”).check(); • Its API provides a fluent interface divided into different //Calculate • Open Source project (Apache 2.0 layers. “Basic ro- window.button(“calculateButton”).click(); license) bot” is the basic layer of the FEST-Swing //Validate the result • Supports both TestNG and JUnit window.textBox(“resultOutput”).requireText(“24”); module. The class • Simplifies troubleshooting GUI test java.awt.Robot , 6 failures finders that can be used. which is part of the Java Development Kit • Hosted at CodeHaus (JDK), is the main component of FEST-Swing. window.robot.finder(). This class is used to generate native system in- findByLabel(label, type)FEST is a Java-based framework for automated put events for the applications, for which inputfunctional GUI testing, which provides a flu- through the mouse and keyboard is needed. This finder will find a component by its refer-ent interface. FEST is an acronym and stands FEST has created an abstraction interface and enced label.for “Fixtures for Easy Software Testing”. Test the implementation delegates the calls to thecases are implemented as unit tests, and ap- After selecting a component, it is possible to robot class of the JDK.plications based on Swing and SwingFX can interact with this component. As shown in thebe tested. This framework is further developed The “component driver” layer contains example, the input of keyboards can be simu-actively and the javadoc coverage of the source driver classes for all Swing GUI components. lated, just as mouse click events can be sent tocode is high. It has an active community; ques- The driver knows in detail which types of in- the components.tions on the mailing list are answered very fast teraction are possible and how the state can beand efficiently. The existing wiki pages also checked. The JTextFieldDriver inter- The components used in the example are onlyinform about advanced topics. Based on the face, for example, contains methods like: simple GUI components. However, how easyfluent interface offered and the good javadoc is the selection of a special node of a JTree?descriptions, intuitive learning is possible. • enterText(..) Imagine a folder structure that is displayed in a tree:FEST is divided into four modules: • setText(…) • My Documents• Swing • requireText(…) ◦◦ FEST Simulation of user-generated events and • requireEditable(…) solid GUI component lookup ▪▪ Article The component “fixture layer” is located• Assertion3 on top of the driver layer and provides a DSL- ▪▪ Sample oriented API for the tester. The main differ- Flexible and fluent assertions ence between the driver and fixture layers is ◦◦ FitNesse the different style of programming. The driver The leaf node “Sample” can be selected using• Reflection4 layer is designed in a classic object-oriented the JTreeFixture: ‘Fluent interface’ for simplifying usage way and is used by the fixture layer. The fix- of reflection. ture layer is designed in a completely different JTreeFixture myDocument- way: every method returns the GUI compo- sTree = JTreeFixture(Robot,• Mock 5 nent fixture class itself to provide the ability of “folderTree”); Eliminates code duplication and clearly method chaining. This fluent interface makes myDocumentsTree. separates mock expectations from code the API much easier to write and read. selectPath(“My Documents/ to test, improving code readability FEST/Sample”); This small example enters a text into two textThe most important module for automated fields, selects a radio button and clicks on thefunctional testing is the Swing module. The calculation button. The last step is the valida- Another more complex GUI component is a JTable, which differentiates between the rendering and FEST Framework the editing of cells, e.g. a cell displays only a text, but when Swing Assertion Reflection Mock you double-click on the cell, a Component Fixture drop-down list is displayed. The following Java code Component Driver shows an interaction with the To-Do-List table of the demo Basic Robot application: These short examples dem- onstrate the elegance andFigure 1: Overview of the modules of the FEST framework the possibilities of the FEST tion of the calculation. Isn’t this code easy to framework testing Swing applications – the2 http://www.infoq.com/articles/internal- read and understand? new rich internet application technologydsls-java3 http://docs.codehaus.org/display/FEST/ By default, all GUI components can be identi- SwingFX is also testable. For readers who areFEST-Assert fied by name, as in the example, or by type. interested in more examples, a good starting4 http://docs.codehaus.org/display/FEST/ point is the sample application of this article However, there are also more sophisticatedFEST-Reflect with more FEST samples7.5 http://docs.codehaus.org/display/FEST/ 6 http://java.sun.com/javase/6/docs/FEST-Mocks api/java/awt/Robot.html 7 http://github.com/DominikDary/ToDo-www.testingexperience.com The Magazine for Professional Testers 83
  4. 4. Figure 2: Screenshot of the sample application //import static org.fest.swing.data.TableCell.row; Biography Dominik Dary is a Software Engineer //Find table GUI component working for Capgemini sd&m AG in JTableFixture taskTable = window. Germany. Dominik has 5 years practical table(“toDoListTable”); experience in specification and realiza- tion of object-oriented software in Java //Selecting the finish cell / Spring / JEE as well as in quality as- JTableCellFixture finishedCell = taskTable. surance and in business intelligence. In cell(row(0).column(2)); 2005 he wrote his diploma thesis about “Quality Assurance in JEE Projects by // Selecting the checkbox Test-Driven Development”. The practi- finishedCell.enterValue(“true”); cal part of this thesis is based on the finishedCell.background().requireEqualTo(Color. “Cactus” and the “Canoo webtest” GREEN); frameworks. //Selecting the project cell Dominik’s first contact with FEST was in JTableCellFixture projectCell = taskTable. 2008 developing a Java Swing document cell(row(0).column(3)); management / workflow application. His further activities in this project included // Select a value in the comboBox test specification and organization of projectCell.enterValue(“Spring DM”); acceptance tests. // Assert the selected value is displayed afterwards In his current project, Dominik works projectCell.requireValue(“Spring DM”); for an e-commerce company doing test management, performance testing and automated testing with the selenium FEST tests are written as unit tests; both popular frameworks - JUnit and TestNG framework. – are supported. If TestNG is used as unit test framework, this offers the possibil- ity to create your own reports. During testing the application, screenshots can be taken using the ScreenshotTaker manually or automatically if the test fails. Those functional tests can be integrated8 into continuous integration environments like Hudson9 or TeamCity10. For self-developed GUI components, it is helpful to create driver and fixture classes as they exist for the Swing components. Another helpful approach is to create fix- ture classes for GUI dialogs to offer methods for easy component selection and some simple flows, e.g. choosing a file with JFileChooser which can be centralized. This helps to minimize code duplication, simplifies the tests and increases once more the readability of the tests. Newly designed and developed Swing desktop applications are much easier to test if code conventions contain a naming concept to guarantee that each GUI component has a unique name. Tests of complex desktop applications need a flexible, well-documented framework to create maintainable tests; check out the sample application and get started writ- ing functional tests with FEST! List 8 http://docs.codehaus.org/display/FEST/Continuous+Integration 9 https://hudson.dev.java.net/ 10 http://www.jetbrains.com/teamcity/ 84 The Magazine for Professional Testers www.testingexperience.com