Testing Dialogs with TestFX

2,719 views
Skip to first unread message

rohit vaidya

unread,
May 5, 2015, 3:19:03 AM5/5/15
to testfx-...@googlegroups.com
Experts, 
I am new to TestFX. My application has multiple dialogs and I would like to test the flows using TestFX. How do I automate clicks on Ok/Cancel of Dialog Boxes. I am using Dialog boxes shipped with pre release JDK8u60 like javafx.scene.control.Alert.
Any sample test examples will be helpful which test Dialog boxes. 
Thanks, 
Rohit Vaidya

tbeernot

unread,
May 5, 2015, 3:23:24 AM5/5/15
to testfx-...@googlegroups.com
In JFXtras I work with popups, which are similar to Dialogs, but I'm not on the latests version on TextFX yet. But maybe it is of use:
https://github.com/JFXtras/jfxtras/blob/8.0/jfxtras-test-support/src/main/java/jfxtras/test/JFXtrasGuiTest.java
--
You received this message because you are subscribed to the Google Groups "TestFX" group.
To unsubscribe from this group and stop receiving emails from it, send an email to testfx-discus...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

rohit vaidya

unread,
May 7, 2015, 2:47:35 AM5/7/15
to testfx-...@googlegroups.com
Thanks Tom for the pointer. 
I was advised against using GuiTest as its part of legacy. 
I have written this simple test. The test blocks when I use alert.showAndWait(). I would want to test dialog boxes in my application which are modal in nature. Hence I must use showAndWait(). Unfortunately this blocks the test from executing. 
public class DialogBoxTest extends ApplicationTest {

GitFxDialog dialog;
@Override
public void start(Stage stage) throws Exception {Alert alert = new Alert(AlertType.INFORMATION);
alert.setTitle(title);
alert.setHeaderText(header);
alert.setContentText(label);
alert.show();
return alert;
}

@Test
public void testDialog() throws Exception {
clickOn("OK");
}
Thanks, 
Rohit Vaidya

rohit vaidya

unread,
May 8, 2015, 10:24:32 AM5/8/15
to testfx-...@googlegroups.com
I was able to test the blocking api showAndWait() by creating a separate thread. 

Benjamin Gudehus

unread,
May 9, 2015, 8:28:41 PM5/9/15
to rohit vaidya, testfx-...@googlegroups.com
Hey,

dialog.GitInformationDialog(...) actually creates a new Dialog and calls showAndWait() on it?

I think we could find more elegang solutions for new Thread() and Thread.sleep().

--Benjamin

rohit vaidya

unread,
May 10, 2015, 5:31:39 AM5/10/15
to testfx-...@googlegroups.com, rohi...@gmail.com
Benjamin,
Yes. dialog.GitInformationDialog internally has a call to showAndWait. I had to use Thread.sleep because the test executes before the dialog launches. Perhaps an API which will wait for the stage be loaded and element available shall be useful. I will search in API doc with such api is available in TestFX.  And showAndWait on dialog is a blocking API and prevents the test from executing and hence I created a separate thread. 
I am thinking about how can I make it better though. I agree with you the solution is not elegant. 
Thanks, 
Rohit Vaidya

rohit vaidya

unread,
May 14, 2015, 12:50:33 AM5/14/15
to testfx-...@googlegroups.com
Benjamin, 
I got rid of the Thread.sleep by using WaitForAsyncUtils.waitForFxEvents();
However not sure how to get rid of new Thread creation for dialog boxes(modal) which block the main FX thread waiting for user input.  
https://github.com/jughyd/GitFx/blob/master/src/test/java/DialogBoxTest.java
Thanks in advance for your inputs. 

Regards, 
Rohit Vaidya
Reply all
Reply to author
Forward
0 new messages