]> source.dussan.org Git - vaadin-framework.git/blob
89ca8f382dde6968cdf5967c98867d00ba2c599c
[vaadin-framework.git] /
1 package com.vaadin.tests.integration;
2
3 import java.awt.image.BufferedImage;
4 import java.io.File;
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;
10
11 import javax.imageio.ImageIO;
12
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;
17
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;
25
26 @RunLocally(Browser.PHANTOMJS)
27 @RunWith(ParallelRunner.class)
28 @TestNameSuffix(property = "server-name")
29 public abstract class AbstractIntegrationTest extends ParallelTest {
30
31     /**
32      * Height of the screenshots we want to capture
33      */
34     private static final int SCREENSHOT_HEIGHT = 850;
35
36     /**
37      * Width of the screenshots we want to capture
38      */
39     private static final int SCREENSHOT_WIDTH = 1500;
40
41     private boolean screenshotErrors;
42
43     @Override
44     public void setup() throws Exception {
45         super.setup();
46
47         testBench().resizeViewPortTo(SCREENSHOT_WIDTH, SCREENSHOT_HEIGHT);
48
49         openTestURL();
50     }
51
52     private void openTestURL() {
53         String url = getDeploymentURL() + getContextPath() + getTestPath() + "?"
54                 + getParameters().collect(Collectors.joining("&"));
55
56         driver.get(url);
57
58         if (!isElementPresent(UIElement.class)) {
59             waitUntil(e -> isElementPresent(UIElement.class), 10);
60         }
61     }
62
63     protected Stream<String> getParameters() {
64         return Stream.of("restartApplication");
65     }
66
67     /**
68      * Returns a path where the test UI is found.
69      *
70      * @return path for test
71      */
72     protected abstract String getTestPath();
73
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";
79         }
80         return deploymentUrl;
81     }
82
83     protected void compareScreen(String identifier) throws IOException {
84         String refFileName = identifier + "-"
85                 + getDesiredCapabilities().getBrowserName().toLowerCase(Locale.ROOT)
86                 + ".png";
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);
93         try {
94             BufferedImage reference = ImageIO.read(referenceFile);
95             if (testBench().compareScreen(reference, errorFileName)) {
96                 return;
97             }
98         } catch (IOException e) {
99             Logger.getLogger(getClass().getName()).warning(
100                     "Missing screenshot reference: " + referenceFile.getPath());
101         }
102         screenshotErrors = true;
103     }
104
105     @After
106     public void teardown() {
107         if (screenshotErrors) {
108             throw new RuntimeException("Screenshots failed.");
109         }
110     }
111
112     /**
113      * Waits the given number of seconds for the given condition to become true.
114      * Use e.g. as
115      * {@link #waitUntil(ExpectedConditions.textToBePresentInElement(by, text))}
116      *
117      * @param condition
118      *            the condition to wait for to become true
119      */
120     protected <T> void waitUntil(ExpectedCondition<T> condition,
121             long timeoutInSeconds) {
122         new WebDriverWait(driver, timeoutInSeconds).until(condition);
123     }
124
125     /**
126      * Returns the deployment context path with a leading slash. If not provided
127      * through {@code deployment.context.path} system property, will default to
128      * {@code /demo}.
129      *
130      * @return deployment context path
131      */
132     protected String getContextPath() {
133         String contextPath = System.getProperty("deployment.context.path");
134         if (contextPath == null || contextPath.isEmpty()) {
135             // Default to /demo
136             return "/demo";
137         }
138         return contextPath;
139     }
140 }