Automated UI Tests with Hudson on Debian

At DFKI we are using Java, Maven2 and Hudson for a Continuous Integration System within the research project MEDICO .

Until now, this works really fine for console based JUnit tests. However, the test coverage of our source code is far from 100% (ashes on me!). This is why I decided to test from two “directions”:

  1. Bottom-up using JUnit tests for simple methods. These tests are easy to write and to maintain. In theory, the whole application works correctly if all methods work correctly. Thus, this requires that you have 100% test coverage for your code. This condition is far from being met by our code base.
  2. To come up with this, we decided to add Top-down tests. Using the Java UI test framework FEST we specified test scripts which perform certain operations on the application level. Each of these scripts define the expected behavior of the UI and the data presented on the screen. Examples are below.

It took me several hours to set this up. The following steps assume that you have already installed Debian Linux, Maven2, Hudson as described here.

Here are the necessary steps:

1. Install Xorg

On a Debian system, you just have to do an

apt-get install xorg

2. Configure Xorg

Since we do not care about any special support for the graphics card (in fact, I run all these services on VMWare instances) you can just use automatic configuration routines from Xorg to find a minimal server layout. Just type

Xorg -configure

This will create a “xorg.conf.something” in the current working directory. To use it, you just have to copy it to “/etc/X11/xorg.conf”.

3. Install an appropriate window manager

I also tried running Xorg completely without any window managers and with tinywm. This is certainly not a good idea since some of my UI tests require the program windows to run maximized. But window maximization seems to require certain functions from the window manager. With the latter two window managers, I got this error message:

“Platform does not support maximizing frames”

This problem was solved by installing ICEWM. On Debian, all you have to do is:

apt-get install icewm

No further configuration needed.

4. Make Xorg start before Tomcat

For this purpose I created a “/etc/init.d/xorg” file with the following content:

case $1 in
start)
startx &
sleep 15
export DISPLAY=:0.0
xhost localhost
;;
stop)
killall -9 xinit
;;
restart)
killall -9 xinit
sleep 5
startx &
sleep 5
xhost localhost
export DISPLAY=:0.0
;;
esac
exit 0

Link this file to /etc/rc2.d/S98xorg to have it started before tomcat. As you can see above, this also activates access to the Xserver for all applications running on the local server. This only makes sense for dedicated servers without any user accounts that could interfere with the UI tests.

5. Export DISPLAY variable to the Tomcat init file

Add this line to /etc/init.d/tomcat

export DISPLAY=:0.0

and make sure that you have no “java.awt.headless=true” parameters in any of the Tomcat or Catalina startup scripts.

6. Define a sample application

Somehow a “Hello World”, here is a very simple sample application:

public class FrameSample {

public static JFrame frame;

public static void main(String[] args) {
frame = new JFrame(“TestFrame”);
frame.setName(“TestFrame”);

JButton button = new JButton(“TestButton”);
button.setName(“TestButton”);

frame.add(button);
frame.pack();
frame.setVisible(true);
}
}

7. Define a UI test for it

A really minimalistic example, that only checks for the presence of the “TestButton” is shown below:

public class FrameSampleTest {

private FrameFixture window;

@Before
public void setUp() {
dfki.km.medico.uisample.FrameSample.main(new String[] {});
window = new FrameFixture(FrameSample.frame);
window.show(); // shows the frame to test

}

@Test
public void testButtonExists() {
window.maximize();
window.button(“TestButton”).requireEnabled();
}
}

Documentation about more complex tests can be found in the FEST wiki.

Leave a Reply