Instantly share code, notes, and snippets.
CreatedOctober 2, 2020 10:05
Save hashrock/26afb842db35eb03699acf735a692f2e to your computer and use it in GitHub Desktop.
AssertJ-Swing wait example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| packagehashrock.study.swing; | |
| importorg.assertj.swing.core.GenericTypeMatcher; | |
| importorg.assertj.swing.edt.GuiActionRunner; | |
| importorg.assertj.swing.fixture.FrameFixture; | |
| importorg.assertj.swing.junit.testcase.AssertJSwingJUnitTestCase; | |
| importorg.assertj.swing.timing.Condition; | |
| importorg.junit.Test; | |
| importstaticorg.assertj.swing.timing.Pause.pause; | |
| importstaticorg.assertj.swing.timing.Timeout.timeout; | |
| importjavax.swing.*; | |
| publicclassMainTestextendsAssertJSwingJUnitTestCase { | |
| privateFrameFixturewindow; | |
| @Override | |
| protectedvoidonSetUp()throwsException { | |
| MainFrameframe =GuiActionRunner.execute(() ->newMainFrame()); | |
| window =newFrameFixture(robot(),frame); | |
| window.show(); | |
| } | |
| @Test | |
| publicvoidshouldTestSwing()throwsInterruptedException { | |
| window.label("output").requireText("TODO"); | |
| GenericTypeMatcher<JButton>textMatcher =newGenericTypeMatcher<JButton>(JButton.class,false) { | |
| @OverrideprotectedbooleanisMatching(JButtonbutton) { | |
| return"ok".equals(button.getName()); | |
| } | |
| }; | |
| JButtontarget =window.button(textMatcher).target(); | |
| pause(newCondition("OK button to be enabled") { | |
| publicbooleantest() { | |
| returntarget.isVisible(); | |
| } | |
| },timeout(10000)); | |
| window.button("ok").click(); | |
| pause(newCondition("Label Timeout") { | |
| publicbooleantest() { | |
| returnwindow.label("output").text().equals("Pressed"); | |
| } | |
| },timeout(10000)); | |
| window.label("output").requireText("Pressed"); | |
| } | |
| } |
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment