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.gradle
that corresponds to the project that builds the WAR file
version: '3'
services:
tomcat:
image: brightspot/tomcat:9-jdk11
volumes:
- .:/code:cached
- storage-data:/servers/tomcat/storage
environment:
- ROOT_WAR=/code/web/build/libs/< PLACE_PROJECT_NAME >-1.0.0-SNAPSHOT.war
- CONTEXT_PROPERTIES=/code/docker-context.properties
- CONTEXT_PROPERTIES_OVERRIDES=/code/docker-context.cypress.properties
- LOGGING_PROPERTIES=/code/docker-logging.properties
- SOLR_URL=http://solr:8080/solr/collection1
solr:
image: brightspot/solr:8
apache:
image: brightspot/apache:2-dims3
volumes:
- storage-data:/var/www/localhost/htdocs/storage
mysql:
image: brightspot/mysql:mysql5.6
cypress:
image: cypress
build:
context: ./cypress
dockerfile: Dockerfile
environment:
- CYPRESS_baseUrl=http://apache
entrypoint: npm run docker-test
volumes:
- ./cypress/cypress:/app/cypress
- ./cypress/cypress/config/cypress.config.js:/app/cypress.config.js
- ./cypress/reporter-config.json:/app/reporter-config.json
volumes:
storage-data:
Cypress Tests Java
Copy and paste contents into CypressTests.java
file in /web/src/intTest/java/brightspot/test
package brightspot.test;
import java.io.File;
import java.io.IOException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.databind.ObjectMapper;
import junit.framework.AssertionFailedError;
import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory;
import org.testcontainers.containers.DockerComposeContainer;
public class CypressTests {
// TODO: pull these from runtime properties
/* All paths are relative to the build.gradle file that invoked this test */
String dockerComposePath = "../docker-compose.cypress.yml";
String mochawesomeReportsPath = "../cypress/cypress/reports/mochawesome";
int quietLogTimeout = 60_000;
String cypressServiceName = "cypress";
String tomcatServiceName = "tomcat";
private final ObjectMapper objectMapper = new ObjectMapper();
@TestFactory
Collection<DynamicTest> doCypressTests() throws IOException {
File dockerComposeFile = new File(dockerComposePath);
try (DockerComposeContainer<?> container = new DockerComposeContainer<>(dockerComposeFile)) {
// Print cypress logs to stdout
AtomicLong lastLogMessage = new AtomicLong(new Date().getTime());
AtomicBoolean done = new AtomicBoolean(false);
container.withLogConsumer(cypressServiceName, outputFrame -> {
lastLogMessage.set(new Date().getTime());
String line = outputFrame.getUtf8String();
System.out.print(line);
if (line != null && line.isEmpty()) {
done.set(true);
}
});
// Print tomcat logs to stderr
container.withLogConsumer(tomcatServiceName, outputFrame -> {
lastLogMessage.set(new Date().getTime());
System.err.print(outputFrame.getUtf8String());
});
// Build the container before starting if necessary
container.withBuild(true);
container.start();
// If the log is quiet for long enough, exit
while (!done.get()) {
if (lastLogMessage.get() <= (new Date().getTime() - quietLogTimeout)) {
throw new InterruptedException("Exiting after Quiet Timeout!");
}
Thread.sleep(5_000);
}
List<DynamicTest> tests = new ArrayList<>();
if (mochawesomeReportsPath != null && new File(mochawesomeReportsPath).exists()) {
try (DirectoryStream<Path> paths = Files.newDirectoryStream(Paths.get(mochawesomeReportsPath), "*.json")) {
for (Path path : paths) {
MochawesomeSpecRunReport specRunReport = objectMapper.readValue(path.toFile(), MochawesomeSpecRunReport.class);
for (Result result : specRunReport.getResults()) {
for (Suite suite : result.getSuites()) {
for (SuiteTest test : suite.getTests()) {
DynamicTest t = DynamicTest.dynamicTest(suite.title + ": " + test.title, () -> {
if (test.isFail()) {
SuiteTestError error = test.getErr();
throw new AssertionFailedError(error.getMessage()
+ '\n' + test.getCode()
+ '\n' + error.getEstack()
+ '\n' + error.getDiff()
);
}
});
tests.add(t);
}
}
}
}
}
}
return tests;
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
private static class MochawesomeSpecRunReport {
private Stats stats;
private List<Result> results;
public Stats getStats() {
return stats;
}
public void setStats(Stats stats) {
this.stats = stats;
}
public List<Result> getResults() {
return results;
}
public void setResults(List<Result> results) {
this.results = results;
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
private static class Stats {
private int tests;
private int passes;
private int failures;
public int getTests() {
return tests;
}
public void setTests(int tests) {
this.tests = tests;
}
public int getPasses() {
return passes;
}
public void setPasses(int passes) {
this.passes = passes;
}
public int getFailures() {
return failures;
}
public void setFailures(int failures) {
this.failures = failures;
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
private static class Result {
private List<Suite> suites;
public List<Suite> getSuites() {
return suites;
}
public void setSuites(List<Suite> suites) {
this.suites = suites;
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
private static class Suite {
private String title;
private List<SuiteTest> tests;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public List<SuiteTest> getTests() {
return tests;
}
public void setTests(List<SuiteTest> tests) {
this.tests = tests;
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
private static class SuiteTest {
private String title;
private String code;
private boolean fail;
private SuiteTestError err;
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public boolean isFail() {
return fail;
}
public void setFail(boolean fail) {
this.fail = fail;
}
public SuiteTestError getErr() {
return err;
}
public void setErr(SuiteTestError err) {
this.err = err;
}
}
@JsonIgnoreProperties(ignoreUnknown = true)
private static class SuiteTestError {
private String message;
private String estack;
private String diff;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getEstack() {
return estack;
}
public void setEstack(String estack) {
this.estack = estack;
}
public String getDiff() {
return diff;
}
public void setDiff(String diff) {
this.diff = diff;
}
}
}