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

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

Merging Selenium with AssertJ Swing stuff in first tutorial test java class. And it works!

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).start();
102 }
103
104
105 @Test
106 public void testGLIRunning() {
107
108
109 // waiting 2 seconds for window, so we can see it
110 try{
111 Thread.sleep(5000);
112 } catch(Exception e) {
113 e.printStackTrace();
114 }
115
116
117 System.err.println("@@@ First test: GLI Running");
118
119
120 String expectedWindowTitle = Gatherer.PROGRAM_NAME;
121
122
123 // https://joel-costigliola.github.io/assertj/assertj-swing-launch.html
124 window = findFrame(new GenericTypeMatcher<JFrame>(JFrame.class) {
125 protected boolean isMatching(JFrame window) {
126 return window.getTitle().trim().startsWith(expectedWindowTitle)
127 && window.isShowing();
128 }
129 }).using(robot());
130
131
132 String gatherPaneLabel = Dictionary.get("GUI.Gather");
133 System.err.println("@@@ Expecting label: " + gatherPaneLabel);
134
135 System.err.println("@@@ Second test: that Gather panel is selected and has right title");
136
137 /*GenericTypeMatcher<JTabbedPane> textMatcher = new GenericTypeMatcher<JTabbedPane>(JTabbedPane.class) {
138 @Override protected boolean isMatching(JTabbedPane tabPane) {
139 int index = GuiActionRunner.execute(() -> tabPane.getSelectedIndex());
140 //int index = tabPane.getSelectedIndex();
141 String selectedTabTitle = GuiActionRunner.execute(() -> tabPane.getTitleAt(index));
142 System.err.println("### GOT TITLE: " + selectedTabTitle);
143 return gatherPaneLabel.equals(selectedTabTitle);
144 }
145 };
146 */
147 JTabbedPaneFixture tab = window.tabbedPane(new GenericTypeMatcher<JTabbedPane>(JTabbedPane.class) {
148 @Override protected boolean isMatching(JTabbedPane tabPane) {
149 System.err.println("### trying for match");
150 //int index = GuiActionRunner.execute(() -> tabPane.getSelectedIndex());
151 int index = tabPane.getSelectedIndex();
152 String selectedTabTitle = tabPane.getTitleAt(index); //GuiActionRunner.execute(() -> tabPane.getTitleAt(index));
153 System.err.println("### GOT TITLE: " + selectedTabTitle);
154 return gatherPaneLabel.equals(selectedTabTitle);
155 }
156 });
157 tab.requireSelectedTab(Index.atIndex(1));
158 tab.requireTitle(gatherPaneLabel, Index.atIndex(1));
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}
Note: See TracBrowser for help on using the repository browser.