-
Notifications
You must be signed in to change notification settings - Fork 23
/
SeleniumTest.java
171 lines (152 loc) · 6.24 KB
/
SeleniumTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
/*
* #%L
* NetarchiveSuite System test
* %%
* Copyright (C) 2005 - 2018 The Royal Danish Library,
* the National Library of France and the Austrian National Library.
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 2.1 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Lesser Public License for more details.
*
* You should have received a copy of the GNU General Lesser Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/lgpl-2.1.html>.
* #L%
*/
package dk.netarkivet.systemtest;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.concurrent.TimeUnit;
import org.jaccept.TestEventManager;
import org.jaccept.structure.ExtendedTestCase;
import org.jaccept.testreport.ReportGenerator;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.BeforeSuite;
import dk.netarkivet.systemtest.environment.TestEnvironmentController;
import dk.netarkivet.systemtest.environment.testGUIController;
import dk.netarkivet.systemtest.page.PageHelper;
import dk.netarkivet.systemtest.page.SelectiveHarvestPageHelper;
/**
* The super class for all Selenium based system tests.
*/
@SuppressWarnings({"unused"})
public abstract class SeleniumTest extends ExtendedTestCase {
protected TestEnvironmentController testController;
protected static testGUIController testGUIController;
private static ReportGenerator reportGenerator;
protected final TestLogger log = new TestLogger(getClass());
protected static WebDriver driver;
protected static String baseUrl;
public SeleniumTest(TestEnvironmentController testController) {
this.testController = testController;
}
@BeforeSuite(alwaysRun = true)
public void setupTest() {
testGUIController = new testGUIController(testController);
deployTestSystem();
initialiseSelenium();
setupFixture();
}
/**
* Start the test system, either the full system including resetting of settings/DB, or just reploy of individual
* component code.
*/
private void deployTestSystem() {
if (System.getProperty("systemtest.deploy", "false").equals("true")) {
try {
testController.runCommandWithoutQuotes(getStartupScript());
} catch (Exception e) {
throw new RuntimeException("Failed to start test system", e);
}
} else {
if (System.getProperty("systemtest.redeploy.gui", "false").equals("true")) {
testGUIController.redeployGUI();
}
}
}
/**
* Defines the default test system startup script to run. May be overridden by subclasses classes.
*
* @return The startup script to run.
*/
protected String getStartupScript() {
Instant instant = Instant.now();
DateTimeFormatter formatter = DateTimeFormatter.ISO_OFFSET_DATE_TIME;
String timestamp = formatter.format(instant.atZone(ZoneId.systemDefault()));
return "all_test.sh 2>&1 | tee system_test_logs/all_test_out_" + timestamp + ".txt";
}
protected void initialiseSelenium(){
/* FirefoxProfile fxProfile = new FirefoxProfile();
fxProfile.setPreference("browser.download.folderList",2);
fxProfile.setPreference("browser.download.manager.showWhenStarting",false);
try {
fxProfile.setPreference("browser.download.dir",(File.createTempFile("aaaa","bbbb")).getParentFile().getAbsolutePath());
} catch (IOException e) {
throw new RuntimeException(e);
}
fxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/csv");
fxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","text/xml");
fxProfile.setPreference("browser.helperApps.neverAsk.saveToDisk","binary/octet-stream");
*/
//driver = new FirefoxDriver(fxProfile);
driver = new SeleniumSession<>();
driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
baseUrl = "http://" + testController.ENV.getGuiHost() + ":" + testController.ENV.getGuiPort();
PageHelper.initialize(driver, baseUrl);
testGUIController.waitForGUIToStart(600);
TestEventManager.getInstance().addFixture("Selecting English as language");
driver.findElement(By.linkText("English")).click();
}
private void setupFixture() {
HarvestUtils.minimizeDefaultHarvest();
}
@AfterSuite(alwaysRun = true)
public void shutdown() {
if (driver != null) {
try {
SelectiveHarvestPageHelper.deactivateAllHarvests();
driver.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
@AfterMethod
/**
* Takes care of failure situations. This includes: <ol>
* <li> Generate a a screen dump of the page failing the test.
* <ol>
*
* This method is called by TestNG.
*
* @param result The result which TestNG will inject
*/
public void onFailure(ITestResult result) {
/* if (!result.isSuccess()) {
log.info("Test failure, dumping screenshot as " + "target/failurescreendumps/" + result.getMethod()
+ ".png");
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
try {
FileUtils.forceMkdir(new File("target"));
FileUtils.copyFile(scrFile, new File("target/failurescreendumps/" + result.getMethod() + ".png"));
} catch (IOException e) {
log.error("Failed to save screendump on error");
}
}*/
}
public TestEnvironmentController getTestController() {
return testController;
}
}