Cypress Testing Files
This page contains example files to import and modify to implement Cypress in your project.
Docker Compose Cypress
Copy and paste contents into docker-compose.cypress.yml file at the project root. And then make these changes to tailor for your project:
- Line 9: Replace
< PLACE_PROJECT_NAME >with the project name insettings.gradlethat corresponds to the project that builds the WAR file
1version: '3'2services:3tomcat:4image: brightspot/tomcat:9-jdk115volumes:6- .:/code:cached7- storage-data:/servers/tomcat/storage8environment:9- ROOT_WAR=/code/web/build/libs/< PLACE_PROJECT_NAME >-1.0.0-SNAPSHOT.war10- CONTEXT_PROPERTIES=/code/docker-context.properties11- CONTEXT_PROPERTIES_OVERRIDES=/code/docker-context.cypress.properties12- LOGGING_PROPERTIES=/code/docker-logging.properties13- SOLR_URL=http://solr:8080/solr/collection114solr:15image: brightspot/solr:816apache:17image: brightspot/apache:2-dims318volumes:19- storage-data:/var/www/localhost/htdocs/storage20mysql:21image: brightspot/mysql:mysql5.622cypress:23image: cypress24build:25context: ./cypress26dockerfile: Dockerfile27environment:28- CYPRESS_baseUrl=http://apache29entrypoint: npm run docker-test30volumes:31- ./cypress/cypress:/app/cypress32- ./cypress/cypress/config/cypress.config.js:/app/cypress.config.js33- ./cypress/reporter-config.json:/app/reporter-config.json34volumes:35storage-data:
Cypress Tests Java
Copy and paste contents into CypressTests.java file in /web/src/intTest/java/brightspot/test
1package brightspot.test;23import java.io.File;4import java.io.IOException;5import java.nio.file.DirectoryStream;6import java.nio.file.Files;7import java.nio.file.Path;8import java.nio.file.Paths;9import java.util.ArrayList;10import java.util.Collection;11import java.util.Date;12import java.util.List;13import java.util.concurrent.atomic.AtomicBoolean;14import java.util.concurrent.atomic.AtomicLong;1516import com.fasterxml.jackson.annotation.JsonIgnoreProperties;17import com.fasterxml.jackson.databind.ObjectMapper;18import junit.framework.AssertionFailedError;19import org.junit.jupiter.api.DynamicTest;20import org.junit.jupiter.api.TestFactory;21import org.testcontainers.containers.DockerComposeContainer;2223public class CypressTests {2425// TODO: pull these from runtime properties26/* All paths are relative to the build.gradle file that invoked this test */27String dockerComposePath = "../docker-compose.cypress.yml";28String mochawesomeReportsPath = "../cypress/cypress/reports/mochawesome";29int quietLogTimeout = 60_000;30String cypressServiceName = "cypress";31String tomcatServiceName = "tomcat";32private final ObjectMapper objectMapper = new ObjectMapper();3334@TestFactory35Collection<DynamicTest> doCypressTests() throws IOException {3637File dockerComposeFile = new File(dockerComposePath);38try (DockerComposeContainer<?> container = new DockerComposeContainer<>(dockerComposeFile)) {3940// Print cypress logs to stdout41AtomicLong lastLogMessage = new AtomicLong(new Date().getTime());42AtomicBoolean done = new AtomicBoolean(false);43container.withLogConsumer(cypressServiceName, outputFrame -> {44lastLogMessage.set(new Date().getTime());45String line = outputFrame.getUtf8String();46System.out.print(line);47if (line != null && line.isEmpty()) {48done.set(true);49}50});5152// Print tomcat logs to stderr53container.withLogConsumer(tomcatServiceName, outputFrame -> {54lastLogMessage.set(new Date().getTime());55System.err.print(outputFrame.getUtf8String());56});5758// Build the container before starting if necessary59container.withBuild(true);60container.start();6162// If the log is quiet for long enough, exit63while (!done.get()) {64if (lastLogMessage.get() <= (new Date().getTime() - quietLogTimeout)) {65throw new InterruptedException("Exiting after Quiet Timeout!");66}67Thread.sleep(5_000);68}69List<DynamicTest> tests = new ArrayList<>();7071if (mochawesomeReportsPath != null && new File(mochawesomeReportsPath).exists()) {72try (DirectoryStream<Path> paths = Files.newDirectoryStream(Paths.get(mochawesomeReportsPath), "*.json")) {73for (Path path : paths) {74MochawesomeSpecRunReport specRunReport = objectMapper.readValue(path.toFile(), MochawesomeSpecRunReport.class);75for (Result result : specRunReport.getResults()) {76for (Suite suite : result.getSuites()) {77for (SuiteTest test : suite.getTests()) {78DynamicTest t = DynamicTest.dynamicTest(suite.title + ": " + test.title, () -> {79if (test.isFail()) {80SuiteTestError error = test.getErr();81throw new AssertionFailedError(error.getMessage()82+ '\n' + test.getCode()83+ '\n' + error.getEstack()84+ '\n' + error.getDiff()85);86}87});88tests.add(t);89}90}91}92}93}94}9596return tests;97} catch (InterruptedException e) {98throw new RuntimeException(e);99}100}101102@JsonIgnoreProperties(ignoreUnknown = true)103private static class MochawesomeSpecRunReport {104105private Stats stats;106private List<Result> results;107108public Stats getStats() {109return stats;110}111112public void setStats(Stats stats) {113this.stats = stats;114}115116public List<Result> getResults() {117return results;118}119120public void setResults(List<Result> results) {121this.results = results;122}123124}125126@JsonIgnoreProperties(ignoreUnknown = true)127private static class Stats {128private int tests;129private int passes;130private int failures;131132public int getTests() {133return tests;134}135136public void setTests(int tests) {137this.tests = tests;138}139140public int getPasses() {141return passes;142}143144public void setPasses(int passes) {145this.passes = passes;146}147148public int getFailures() {149return failures;150}151152public void setFailures(int failures) {153this.failures = failures;154}155}156157@JsonIgnoreProperties(ignoreUnknown = true)158private static class Result {159private List<Suite> suites;160161public List<Suite> getSuites() {162return suites;163}164165public void setSuites(List<Suite> suites) {166this.suites = suites;167}168}169170@JsonIgnoreProperties(ignoreUnknown = true)171private static class Suite {172private String title;173private List<SuiteTest> tests;174175public String getTitle() {176return title;177}178179public void setTitle(String title) {180this.title = title;181}182183public List<SuiteTest> getTests() {184return tests;185}186187public void setTests(List<SuiteTest> tests) {188this.tests = tests;189}190}191192@JsonIgnoreProperties(ignoreUnknown = true)193private static class SuiteTest {194private String title;195private String code;196private boolean fail;197private SuiteTestError err;198199public String getTitle() {200return title;201}202203public void setTitle(String title) {204this.title = title;205}206207public String getCode() {208return code;209}210211public void setCode(String code) {212this.code = code;213}214215public boolean isFail() {216return fail;217}218219public void setFail(boolean fail) {220this.fail = fail;221}222223public SuiteTestError getErr() {224return err;225}226227public void setErr(SuiteTestError err) {228this.err = err;229}230}231232@JsonIgnoreProperties(ignoreUnknown = true)233private static class SuiteTestError {234private String message;235private String estack;236private String diff;237238public String getMessage() {239return message;240}241242public void setMessage(String message) {243this.message = message;244}245246public String getEstack() {247return estack;248}249250public void setEstack(String estack) {251this.estack = estack;252}253254public String getDiff() {255return diff;256}257258public void setDiff(String diff) {259this.diff = diff;260}261}262}