I've seen other messages on this subject but haven't been able to find any solutions, so please can someone explain how to test an application that has a main JFrame which opens a modal JDialog which open another modal JDialog.
The following code demonstrates the problem, basically it creates a JFrame with a single button which when pressed creates and opens a JDialog which has 2 buttons: one to open another instance of this JDialog and one to close it.
import java.awt.FlowLayout;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
public class JDialogTest extends JDialog
{
private static final long serialVersionUID = -5010650261572449856L;
private static int count = 0;
JDialogTest(Window owner)
{
super(owner);
count++;
setModal(true);
setTitle("Dialog "+count);
setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
JButton btn = new JButton("Test Dialog "+count);
btn.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
JDialogTest dlg = new JDialogTest(JDialogTest.this);
dlg.setVisible(true);
}
});
add(btn);
btn = new JButton("Close");
btn.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
setVisible(false);
}
});
add(btn);
pack();
}
public static void main(String[] args)
{
final JFrame frame = new JFrame("Dialog Tester");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton btn = new JButton("Open Dialog");
btn.addActionListener(new ActionListener()
{
@Override
public void actionPerformed(ActionEvent e)
{
JDialogTest dlg = new JDialogTest(frame);
dlg.setVisible(true);
}
});
frame.add(btn);
frame.pack();
frame.setVisible(true);
}
I recorded the following test where I opened several dialogs and then closed them.
package Test;
import com.windowtester.runtime.swing.locator.JButtonLocator;
import com.windowtester.runtime.swing.UITestCaseSwing;
import com.windowtester.runtime.IUIContext;
import com.windowtester.runtime.swing.condition.WindowShowingCondition;
import com.windowtester.runtime.swing.condition.WindowDisposedCondition;
public class DialogTest extends UITestCaseSwing
{
/**
* Create an Instance
*/
public DialogTest()
{
super(JDialogTest.class);
}
/**
* Main test method.
*/
public void testDialog() throws Exception
{
IUIContext ui = getUI();
ui.click(new JButtonLocator("Open Dialog"));
ui.wait(new WindowShowingCondition("Dialog 1"));
ui.click(new JButtonLocator("Test Dialog 1"));
ui.wait(new WindowShowingCondition("Dialog 2"));
ui.click(new JButtonLocator("Test Dialog 2"));
ui.wait(new WindowShowingCondition("Dialog 3"));
ui.click(new JButtonLocator("Close"));
ui.wait(new WindowDisposedCondition("Dialog 3"));
ui.click(new JButtonLocator("Close"));
ui.wait(new WindowDisposedCondition("Dialog 2"));
ui.click(new JButtonLocator("Close"));
ui.wait(new WindowDisposedCondition("Dialog 1"));
ui.wait(new WindowDisposedCondition("Dialog Tester"));
}
}
On running the test class it opens Dialog 2 but then throws the following exception:
com.windowtester.runtime.WaitTimedOutException: Timed out waiting for Dialog 2 to show
at com.windowtester.internal.swing.UIContextSwing.wait(UIContextSwing.java:191)
at com.windowtester.internal.swing.UIContextSwing.wait(UIContextSwing.java:178)
at com.windowtester.internal.swing.UIContextSwing.wait(UIContextSwing.java:174)
at Test.DialogTest.testDialog(DialogTest.java:29)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:164)
at junit.framework.TestCase.runBare(TestCase.java:130)
at com.windowtester.runtime.common.UITestCaseCommon.access$0(UITestCaseCommon.java:1)
at com.windowtester.runtime.common.UITestCaseCommon$2.run(UITestCaseCommon.java:140)
at com.windowtester.runtime.common.UITestCaseCommon$3.run(UITestCaseCommon.java:161)
at com.windowtester.internal.runtime.junit.core.SequenceRunner$1.run(SequenceRunner.java:50)