1 | package gstests.tutorials;
|
---|
2 |
|
---|
3 | /*
|
---|
4 | Ctrl+Shift+A TO ABORT A RUNNING GUI-TEST CYCLE (won't abort Selenium/browser tests)
|
---|
5 |
|
---|
6 | Using AssertJ Swing to do Java GUI testing (of swing classes)
|
---|
7 | - Obtain from: https://search.maven.org/search?q=assertj-swing-junit
|
---|
8 | - Documentation: https://joel-costigliola.github.io/assertj/assertj-swing.html
|
---|
9 |
|
---|
10 | Alternatives to AssertJSwing (googled: automate java interface testing) suggested at
|
---|
11 | https://sqa.stackexchange.com/questions/18554/open-source-tools-for-automation-of-java-gui-application-testing
|
---|
12 |
|
---|
13 | Event Dispatch Thread (EDT) pages:
|
---|
14 | - https://joel-costigliola.github.io/assertj/assertj-swing-edt.html
|
---|
15 | - https://web.archive.org/web/20120526191520/http://alexruiz.developerblogs.com/?p=160
|
---|
16 | - https://web.archive.org/web/20130218063544/http://weblogs.java.net/blog/alexfromsun/archive/2006/02/debugging_swing.html
|
---|
17 | - https://stackoverflow.com/questions/2829364/java-difference-between-swingworker-and-swingutilities-invokelater
|
---|
18 |
|
---|
19 | Got AssertJ Swing from Maven Central Repository:
|
---|
20 | https://search.maven.org/search?q=assertj-swing-junit
|
---|
21 | -> http://central.maven.org/maven2/org/assertj/assertj-core/3.8.0/
|
---|
22 | More jar files: http://repo1.maven.org/maven2/org/assertj/
|
---|
23 | - http://repo1.maven.org/maven2/org/assertj/assertj-swing/3.8.0/
|
---|
24 | - http://central.maven.org/maven2/org/assertj/assertj-core/3.8.0/
|
---|
25 |
|
---|
26 | API:
|
---|
27 | https://joel-costigliola.github.io/assertj/swing/api/index.html
|
---|
28 |
|
---|
29 | JUNIT:
|
---|
30 | - https://junit.org/junit4/faq.html#atests_2
|
---|
31 | How do I use a test fixture?
|
---|
32 | - https://junit.org/junit4/faq.html#organize_3
|
---|
33 | How can I run setUp() and tearDown() code once for all of my tests?
|
---|
34 |
|
---|
35 | */
|
---|
36 |
|
---|
37 | // Junit imports
|
---|
38 | import org.junit.AfterClass;
|
---|
39 | import org.junit.After;
|
---|
40 | import org.junit.Assert;
|
---|
41 | import org.junit.Before;
|
---|
42 | import org.junit.BeforeClass;
|
---|
43 | import org.junit.Test;
|
---|
44 |
|
---|
45 | // GLI imports
|
---|
46 | import org.greenstone.gatherer.Gatherer;
|
---|
47 | import org.greenstone.gatherer.GathererProg; // main GLI class we'll be testing
|
---|
48 | import org.greenstone.gatherer.Dictionary; // access to display strings
|
---|
49 |
|
---|
50 | // Java GUI testing with AssertJ Swing
|
---|
51 | import org.assertj.swing.junit.testcase.AssertJSwingJUnitTestCase;
|
---|
52 | import org.assertj.swing.fixture.*;
|
---|
53 | import org.assertj.swing.core.*; // Robot
|
---|
54 | import org.assertj.swing.data.Index;
|
---|
55 | import org.assertj.swing.timing.Timeout;
|
---|
56 |
|
---|
57 | // Selenium
|
---|
58 | import org.openqa.selenium.By;
|
---|
59 | import org.openqa.selenium.WebDriver;
|
---|
60 | import org.openqa.selenium.WebElement;
|
---|
61 | import org.openqa.selenium.firefox.FirefoxDriver;
|
---|
62 |
|
---|
63 | // Helper classes for selenium and AssertJ Swing tests
|
---|
64 | import org.greenstone.gsdl3.testing.GSTestingUtil;
|
---|
65 | import org.greenstone.gsdl3.testing.GSGUITestingUtil;
|
---|
66 |
|
---|
67 | // Java imports
|
---|
68 | import javax.swing.*;
|
---|
69 |
|
---|
70 | // static imports - call static functions without class as namespace qualifier
|
---|
71 | import static org.greenstone.gsdl3.testing.GSGUITestingUtil.*;
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Class that will eventually go through all the Greenstone3 tutorials by running GLI and GEMS.
|
---|
75 | */
|
---|
76 | public class RunGLITest extends AssertJSwingJUnitTestCase {
|
---|
77 | // For aborting running test suite, with Ctrl+Shift+A by default
|
---|
78 | // https://joel-costigliola.github.io/assertj/assertj-swing-running.html
|
---|
79 | private static EmergencyAbortListener abortListener = EmergencyAbortListener.registerInToolkit();
|
---|
80 | private static WebDriver _driver = new FirefoxDriver(); // selenium
|
---|
81 |
|
---|
82 | private FrameFixture window;
|
---|
83 |
|
---|
84 | // https://stackoverflow.com/questions/50815329/close-application-after-test-without-test-failure-using-assertj
|
---|
85 | // https://stackoverflow.com/questions/23139321/testing-java-gui-application-closing-window-during-test-ends-test-suite
|
---|
86 | // https://stackoverflow.com/questions/5401281/preventing-system-exit-from-api
|
---|
87 | /*
|
---|
88 | @BeforeClass
|
---|
89 | public static void initOnce() {
|
---|
90 | // to prevent system.exit from causing issues
|
---|
91 | org.assertj.swing.security.NoExitSecurityManagerInstaller.installNoExitSecurityManager();
|
---|
92 | }
|
---|
93 | */
|
---|
94 | // Selenium
|
---|
95 | @Before
|
---|
96 | public void init()
|
---|
97 | {
|
---|
98 | //https://stackoverflow.com/questions/38676719/selenium-using-java-the-path-to-the-driver-executable-must-be-set-by-the-webdr
|
---|
99 | // GS3's build.xml would have set the webdriver.gecko.driver path System.Property to
|
---|
100 | // the location of Firefox' geckodriver executable when launching this test class already.
|
---|
101 | // So now we can continue to just do:
|
---|
102 | _driver.get(System.getProperty("SERVERURL"));
|
---|
103 | }
|
---|
104 |
|
---|
105 |
|
---|
106 | // assertj-swing
|
---|
107 | // As init() above, onSetUp() gets called before every test method too,
|
---|
108 | // but is overridden from assertj-swing base class
|
---|
109 | @Override
|
---|
110 | protected void onSetUp() {
|
---|
111 | System.err.println("#### onSetUp called - running GLI");
|
---|
112 | // Launch GLI and then get a ref to the launched app window:
|
---|
113 | runGLI();
|
---|
114 |
|
---|
115 |
|
---|
116 | // IMPORTANT, note the call to 'robot()': must use the Robot from AssertJSwingJUnitTestCase
|
---|
117 | //window = findFrame("GUIManager").using(robot());
|
---|
118 | window = GSGUITestingUtil.getGLIApplicationWindow(robot());
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | @Test
|
---|
123 | public void testGLIRunning() {
|
---|
124 | // waiting a few seconds for window, so we can see it
|
---|
125 | PAUSE(2);
|
---|
126 |
|
---|
127 | System.err.println("@@@ First test: GLI Running");
|
---|
128 |
|
---|
129 | // steal any collection lock that may or may not presents itself within param seconds
|
---|
130 | stealAnyCollectionLock(1);
|
---|
131 |
|
---|
132 | String expectedWindowTitle = Gatherer.PROGRAM_NAME;
|
---|
133 |
|
---|
134 |
|
---|
135 | String gatherPaneLabel = Dictionary.get("GUI.Gather");
|
---|
136 | System.err.println("@@@ Expecting label: " + gatherPaneLabel);
|
---|
137 |
|
---|
138 | System.err.println("@@@ Second test: that Gather panel is selected and has right title");
|
---|
139 |
|
---|
140 | JTabbedPaneFixture tab = window.tabbedPane("GUIManager.tab_pane");
|
---|
141 |
|
---|
142 |
|
---|
143 | tab.requireSelectedTab(Index.atIndex(1));
|
---|
144 | tab.requireTitle(gatherPaneLabel, Index.atIndex(1));
|
---|
145 |
|
---|
146 | // attempt to switch to enrich pane, uses static methods of GSGUITestingUtil
|
---|
147 | // through static import of that class
|
---|
148 | switchToPane(DOWNLOAD_PANE);
|
---|
149 |
|
---|
150 | // For testing, want GLI to be in librarian mode by default
|
---|
151 | changeUserMode("librarian");
|
---|
152 |
|
---|
153 | switchToPane(GATHER_PANE);
|
---|
154 |
|
---|
155 | loadCollection("lucene-jdbm-demo");
|
---|
156 |
|
---|
157 | // wait a couple of seconds again?
|
---|
158 | PAUSE(2);
|
---|
159 | closeCollection();
|
---|
160 |
|
---|
161 | createCollection("pinky", "Pinky was here", null);
|
---|
162 | closeCollection(); // collection must be closed in order to be deleted
|
---|
163 | deleteCollection("pinky");
|
---|
164 | // wait a few of seconds again?
|
---|
165 | PAUSE(2);
|
---|
166 |
|
---|
167 | exportCollection("GreenstoneMETS", "lucene-jdbm-demo");
|
---|
168 |
|
---|
169 | // Reset GLI to expert mode for regular GLI use
|
---|
170 | changeUserMode("expert");
|
---|
171 |
|
---|
172 | PAUSE(2);
|
---|
173 |
|
---|
174 | switchToPane(GATHER_PANE);
|
---|
175 |
|
---|
176 | exitGLI(); //System.exit() on GLI won't allow quit to be called on Selenium's web _driver, why? Because System.exit exited the JUnit tests too
|
---|
177 |
|
---|
178 | PAUSE(3);
|
---|
179 | }
|
---|
180 |
|
---|
181 |
|
---|
182 | @Test
|
---|
183 | public void tutorial_tests() {
|
---|
184 | /*
|
---|
185 | // Preliminaries
|
---|
186 | // waiting a few seconds for window, so we can see GLI running
|
---|
187 | PAUSE(2);
|
---|
188 |
|
---|
189 | System.err.println("@@@ First test: GLI Running");
|
---|
190 |
|
---|
191 | // steal any collection lock that may or may not presents itself within param seconds
|
---|
192 | stealAnyCollectionLock(1);
|
---|
193 | switchToPane(GATHER_PANE);
|
---|
194 |
|
---|
195 | // dummy tests
|
---|
196 | testGLIRunning();
|
---|
197 | */
|
---|
198 | // First tutorial
|
---|
199 | simpleHTML();
|
---|
200 |
|
---|
201 | }
|
---|
202 |
|
---|
203 | // Selenium
|
---|
204 | // called once and only once: to quit the firefox driver geckodriver
|
---|
205 | @AfterClass
|
---|
206 | public static void destroy()
|
---|
207 | {
|
---|
208 | abortListener.unregister(); // aborting assertJ-swing GUI tests
|
---|
209 | _driver.quit();
|
---|
210 | //After exiting your application uninstall the no exit security manager
|
---|
211 | //org.assertj.swing.security.NoExitSecurityManagerInstaller.installNoExitSecurityManager().uninstall();
|
---|
212 | }
|
---|
213 |
|
---|
214 | /****************************** TUTORIAL FUNCTIONS *******************************/
|
---|
215 | // Tutorial functions: regular methods called by JUnit at-Test methods in sequence
|
---|
216 | // to ensure sequential ordering of tests
|
---|
217 | public void simpleHTML() {
|
---|
218 | System.err.println("@@@ Tutorial 1: simple HTML");
|
---|
219 | stealAnyCollectionLock(1);
|
---|
220 | switchToPane(GATHER_PANE);
|
---|
221 | changeUserMode("librarian");
|
---|
222 | }
|
---|
223 |
|
---|
224 | }
|
---|