Testing swing/jclient applications with Marathon
Marathon is a
very nice tool to test swing applications. You can write test scripts
using python, or you can have Marathon record your actions. The result
of this is a python script which looks like this:
useFixture(default)
def test():
window('My Application')
click('JTree', '/Someone/Contact Data')
select('sexComboBox', 'F')
select('weightFormattedTextField', '90')
assertText('statusLabel','')
select('dateOfBirthFormattedTextField', '01.01.1900')
close()
You specify which components you’re using by refering to the
component name, which you can set using setName() on every component.
In jdeveloper you’d expect that if you specify name in the property
sheet of a component that jdeveloper would generate a setName call in
your code. This is not the case however. Instead it changes the
instance name of the component, which isn’t bad either, but not very
helpfull if you’re creating marathon scripts. You’re test scripts will
be a lot less readable. In the example above, i didn’t manually call
setName for the JTree component. If i’d had 3 JTree components they
would have referred to as JTree, JTree1, JTree2.
I’ve created a small utility which will set the name of a component and it’s child components by taking the instance name and calling setName on the component. Might be usefull if you’re using Marathon.
package nl.iteye.swingtest;import java.awt.Component;
import java.awt.Container;
import java.lang.reflect.Field;
import javax.swing.JFrame;
import javax.swing.JPanel;/**
* @author Andrej Koelewijn */
public class TestHelper { public TestHelper() { } public static void setComponentNames(Component parent) { if (parent instanceof Container) { Component[] childs = ((Container) parent).getComponents(); Field[] fields = parent.getClass().getDeclaredFields(); for (int i = 0, n = fields.length; i < n; i++) { if (Component.class.isAssignableFrom(fields[i].getType())) { try { fields[i].setAccessible(true); Object o = fields[i].get(parent); if (o != null) { ((Component) o).setName(fields[i].getName()); } } catch (IllegalAccessException e) { } } } for (int i = 0, n = childs.length; i < n; i++) { if (childs[i] instanceof Container) { TestHelper.setComponentNames(childs[i]); } } } }
}