1 | package gstests.tutorials;
|
---|
2 |
|
---|
3 | /*
|
---|
4 | Using AssertJ Swing to do Java GUI testing (of swing classes)
|
---|
5 | - Obtain from: https://search.maven.org/search?q=assertj-swing-junit
|
---|
6 | - Documentation: https://joel-costigliola.github.io/assertj/assertj-swing.html
|
---|
7 |
|
---|
8 | Alternatives to AssertJSwing:
|
---|
9 | https://sqa.stackexchange.com/questions/18554/open-source-tools-for-automation-of-java-gui-application-testing
|
---|
10 |
|
---|
11 | Event Dispatch Thread (EDT) pages:
|
---|
12 | - https://web.archive.org/web/20120526191520/http://alexruiz.developerblogs.com/?p=160
|
---|
13 | - https://web.archive.org/web/20130218063544/http://weblogs.java.net/blog/alexfromsun/archive/2006/02/debugging_swing.html
|
---|
14 |
|
---|
15 | */
|
---|
16 |
|
---|
17 | import org.junit.AfterClass;
|
---|
18 | import org.junit.Assert;
|
---|
19 | import org.junit.Before;
|
---|
20 | import org.junit.Test;
|
---|
21 | import org.assertj.swing.data.Index;
|
---|
22 |
|
---|
23 | import org.greenstone.gatherer.Gatherer;
|
---|
24 | import org.greenstone.gatherer.GathererProg; // main GLI class we'll be testing
|
---|
25 | import org.greenstone.gatherer.Dictionary; // access to display strings
|
---|
26 |
|
---|
27 | import org.assertj.swing.junit.testcase.AssertJSwingJUnitTestCase;
|
---|
28 | import org.assertj.swing.fixture.*;
|
---|
29 | import org.assertj.swing.edt.GuiActionRunner;
|
---|
30 | import org.assertj.swing.core.*;
|
---|
31 | import javax.swing.*;
|
---|
32 |
|
---|
33 | // static imports
|
---|
34 | import static org.assertj.swing.launcher.ApplicationLauncher.*;
|
---|
35 | import static org.assertj.swing.finder.WindowFinder.findFrame;
|
---|
36 |
|
---|
37 | public class RunGLITest extends AssertJSwingJUnitTestCase {
|
---|
38 | private FrameFixture window;
|
---|
39 |
|
---|
40 | @Override
|
---|
41 | protected void onSetUp() {
|
---|
42 | GathererProg frame = GuiActionRunner.execute(() -> new GathererProg());
|
---|
43 | // IMPORTANT: note the call to 'robot()'
|
---|
44 | // we must use the Robot from AssertJSwingJUnitTestCase
|
---|
45 |
|
---|
46 | //window = new FrameFixture(robot(), frame);
|
---|
47 | //window.show(); // shows the frame to test
|
---|
48 |
|
---|
49 | // Launch GathererProg.java's main() method
|
---|
50 | // See https://joel-costigliola.github.io/assertj/assertj-swing-launch.html
|
---|
51 |
|
---|
52 | String GSDLOS = System.getenv("GSDLOS");
|
---|
53 | String GSDLHOME = System.getenv("GSDLHOME");
|
---|
54 | String GSDL3HOME = System.getenv("GSDL3HOME");
|
---|
55 | String GSDL3SRCHOME = System.getenv("GSDL3SRCHOME");
|
---|
56 | application("org.greenstone.gatherer.GathererProg").withArgs(
|
---|
57 | "-gsdl", GSDLHOME,
|
---|
58 | "-gsdlos", GSDLOS,
|
---|
59 | "-gsdl3", GSDL3HOME,
|
---|
60 | "-gsdl3src", GSDL3SRCHOME).start();
|
---|
61 | }
|
---|
62 |
|
---|
63 |
|
---|
64 | @Test
|
---|
65 | public void testGLIRunning() {
|
---|
66 |
|
---|
67 |
|
---|
68 | // waiting 2 seconds for window, so we can see it
|
---|
69 | try{
|
---|
70 | Thread.sleep(5000);
|
---|
71 | } catch(Exception e) {
|
---|
72 | e.printStackTrace();
|
---|
73 | }
|
---|
74 |
|
---|
75 |
|
---|
76 | System.err.println("@@@ First test: GLI Running");
|
---|
77 |
|
---|
78 |
|
---|
79 | String expectedWindowTitle = Gatherer.PROGRAM_NAME;
|
---|
80 |
|
---|
81 |
|
---|
82 | // https://joel-costigliola.github.io/assertj/assertj-swing-launch.html
|
---|
83 | window = findFrame(new GenericTypeMatcher<JFrame>(JFrame.class) {
|
---|
84 | protected boolean isMatching(JFrame window) {
|
---|
85 | return window.getTitle().trim().startsWith(expectedWindowTitle)
|
---|
86 | && window.isShowing();
|
---|
87 | }
|
---|
88 | }).using(robot());
|
---|
89 |
|
---|
90 |
|
---|
91 | String gatherPaneLabel = Dictionary.get("GUI.Gather");
|
---|
92 | System.err.println("@@@ Expecting label: " + gatherPaneLabel);
|
---|
93 |
|
---|
94 | System.err.println("@@@ Second test: that Gather panel is selected and has right title");
|
---|
95 |
|
---|
96 | /*GenericTypeMatcher<JTabbedPane> textMatcher = new GenericTypeMatcher<JTabbedPane>(JTabbedPane.class) {
|
---|
97 | @Override protected boolean isMatching(JTabbedPane tabPane) {
|
---|
98 | int index = GuiActionRunner.execute(() -> tabPane.getSelectedIndex());
|
---|
99 | //int index = tabPane.getSelectedIndex();
|
---|
100 | String selectedTabTitle = GuiActionRunner.execute(() -> tabPane.getTitleAt(index));
|
---|
101 | System.err.println("### GOT TITLE: " + selectedTabTitle);
|
---|
102 | return gatherPaneLabel.equals(selectedTabTitle);
|
---|
103 | }
|
---|
104 | };
|
---|
105 | */
|
---|
106 | JTabbedPaneFixture tab = window.tabbedPane(new GenericTypeMatcher<JTabbedPane>(JTabbedPane.class) {
|
---|
107 | @Override protected boolean isMatching(JTabbedPane tabPane) {
|
---|
108 | System.err.println("### trying for match");
|
---|
109 | //int index = GuiActionRunner.execute(() -> tabPane.getSelectedIndex());
|
---|
110 | int index = tabPane.getSelectedIndex();
|
---|
111 | String selectedTabTitle = tabPane.getTitleAt(index); //GuiActionRunner.execute(() -> tabPane.getTitleAt(index));
|
---|
112 | System.err.println("### GOT TITLE: " + selectedTabTitle);
|
---|
113 | return gatherPaneLabel.equals(selectedTabTitle);
|
---|
114 | }
|
---|
115 | });
|
---|
116 | tab.requireSelectedTab(Index.atIndex(1));
|
---|
117 | tab.requireTitle(gatherPaneLabel, Index.atIndex(1));
|
---|
118 | }
|
---|
119 |
|
---|
120 |
|
---|
121 | }
|
---|