Closed nashirj closed 5 years ago
This is not a solution for the issue, but is a workaround that allows the desired behavior. The issue can be mitigated by invoking robot() for the comboBox().
Instead of doing
window.comboBox().pressAndReleaseKeys(KeyEvent.VK_S);
doing
window.comboBox().robot().pressAndReleaseKeys(KeyEvent.VK_S);
works as expected.
Thanks for opening the issue and posting the solution/workaround to your problem.
Sure thing! Thanks for all your work on assertj.
When trying to simulate input using AssertJ's pressAndReleaseKeys() for unit testing a JComboBox in a Java Swing program, I am not seeing the expected behavior. The program will most often hang on the pressAndReleaseKeys line and then fail, or occasionally will delete all the text currently in the JComboBox being tested, causing later assertions to fail (i.e. requireSelection()). The stack trace I receive for the provided example program (see below) when it hangs is as follows:
I have been using FEST and am hoping to migrate my tests to AssertJ since it is being actively maintained, whereas FEST hasn't been updated for years. I used Joel Costigliola's migration from Fest to AssertJ guide, but am having trouble when simulating keyboard input by using pressAndReleaseKeys(). I am able to simulate input when using a JTextComponentFixture i.e.
(where window is a FrameFixture, a container in both AssertJ and FEST), but I am unable to simulate input when using a JComboBoxFixture i.e.
This obstacle can usually be avoided, since most "key presses" can be simulated by using enterText i.e.
but I would like to be able to use the arrow keys, control key, and other keys where I can't simulate the key press using enterText(). Is this failure due to an issue with my environment*, an issue with the way I'm using it, or is the API itself flawed?
I tried using pressKey() and then releaseKey() as a workaround, but that doesn't work with JComboBox either, and my program instead hangs on pressKey(). That being said, I am not able to use pressKey() and releaseKey() to test a JComboBox with FEST either.
*Environment details: Language version: java version "1.8.0_131" AssertJ packages: assertj-core-3.11.1.jar, assertj-swing-3.9.2.jar Operating system: Red Hat Release 6.10 (Santiago) IDE: Netbeans 8.0.2
Please let me know if there's any other information I can provide to help fix the issue!
Sample GUI application:
Test file for AssertJ: