-
Notifications
You must be signed in to change notification settings - Fork 23
/
SeleniumTest.java
169 lines (151 loc) · 6.11 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
/*
* #%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.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.jaccept.TestEventManager;
import org.jaccept.structure.ExtendedTestCase;
import org.jaccept.testreport.ReportGenerator;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
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() {
return "all_test.sh 2>&1 | tee all_test_out.txt'";
}
private 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.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
baseUrl = testController.ENV.getGuiHost() + ":" + testController.ENV.getGuiPort();
PageHelper.initialize(driver, baseUrl);
TestGUIController.waitForGUIToStart(60);
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;
}
}