1 package com.vaadin.tests.integration;
3 import java.awt.image.BufferedImage;
5 import java.io.IOException;
6 import java.util.Locale;
7 import java.util.logging.Logger;
8 import java.util.stream.Collectors;
9 import java.util.stream.Stream;
11 import javax.imageio.ImageIO;
13 import org.junit.After;
14 import org.junit.runner.RunWith;
15 import org.openqa.selenium.support.ui.ExpectedCondition;
16 import org.openqa.selenium.support.ui.WebDriverWait;
18 import com.vaadin.testbench.annotations.RunLocally;
19 import com.vaadin.testbench.elements.UIElement;
20 import com.vaadin.testbench.parallel.Browser;
21 import com.vaadin.testbench.parallel.ParallelRunner;
22 import com.vaadin.testbench.parallel.ParallelTest;
23 import com.vaadin.testbench.parallel.TestNameSuffix;
24 import com.vaadin.testbench.screenshot.ImageFileUtil;
26 @RunLocally(Browser.PHANTOMJS)
27 @RunWith(ParallelRunner.class)
28 @TestNameSuffix(property = "server-name")
29 public abstract class AbstractIntegrationTest extends ParallelTest {
32 * Height of the screenshots we want to capture
34 private static final int SCREENSHOT_HEIGHT = 850;
37 * Width of the screenshots we want to capture
39 private static final int SCREENSHOT_WIDTH = 1500;
41 private boolean screenshotErrors;
44 public void setup() throws Exception {
47 testBench().resizeViewPortTo(SCREENSHOT_WIDTH, SCREENSHOT_HEIGHT);
52 private void openTestURL() {
53 String url = getDeploymentURL() + getContextPath() + getTestPath() + "?"
54 + getParameters().collect(Collectors.joining("&"));
58 if (!isElementPresent(UIElement.class)) {
59 waitUntil(e -> isElementPresent(UIElement.class), 10);
63 protected Stream<String> getParameters() {
64 return Stream.of("restartApplication");
68 * Returns a path where the test UI is found.
70 * @return path for test
72 protected abstract String getTestPath();
74 private String getDeploymentURL() {
75 String deploymentUrl = System.getProperty("deployment.url");
76 if (deploymentUrl == null || deploymentUrl.isEmpty()) {
77 // Default to http://localhost:8080
78 return "http://localhost:8080";
83 protected void compareScreen(String identifier) throws IOException {
84 String refFileName = identifier + "-"
85 + getDesiredCapabilities().getBrowserName().toLowerCase(Locale.ROOT)
87 String errorFileName = identifier + "-"
88 + getDesiredCapabilities().getBrowserName().toLowerCase(Locale.ROOT) + "-"
89 + System.getProperty("server-name") + "["
90 + getClass().getSimpleName() + "].png";
91 File referenceFile = ImageFileUtil
92 .getReferenceScreenshotFile(refFileName);
94 BufferedImage reference = ImageIO.read(referenceFile);
95 if (testBench().compareScreen(reference, errorFileName)) {
98 } catch (IOException e) {
99 Logger.getLogger(getClass().getName()).warning(
100 "Missing screenshot reference: " + referenceFile.getPath());
102 screenshotErrors = true;
106 public void teardown() {
107 if (screenshotErrors) {
108 throw new RuntimeException("Screenshots failed.");
113 * Waits the given number of seconds for the given condition to become true.
115 * {@link #waitUntil(ExpectedConditions.textToBePresentInElement(by, text))}
118 * the condition to wait for to become true
120 protected <T> void waitUntil(ExpectedCondition<T> condition,
121 long timeoutInSeconds) {
122 new WebDriverWait(driver, timeoutInSeconds).until(condition);
126 * Returns the deployment context path with a leading slash. If not provided
127 * through {@code deployment.context.path} system property, will default to
130 * @return deployment context path
132 protected String getContextPath() {
133 String contextPath = System.getProperty("deployment.context.path");
134 if (contextPath == null || contextPath.isEmpty()) {