aboutsummaryrefslogtreecommitdiffstats
path: root/test/servlet-containers/generic-tests/src/main/java/com/vaadin/tests/integration/AbstractIntegrationTest.java
blob: 90bec7ae4860d1a837556b11f82b429b0eb65575 (plain)
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
package com.vaadin.tests.integration;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Locale;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.imageio.ImageIO;

import org.junit.After;
import org.junit.runner.RunWith;

import com.vaadin.testbench.annotations.RunLocally;
import com.vaadin.testbench.elements.UIElement;
import com.vaadin.testbench.parallel.Browser;
import com.vaadin.testbench.parallel.ParallelRunner;
import com.vaadin.testbench.parallel.ParallelTest;
import com.vaadin.testbench.parallel.TestNameSuffix;
import com.vaadin.testbench.screenshot.ImageFileUtil;

@RunLocally(Browser.PHANTOMJS)
@RunWith(ParallelRunner.class)
@TestNameSuffix(property = "server-name")
public abstract class AbstractIntegrationTest extends ParallelTest {

    /**
     * Height of the screenshots we want to capture
     */
    private static final int SCREENSHOT_HEIGHT = 850;

    /**
     * Width of the screenshots we want to capture
     */
    private static final int SCREENSHOT_WIDTH = 1500;

    private boolean screenshotErrors;

    @Override
    public void setup() throws Exception {
        super.setup();

        testBench().resizeViewPortTo(SCREENSHOT_WIDTH, SCREENSHOT_HEIGHT);

        openTestURL(getParameters());
    }

    protected void openTestURL(Stream<String> parameters) {
        String url = getDeploymentURL() + getContextPath() + getTestPath() + "?"
                + parameters.collect(Collectors.joining("&"));

        driver.get(url);

        if (!isElementPresent(UIElement.class)) {
            waitUntil(e -> isElementPresent(UIElement.class), 10);
        }
    }

    protected Stream<String> getParameters() {
        return Stream.of("restartApplication");
    }

    /**
     * Returns a path where the test UI is found.
     *
     * @return path for test
     */
    protected abstract String getTestPath();

    private String getDeploymentURL() {
        String deploymentUrl = System.getProperty("deployment.url");
        if (deploymentUrl == null || deploymentUrl.isEmpty()) {
            // Default to http://localhost:8080
            return "http://localhost:8080";
        }
        return deploymentUrl;
    }

    protected void compareScreen(String identifier) throws IOException {
        String refFileName = identifier + "-"
                + getDesiredCapabilities().getBrowserName().toLowerCase(Locale.ROOT)
                + ".png";
        String errorFileName = identifier + "-"
                + getDesiredCapabilities().getBrowserName().toLowerCase(Locale.ROOT) + "-"
                + System.getProperty("server-name") + "["
                + getClass().getSimpleName() + "].png";
        File referenceFile = ImageFileUtil
                .getReferenceScreenshotFile(refFileName);
        try {
            BufferedImage reference = ImageIO.read(referenceFile);
            if (testBench().compareScreen(reference, errorFileName)) {
                return;
            }
        } catch (IOException e) {
            Logger.getLogger(getClass().getName()).warning(
                    "Missing screenshot reference: " + referenceFile.getPath());
        }
        screenshotErrors = true;
    }

    @After
    public void teardown() {
        if (screenshotErrors) {
            throw new RuntimeException("Screenshots failed.");
        }
    }

    /**
     * Returns the deployment context path with a leading slash. If not provided
     * through {@code deployment.context.path} system property, will default to
     * {@code /demo}.
     *
     * @return deployment context path
     */
    protected String getContextPath() {
        String contextPath = System.getProperty("deployment.context.path");
        if (contextPath == null || contextPath.isEmpty()) {
            // Default to /demo
            return "/demo";
        }
        return contextPath;
    }
}