I'm having issues with some test cases that make use of JFileChoosers. I'm looking for a way to programmatically get rid of file chooser windows (instead of pressing ESC 7 times) when running the JUnit tests.
I've tried to include the following in my test case:
Robot robot = new Robot();
robot.delay(1000);
robot.keyPress(KeyEvent.VK_ESCAPE);
This does not seem to work. Do you have a suggestion?
Thanks in advance.
Just a guess, but sounds like you're running the Robot on the same thread as you're launching the JFileChooser. If memory serves, a lot of the JFileChooser methods block the current thread until the user has selected a file.
Try launching the Robot in a separate thread if you aren't already.
EDIT:
For example:
// Start Robot in a new thread.
new Thread(new Runnable() {
@Override
public void run() {
Robot robot = new Robot();
robot.delay(1000);
robot.keyPress(KeyEvent.VK_ESCAPE);
}
}).start();
// Launch JFileChooser.
jFileChooser.getSelectedFile();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With