Thursday September 27, 2007
GUI Testing on the NetBeans Platform (Part 2)
Let's create a realistic functional test for the JFugue Music NotePad, i.e., a test that runs through a complete user scenario, rather than just simply invoking a single action. In the end, we'll type this on the command line:
ant -Dxtest.testtype=qa-functional
And then the application will start, the New action will be invoked, various selections will be made in the wizard, and then a number of notes will be added to the music sheet. The Output window will provide information like the following:
And when you go to the file indicated above, you will see info similar to the first screenshot in yesterday's blog entry. So, clearly, this could be part of some batch process, where the call to the Ant script is done as part of a larger process.
So, here's our test, everything is the same as yesterday, except the test1() is now called testComposition() (it is important that test methods begin with the word test), with this content:
public void testComposition() {
// use main menu File -> New to open New Composition modal dialog
new ActionNoBlock("File|New", null).perform();
// wait for the New Composition dialog
WizardOperator newCompositionOper = new WizardOperator("New Composition");
// set title
new JTextFieldOperator(newCompositionOper).setText("My Composition");
// set tempo
new JTextFieldOperator(newCompositionOper, 2).setText("100");
newCompositionOper.finish();
// wait for composition pane
TopComponentOperator compositionOper = new TopComponentOperator("My Composition");
// find half note
JToggleButtonOperator halfNoteOper = new JToggleButtonOperator(compositionOper,
new ToolbarButtonChooser("Half note"));
// find quarter note
JToggleButtonOperator quarterNoteOper = new JToggleButtonOperator(compositionOper,
new ToolbarButtonChooser("Quarter note"));
// note constants
int a5 = 85;
int g5 = 90;
int f5 = 95;
int e5 = 100;
// click quarter note
quarterNoteOper.push();
// click to put note
compositionOper.clickMouse(compositionOper.getWidth()-10, g5, 1);
compositionOper.clickMouse(compositionOper.getWidth()-10, g5, 1);
halfNoteOper.push();
compositionOper.clickMouse(compositionOper.getWidth()-10, e5, 1);
quarterNoteOper.push();
compositionOper.clickMouse(compositionOper.getWidth()-10, g5, 1);
compositionOper.clickMouse(compositionOper.getWidth()-10, g5, 1);
halfNoteOper.push();
compositionOper.clickMouse(compositionOper.getWidth()-10, e5, 1);
quarterNoteOper.push();
compositionOper.clickMouse(compositionOper.getWidth()-10, g5, 1);
compositionOper.clickMouse(compositionOper.getWidth()-10, g5, 1);
compositionOper.clickMouse(compositionOper.getWidth()-10, a5, 1);
compositionOper.clickMouse(compositionOper.getWidth()-10, g5, 1);
halfNoteOper.push();
compositionOper.clickMouse(compositionOper.getWidth()-10, g5, 1);
compositionOper.clickMouse(compositionOper.getWidth()-10, f5, 1);
}
private static class ToolbarButtonChooser implements ComponentChooser {
private String buttonTooltip;
private StringComparator comparator;
public ToolbarButtonChooser(String buttonTooltip) {
this.buttonTooltip = buttonTooltip;
this.comparator = Operator.getDefaultStringComparator();
}
public boolean checkComponent(Component comp) {
return comparator.equals(((JComponent)comp).getToolTipText(), buttonTooltip);
}
public String getDescription() {
return "Toolbar button with tooltip \""+buttonTooltip+"\".";
}
}
Thanks to Jiri Skrivanek, who created this test as an illustration of how you can write Jelly tests in NetBeans IDE. When the test has completed its run, the application is closed. In the meantime, however, it has played Skákal pes, which Roumen helpfully pointed me to, about a dog jumping over oats...
Sep 27 2007, 12:47:55 AM PDT Permalink


