Posts‎ > ‎

Two simple tools to let you test your Swing/JavaFX applications easily

posted Oct 5, 2013, 10:02 AM by Renato Athaydes   [ updated Oct 6, 2013, 8:28 AM ]
Writing Swing applications is not easy. Testing them can be nearly as hard! Looking at what tools are available to test Swing apps, it really seems that even after all these years there's no good, simple, standard tool you can use if all you need to do is drive some clicks around and make sure your screens react in a more or less acceptable manner.

I help develop LoadUI during my working hours, a load testing tool written on JavaFX. JavaFX, being a fairly new technology, also suffers from the same problem as Swing, so it was hard for us, at least in the beginning, to write good automated tests for LoadUI. We (actually they, I was not part of the team yet) decided to write our own tool to do that, which apparently has been the most common route taken also by most people testing Swing apps.

Enter TestFX

Well, out of that effort, TestFX was born. TestFX allows you to write tests that can be easily read, or even written, by non-programmers. It was just recently split from the main LoadUI source base by my colleague and LoadUI Product Owner, Henrik Olsson... he even participated in a few conferences, including JavaOne 2013, where he spoke about the LoadUI team experience with JavaFX and demoed TestFX for the first time. There was quite a lot of interest in TestFX... it was mentioned in a few other talks in JavaOne and people seem to have liked the idea behind it.

Example TestFX code:
rightClick( "#desktop" ).move( "New" ).click( "Text Document" ).
                             type( "myTextfile.txt" ).push( ENTER );
assertThat( "#desktop .file", hasLabel( "myTextFile.txt" ) );

However, TestFX can currently test only JavaFX applications. We have done a little research and there seems to be no good open-source tool that does the same kind of thing for Swing.
Our colleagues a few meters across the floor who develop SoapUI (a large tool for functionally testing online APIs such as SOAP and REST) seem to have been facing the same difficulties in their struggles with Swing for some years. They showed some interest in having a tool like TestFX to avoid using some heavy-weight tool such as FEST. There is a possibility that SoapUI might start using some JavaFX features in the future, and one of the concerns is that there are very few tools (none open-source as far as I know) that support both Swing and JavaFX.

... and The Automaton

At one lunch-time discussion, I decided to mention that I had actually written just such tool in my own spare time in an effort to write some tests for a GUI I was developing at the time (and to be honest, out of curiosity to know how to drive GUI tests in the JVM). Of course, my design was based on the LoadUI testing framework which I was already familiar with... but from the start, I wrote it to support both Swing and JavaFX.

I did not really expect that anybody else would be interested in something like that... but as often happens, I was wrong! And that's why I decided to put some effort into making my little framework, which I called Automaton, more usable, and perhaps help some people in the far corners of the IT world who perhaps need a hand testing their GUIs.

Example Automaton code:
SwingerFxer.userWith( frame, fxPanel.getScene().getRoot() )
           .clickOn( "text-area" )
           .type( swingTextAreaText ).pause( 1000 )
           .clickOn( "#left-color-picker" )
           .pause( 1000 ).moveBy( 60, 40 ).click()
           .pause( 1000 ).clickOn( "#fx-input" )
           .type( fxInputText ).moveBy( 100, 0 )
           .pause( 500 );

JTextArea jTextArea = ( JTextArea ) lookup( "text-area", frame );
TextField textField = ( TextField ) fxPanel.getScene().lookup( "#fx-input" );

assertEquals( swingTextAreaText, jTextArea.getText() );
assertEquals( fxInputText, textField.getText() );
assertEquals( "0x999999ff", swingJavaFx.getTextLeftColor().toString() );

I wrote the Automaton in Groovy as I firmly believe Groovy is the best language for writing automated tests, even for testing most Java code (GUIs being probably the most compelling case). But it is completely painless to use it with pure-Java applications. I even added a sample Java test and wrote the examples in the project front-page in Java so that die-hard Java people don't get too scared. 

So please check out both our pages on GitHub and see if there's something there for you.