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