aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/test/java/com/vaadin/tests/tb3/ScreenshotTB3Test.java
blob: 6a219065274f085f3f435b29258474efb17f154f (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
package com.vaadin.tests.tb3;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.openqa.selenium.HasCapabilities;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import com.vaadin.testbench.TestBenchElement;
import com.vaadin.testbench.screenshot.ImageFileUtil;

/**
 * Base class which provides functionality for tests which use the automatic
 * screenshot comparison function.
 *
 * @author Vaadin Ltd
 */
public abstract class ScreenshotTB3Test extends AbstractTB3Test {

    /**
     * Contains a list of screenshot identifiers for which
     * {@link #compareScreen(String)} has failed during the test
     */
    private List<String> screenshotFailures = new ArrayList<>();

    @Override
    public void setDriver(WebDriver driver) {
        super.setDriver(driver);

        // After setting up the driver, we can set the reference name generator.
        String testClassName = getClass().getSimpleName();
        testBench().setReferenceNameGenerator((identifier, capabilities) -> {
            // Make sure error screenshot directory exists.
            String browserFolder = capabilities.getBrowserName().toLowerCase()
                    .replaceAll(" ", "");
            Paths.get(ImageFileUtil.getScreenshotErrorDirectory(),
                    browserFolder).toFile().mkdirs();
            return Paths
                    .get(browserFolder,
                            String.format("%s-%s%s", testClassName,
                                    testName.getMethodName().replace('[', '_')
                                            .replace(']', '_'),
                                    identifier))
                    .toString();
        });
    }

    /**
     * Grabs a screenshot and compares with the reference image with the given
     * identifier. Supports alternative references and will succeed if the
     * screenshot matches at least one of the references.
     *
     * In case of a failed comparison this method stores the grabbed screenshots
     * in the error directory as defined by
     * {@link #getScreenshotErrorDirectory()}. It will also generate a html file
     * in the same directory, comparing the screenshot with the first found
     * reference.
     *
     * @param identifier
     * @throws IOException
     */
    protected void compareScreen(String identifier) throws IOException {
        if (!testBench().compareScreen(identifier)) {
            screenshotFailures.add(testBench().getReferenceNameGenerator()
                    .generateName(identifier,
                            ((HasCapabilities) getDriver()).getCapabilities()));
        }
    }

    protected void compareScreen(WebElement element, String identifier)
            throws IOException {
        if (!((TestBenchElement) element).compareScreen(identifier)) {
            screenshotFailures.add(testBench().getReferenceNameGenerator()
                    .generateName(identifier,
                            ((HasCapabilities) getDriver()).getCapabilities()));
        }
    }

    /**
     * Checks if any screenshot comparisons failures occurred during the test
     * and combines all comparison errors into one exception
     *
     * @throws IOException
     *             If there were failures during the test
     */
    @After
    public void checkCompareFailures() throws IOException {
        if (screenshotFailures != null && !screenshotFailures.isEmpty()) {
            throw new IOException(
                    "The following screenshots did not match the reference: "
                            + screenshotFailures.toString());
        }
    }
}