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 | JUNIT:
|
---|
27 | - https://junit.org/junit4/faq.html#atests_2
|
---|
28 | How do I use a test fixture?
|
---|
29 | - https://junit.org/junit4/faq.html#organize_3
|
---|
30 | How can I run setUp() and tearDown() code once for all of my tests?
|
---|
31 |
|
---|
32 | */
|
---|
33 |
|
---|
34 | // Junit imports
|
---|
35 | import org.junit.AfterClass;
|
---|
36 | import org.junit.Assert;
|
---|
37 | import org.junit.Before;
|
---|
38 | import org.junit.Test;
|
---|
39 |
|
---|
40 | // GLI imports
|
---|
41 | import org.greenstone.gatherer.Gatherer;
|
---|
42 | import org.greenstone.gatherer.GathererProg; // main GLI class we'll be testing
|
---|
43 | import org.greenstone.gatherer.Dictionary; // access to display strings
|
---|
44 |
|
---|
45 | // Java GUI testing with AssertJ Swing
|
---|
46 | import org.assertj.swing.junit.testcase.AssertJSwingJUnitTestCase;
|
---|
47 | import org.assertj.swing.fixture.*;
|
---|
48 | import org.assertj.swing.edt.GuiActionRunner;
|
---|
49 | import org.assertj.swing.core.*;
|
---|
50 | import org.assertj.swing.data.Index;
|
---|
51 |
|
---|
52 | // Selenium
|
---|
53 | import org.openqa.selenium.By;
|
---|
54 | import org.openqa.selenium.WebDriver;
|
---|
55 | import org.openqa.selenium.WebElement;
|
---|
56 | import org.openqa.selenium.firefox.FirefoxDriver;
|
---|
57 |
|
---|
58 | // Helper classes for selenium and AssertJ Swing tests
|
---|
59 | import org.greenstone.gsdl3.testing.GSTestingUtil;
|
---|
60 | import org.greenstone.gsdl3.testing.GSGUITestingUtil;
|
---|
61 |
|
---|
62 | // Java imports
|
---|
63 | import javax.swing.*;
|
---|
64 |
|
---|
65 |
|
---|
66 |
|
---|
67 | // static imports
|
---|
68 | import static org.assertj.swing.launcher.ApplicationLauncher.*;
|
---|
69 | import static org.assertj.swing.finder.WindowFinder.findFrame;
|
---|
70 |
|
---|
71 | import static org.greenstone.gsdl3.testing.GSGUITestingUtil.*;
|
---|
72 |
|
---|
73 | public class RunGLITest extends AssertJSwingJUnitTestCase {
|
---|
74 |
|
---|
75 | private static WebDriver _driver = new FirefoxDriver(); // selenium
|
---|
76 |
|
---|
77 | private FrameFixture window;
|
---|
78 |
|
---|
79 | // Selenium
|
---|
80 | @Before
|
---|
81 | public void init()
|
---|
82 | {
|
---|
83 | //https://stackoverflow.com/questions/38676719/selenium-using-java-the-path-to-the-driver-executable-must-be-set-by-the-webdr
|
---|
84 | // GS3's build.xml would have set the webdriver.gecko.driver path System.Property to
|
---|
85 | // the location of Firefox' geckodriver executable when launching this test class already.
|
---|
86 | // So now we can continue to just do:
|
---|
87 | _driver.get(System.getProperty("SERVERURL"));
|
---|
88 | }
|
---|
89 |
|
---|
90 | @Override
|
---|
91 | protected void onSetUp() {
|
---|
92 | GathererProg frame = GuiActionRunner.execute(() -> new GathererProg());
|
---|
93 | // IMPORTANT: note the call to 'robot()'
|
---|
94 | // we must use the Robot from AssertJSwingJUnitTestCase
|
---|
95 |
|
---|
96 | //window = new FrameFixture(robot(), frame);
|
---|
97 | //window.show(); // shows the frame to test
|
---|
98 |
|
---|
99 | // Launch GathererProg.java's main() method
|
---|
100 | // See https://joel-costigliola.github.io/assertj/assertj-swing-launch.html
|
---|
101 |
|
---|
102 | String GSDLOS = System.getenv("GSDLOS");
|
---|
103 | String GSDLHOME = System.getenv("GSDLHOME");
|
---|
104 | String GSDL3HOME = System.getenv("GSDL3HOME");
|
---|
105 | String GSDL3SRCHOME = System.getenv("GSDL3SRCHOME");
|
---|
106 | application("org.greenstone.gatherer.GathererProg").withArgs(
|
---|
107 | "-gsdl", GSDLHOME,
|
---|
108 | "-gsdlos", GSDLOS,
|
---|
109 | "-gsdl3", GSDL3HOME,
|
---|
110 | "-gsdl3src", GSDL3SRCHOME,
|
---|
111 | "-testing_mode").start();
|
---|
112 | }
|
---|
113 |
|
---|
114 |
|
---|
115 | @Test
|
---|
116 | public void testGLIRunning() {
|
---|
117 |
|
---|
118 |
|
---|
119 | // waiting 2 seconds for window, so we can see it
|
---|
120 | try{
|
---|
121 | Thread.sleep(5000);
|
---|
122 | } catch(Exception e) {
|
---|
123 | e.printStackTrace();
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | System.err.println("@@@ First test: GLI Running");
|
---|
128 |
|
---|
129 |
|
---|
130 | String expectedWindowTitle = Gatherer.PROGRAM_NAME;
|
---|
131 |
|
---|
132 | window = findFrame("GUIManager").using(robot());
|
---|
133 |
|
---|
134 | String gatherPaneLabel = Dictionary.get("GUI.Gather");
|
---|
135 | System.err.println("@@@ Expecting label: " + gatherPaneLabel);
|
---|
136 |
|
---|
137 | System.err.println("@@@ Second test: that Gather panel is selected and has right title");
|
---|
138 |
|
---|
139 | JTabbedPaneFixture tab = window.tabbedPane("GUIManager.tab_pane");
|
---|
140 |
|
---|
141 |
|
---|
142 | tab.requireSelectedTab(Index.atIndex(1));
|
---|
143 | tab.requireTitle(gatherPaneLabel, Index.atIndex(1));
|
---|
144 |
|
---|
145 | // attempt to switch to enrich pane, uses static methods of GSGUITestingUtil
|
---|
146 | // through static import of that class
|
---|
147 | switchToPane(window, DOWNLOAD_PANE);
|
---|
148 |
|
---|
149 | loadCollection(window, "lucene-jdbm-demo");
|
---|
150 |
|
---|
151 | // wait a couple of seconds again?
|
---|
152 | try{
|
---|
153 | Thread.sleep(5000);
|
---|
154 | } catch(Exception e) {
|
---|
155 | e.printStackTrace();
|
---|
156 | }
|
---|
157 |
|
---|
158 |
|
---|
159 | }
|
---|
160 |
|
---|
161 | // Selenium
|
---|
162 | // called once and only once: to quit the firefox driver geckodriver
|
---|
163 | @AfterClass
|
---|
164 | public static void destroy()
|
---|
165 | {
|
---|
166 | _driver.quit();
|
---|
167 | }
|
---|
168 | }
|
---|