diff --git a/pom.xml b/pom.xml index 1b207e9..14b464f 100644 --- a/pom.xml +++ b/pom.xml @@ -103,6 +103,17 @@ 3.8.1 test + + org.springframework.boot + spring-boot-starter-test + test + + + org.junit.vintage + junit-vintage-engine + + + org.junit.jupiter @@ -172,6 +183,21 @@ maven-surefire-plugin 2.22.2 + + org.apache.maven.plugins + maven-failsafe-plugin + 2.22.2 + + + + integration-test + + + + + false + + org.jacoco jacoco-maven-plugin diff --git a/src/test/java/edu/kit/typicalc/view/AbstractIT.java b/src/test/java/edu/kit/typicalc/view/AbstractIT.java new file mode 100644 index 0000000..117377f --- /dev/null +++ b/src/test/java/edu/kit/typicalc/view/AbstractIT.java @@ -0,0 +1,27 @@ +package edu.kit.typicalc.view; + +import org.junit.Before; +import org.junit.Rule; + +import com.vaadin.testbench.IPAddress; +import com.vaadin.testbench.ScreenshotOnFailureRule; +import com.vaadin.testbench.TestBenchTestCase; +import org.openqa.selenium.firefox.FirefoxDriver; + +/** + * Base class for all our integration tests, allowing us to change the applicable driver, + * test URL or other configurations in one place. + */ +public abstract class AbstractIT extends TestBenchTestCase { + + @Rule + public ScreenshotOnFailureRule rule = new ScreenshotOnFailureRule(this, + true); + + @Before + public void setUp() { + setDriver(new FirefoxDriver()); + getDriver().get("http://127.0.0.1:8080"); + } + +} diff --git a/src/test/java/edu/kit/typicalc/view/ScreenshotIT.java b/src/test/java/edu/kit/typicalc/view/ScreenshotIT.java new file mode 100644 index 0000000..a3528d2 --- /dev/null +++ b/src/test/java/edu/kit/typicalc/view/ScreenshotIT.java @@ -0,0 +1,93 @@ +package edu.kit.typicalc.view; + +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.IOException; + +import edu.kit.typicalc.view.main.MainViewImpl; +import org.junit.Test; +import org.openqa.selenium.HasCapabilities; + +import com.vaadin.testbench.Parameters; +import com.vaadin.testbench.commands.TestBenchCommandExecutor; + +/** + * This example contains usage examples of screenshot comparison feature. + *

+ */ +public class ScreenshotIT extends AbstractIT { + + /** + * We'll want to perform some additional setup functions, so we override the + * setUp() method. + */ + @Override + public void setUp() { + super.setUp(); + + // Set a fixed viewport size so the screenshot is always the same + // resolution + testBench().resizeViewPortTo(1000, 500); + + // Define the directory for reference screenshots and for error files + Parameters.setScreenshotReferenceDirectory("src/test/resources/screenshots"); + Parameters.setScreenshotErrorDirectory("target/screenshot_errors"); + } + + @Test + public void initialView() throws Exception { + // Change this calculation after running the test once to see how errors + // in screenshots are verified. + // The output is placed in target/screenshot_errors + + generateReferenceIfNotFound("initialView"); + + // Compare screen with reference image with id "oneplustwo" from the + // reference image directory. Reference image filenames also contain + // browser, version and platform. + assertTrue( + "Screenshot comparison for 'initialView' failed, see " + + Parameters.getScreenshotErrorDirectory() + + " for error images", + testBench().compareScreen("initialView")); + } + + /** + * Generates a reference screenshot if no reference exists. + *

+ * This method only exists for demonstration purposes. Normally you should + * perform this task manually after verifying that the screenshots look + * correct. + * + * @param referenceId + * the id of the reference image + * @throws IOException + */ + private void generateReferenceIfNotFound(String referenceId) + throws IOException { + String refName = ((TestBenchCommandExecutor) testBench()) + .getReferenceNameGenerator().generateName(referenceId, + ((HasCapabilities) getDriver()).getCapabilities()); + File referenceFile = new File( + Parameters.getScreenshotReferenceDirectory(), refName + ".png"); + if (referenceFile.exists()) { + return; + } + + if (!referenceFile.getParentFile().exists()) { + referenceFile.getParentFile().mkdirs(); + } + + File errorFile = new File(Parameters.getScreenshotErrorDirectory(), + referenceFile.getName()); + + // Take a screenshot and move it to the reference location + testBench().compareScreen(referenceId); + errorFile.renameTo(referenceFile); + + System.out.println("Created new reference file in " + referenceFile); + + } + +} diff --git a/src/test/resources/checkstyle-suppressions.xml b/src/test/resources/checkstyle-suppressions.xml index 0ffc556..0aff2fd 100644 --- a/src/test/resources/checkstyle-suppressions.xml +++ b/src/test/resources/checkstyle-suppressions.xml @@ -5,5 +5,6 @@ +