-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAbstractViewTest.java
180 lines (149 loc) · 5.5 KB
/
AbstractViewTest.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
172
173
174
175
176
177
178
179
180
package org.vaadin.tatu;
import io.github.bonigarcia.wdm.WebDriverManager;
import java.util.Arrays;
import java.util.stream.Collectors;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import com.vaadin.testbench.Parameters;
import com.vaadin.testbench.ScreenshotOnFailureRule;
import com.vaadin.testbench.TestBench;
import com.vaadin.testbench.TestBenchTestCase;
/**
* Base class for ITs
* <p>
* The tests use Chrome driver (see pom.xml for integration-tests profile) to
* run integration tests on a headless Chrome. If a property {@code test.use
* .hub} is set to true, {@code AbstractViewTest} will assume that the TestBench
* test is running in a CI environment. In order to keep the this class light,
* it makes certain assumptions about the CI environment (such as available
* environment variables). It is not advisable to use this class as a base class
* for you own TestBench tests.
* <p>
* To learn more about TestBench, visit <a href=
* "https://vaadin.com/docs/v10/testbench/testbench-overview.html">Vaadin
* TestBench</a>.
*/
public abstract class AbstractViewTest extends TestBenchTestCase {
private static final int SERVER_PORT = 8080;
private static final String WEB_SOCKET_CONNECTION_ERROR_PREFIX = "WebSocket connection to ";
private final String route;
@Rule
public ScreenshotOnFailureRule rule = new ScreenshotOnFailureRule(this,
true);
@BeforeClass
public static void setupClass() {
WebDriverManager.chromedriver().setup();
}
public AbstractViewTest() {
this("");
}
protected AbstractViewTest(String route) {
this.route = route;
}
protected void open() {
open((String[]) null);
}
protected void open(String... parameters) {
String url = getTestURL(parameters);
getDriver().get(url);
waitForDevServer();
}
/**
* Returns the URL to the root of the server, e.g. "http://localhost:8888"
*
* @return the URL to the root
*/
protected String getRootURL() {
return "http://" + getDeploymentHostname() + ":" + getDeploymentPort();
}
protected int getDeploymentPort() {
return SERVER_PORT;
}
protected String getTestURL(String... parameters) {
return getTestURL(getRootURL(), parameters);
}
public static String getTestURL(String rootUrl, String... parameters) {
while (rootUrl.endsWith("/")) {
rootUrl = rootUrl.substring(0, rootUrl.length() - 1);
}
if (parameters != null && parameters.length != 0) {
if (!rootUrl.contains("?")) {
rootUrl += "?";
} else {
rootUrl += "&";
}
rootUrl += Arrays.stream(parameters)
.collect(Collectors.joining("&"));
}
return rootUrl;
}
@Before
public void setup() throws Exception {
ChromeOptions options = new ChromeOptions();
options.addArguments("--headless=new");
setDriver(TestBench.createDriver(new ChromeDriver(options)));
getDriver().get(getURL(route));
// We do screenshot testing, adjust settings to ensure less flakiness
Parameters.setScreenshotComparisonTolerance(0.1);
Parameters.setScreenshotComparisonCursorDetection(true);
testBench().resizeViewPortTo(1280, 900);
Parameters.setMaxScreenshotRetries(3);
Parameters.setScreenshotRetryDelay(1000);
// Wait for frontend compilation complete before testing
waitForDevServer();
}
public void waitForDevServer() {
Object result;
do {
getCommandExecutor().waitForVaadin();
result = getCommandExecutor().executeScript(
"return window.Vaadin && window.Vaadin.Flow && window.Vaadin.Flow.devServerIsNotLoaded;");
} while (Boolean.TRUE.equals(result));
}
/**
* Returns deployment host name concatenated with route.
*
* @return URL to route
*/
private static String getURL(String route) {
return String.format("http://%s:%d/%s", getDeploymentHostname(),
SERVER_PORT, route);
}
/**
* Property set to true when running on a test hub.
*/
private static final String USE_HUB_PROPERTY = "test.use.hub";
/**
* Returns whether we are using a test hub. This means that the starter is
* running tests in Vaadin's CI environment, and uses TestBench to connect
* to the testing hub.
*
* @return whether we are using a test hub
*/
private static boolean isUsingHub() {
return Boolean.TRUE.toString()
.equals(System.getProperty(USE_HUB_PROPERTY));
}
/**
* If running on CI, get the host name from environment variable HOSTNAME
*
* @return the host name
*/
private static String getDeploymentHostname() {
return isUsingHub() ? System.getenv("HOSTNAME") : "localhost";
}
protected void waitForElementPresent(final By by) {
waitUntil(ExpectedConditions.presenceOfElementLocated(by));
}
protected void waitForElementNotPresent(final By by) {
waitUntil(input -> input.findElements(by).isEmpty());
}
protected void waitForElementVisible(final By by) {
waitUntil(ExpectedConditions.visibilityOfElementLocated(by));
}
}