Skip to content

Instantly share code, notes, and snippets.

@hashrock
Created October 2, 2020 10:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hashrock/26afb842db35eb03699acf735a692f2e to your computer and use it in GitHub Desktop.
Save hashrock/26afb842db35eb03699acf735a692f2e to your computer and use it in GitHub Desktop.
AssertJ-Swing wait example
package hashrock.study.swing;
import org.assertj.swing.core.GenericTypeMatcher;
import org.assertj.swing.edt.GuiActionRunner;
import org.assertj.swing.fixture.FrameFixture;
import org.assertj.swing.junit.testcase.AssertJSwingJUnitTestCase;
import org.assertj.swing.timing.Condition;
import org.junit.Test;
import static org.assertj.swing.timing.Pause.pause;
import static org.assertj.swing.timing.Timeout.timeout;
import javax.swing.*;
public class MainTest extends AssertJSwingJUnitTestCase {
private FrameFixture window;
@Override
protected void onSetUp() throws Exception {
MainFrame frame = GuiActionRunner.execute(() -> new MainFrame());
window = new FrameFixture(robot(), frame);
window.show();
}
@Test
public void shouldTestSwing() throws InterruptedException {
window.label("output").requireText("TODO");
GenericTypeMatcher<JButton> textMatcher = new GenericTypeMatcher<JButton>(JButton.class, false) {
@Override protected boolean isMatching(JButton button) {
return "ok".equals(button.getName());
}
};
JButton target = window.button(textMatcher).target();
pause(new Condition("OK button to be enabled") {
public boolean test() {
return target.isVisible();
}
}, timeout(10000));
window.button("ok").click();
pause(new Condition("Label Timeout") {
public boolean test() {
return window.label("output").text().equals("Pressed");
}
}, timeout(10000));
window.label("output").requireText("Pressed");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment