source: gs3-extensions/testing/trunk/src/src/gstests/tutorials/RunGLITest.java@ 32690

Last change on this file since 32690 was 32690, checked in by ak19, 5 years ago

New flag -testing_mode when running GLI only used by the test harness.

File size: 5.9 KB
Line 
1package gstests.tutorials;
2
3/*
4Using 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
8Alternatives to AssertJSwing (googled: automate java interface testing) suggested at
9https://sqa.stackexchange.com/questions/18554/open-source-tools-for-automation-of-java-gui-application-testing
10
11Event 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
16Got AssertJ Swing from Maven Central Repository:
17https://search.maven.org/search?q=assertj-swing-junit
18-> http://central.maven.org/maven2/org/assertj/assertj-core/3.8.0/
19More 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
23API:
24https://joel-costigliola.github.io/assertj/swing/api/index.html
25*/
26
27// Junit imports
28import org.junit.AfterClass;
29import org.junit.Assert;
30import org.junit.Before;
31import org.junit.Test;
32
33// GLI imports
34import org.greenstone.gatherer.Gatherer;
35import org.greenstone.gatherer.GathererProg; // main GLI class we'll be testing
36import org.greenstone.gatherer.Dictionary; // access to display strings
37
38// Java GUI testing with AssertJ Swing
39import org.assertj.swing.junit.testcase.AssertJSwingJUnitTestCase;
40import org.assertj.swing.fixture.*;
41import org.assertj.swing.edt.GuiActionRunner;
42import org.assertj.swing.core.*;
43import org.assertj.swing.data.Index;
44
45// Selenium
46import org.openqa.selenium.By;
47import org.openqa.selenium.WebDriver;
48import org.openqa.selenium.WebElement;
49import org.openqa.selenium.firefox.FirefoxDriver;
50
51// Helper classes for selenium and AssertJ Swing tests
52import org.greenstone.gsdl3.testing.GSTestingUtil;
53import org.greenstone.gsdl3.testing.GSGUITestingUtil;
54
55// Java imports
56import javax.swing.*;
57
58
59
60// static imports
61import static org.assertj.swing.launcher.ApplicationLauncher.*;
62import static org.assertj.swing.finder.WindowFinder.findFrame;
63
64public class RunGLITest extends AssertJSwingJUnitTestCase {
65
66 private static WebDriver _driver = new FirefoxDriver(); // selenium
67
68 private FrameFixture window;
69
70 // Selenium
71 @Before
72 public void init()
73 {
74 //https://stackoverflow.com/questions/38676719/selenium-using-java-the-path-to-the-driver-executable-must-be-set-by-the-webdr
75 // GS3's build.xml would have set the webdriver.gecko.driver path System.Property to
76 // the location of Firefox' geckodriver executable when launching this test class already.
77 // So now we can continue to just do:
78 _driver.get(System.getProperty("SERVERURL"));
79 }
80
81 @Override
82 protected void onSetUp() {
83 GathererProg frame = GuiActionRunner.execute(() -> new GathererProg());
84 // IMPORTANT: note the call to 'robot()'
85 // we must use the Robot from AssertJSwingJUnitTestCase
86
87 //window = new FrameFixture(robot(), frame);
88 //window.show(); // shows the frame to test
89
90 // Launch GathererProg.java's main() method
91 // See https://joel-costigliola.github.io/assertj/assertj-swing-launch.html
92
93 String GSDLOS = System.getenv("GSDLOS");
94 String GSDLHOME = System.getenv("GSDLHOME");
95 String GSDL3HOME = System.getenv("GSDL3HOME");
96 String GSDL3SRCHOME = System.getenv("GSDL3SRCHOME");
97 application("org.greenstone.gatherer.GathererProg").withArgs(
98 "-gsdl", GSDLHOME,
99 "-gsdlos", GSDLOS,
100 "-gsdl3", GSDL3HOME,
101 "-gsdl3src", GSDL3SRCHOME,
102 "-testing_mode").start();
103 }
104
105
106 @Test
107 public void testGLIRunning() {
108
109
110 // waiting 2 seconds for window, so we can see it
111 try{
112 Thread.sleep(5000);
113 } catch(Exception e) {
114 e.printStackTrace();
115 }
116
117
118 System.err.println("@@@ First test: GLI Running");
119
120
121 String expectedWindowTitle = Gatherer.PROGRAM_NAME;
122
123
124 // https://joel-costigliola.github.io/assertj/assertj-swing-launch.html
125 window = findFrame(new GenericTypeMatcher<JFrame>(JFrame.class) {
126 protected boolean isMatching(JFrame window) {
127 return window.getTitle().trim().startsWith(expectedWindowTitle)
128 && window.isShowing();
129 }
130 }).using(robot());
131
132
133 String gatherPaneLabel = Dictionary.get("GUI.Gather");
134 System.err.println("@@@ Expecting label: " + gatherPaneLabel);
135
136 System.err.println("@@@ Second test: that Gather panel is selected and has right title");
137
138 /*GenericTypeMatcher<JTabbedPane> textMatcher = new GenericTypeMatcher<JTabbedPane>(JTabbedPane.class) {
139 @Override protected boolean isMatching(JTabbedPane tabPane) {
140 int index = GuiActionRunner.execute(() -> tabPane.getSelectedIndex());
141 //int index = tabPane.getSelectedIndex();
142 String selectedTabTitle = GuiActionRunner.execute(() -> tabPane.getTitleAt(index));
143 System.err.println("### GOT TITLE: " + selectedTabTitle);
144 return gatherPaneLabel.equals(selectedTabTitle);
145 }
146 };
147 */
148 JTabbedPaneFixture tab = window.tabbedPane(new GenericTypeMatcher<JTabbedPane>(JTabbedPane.class) {
149 @Override protected boolean isMatching(JTabbedPane tabPane) {
150 System.err.println("### trying for match");
151 //int index = GuiActionRunner.execute(() -> tabPane.getSelectedIndex());
152 int index = tabPane.getSelectedIndex();
153 String selectedTabTitle = tabPane.getTitleAt(index); //GuiActionRunner.execute(() -> tabPane.getTitleAt(index));
154 System.err.println("### GOT TITLE: " + selectedTabTitle);
155 return gatherPaneLabel.equals(selectedTabTitle);
156 }
157 });
158 tab.requireSelectedTab(Index.atIndex(1));
159 tab.requireTitle(gatherPaneLabel, Index.atIndex(1));
160 }
161
162 // Selenium
163 // called once and only once: to quit the firefox driver geckodriver
164 @AfterClass
165 public static void destroy()
166 {
167 _driver.quit();
168 }
169}
Note: See TracBrowser for help on using the repository browser.