Skip to main content

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:

  1. Line 9: Replace < PLACE_PROJECT_NAME > with the project name in settings.gradle that corresponds to the project that builds the WAR file
1
version: '3'
2
services:
3
tomcat:
4
image: brightspot/tomcat:9-jdk11
5
volumes:
6
- .:/code:cached
7
- storage-data:/servers/tomcat/storage
8
environment:
9
- ROOT_WAR=/code/web/build/libs/< PLACE_PROJECT_NAME >-1.0.0-SNAPSHOT.war
10
- CONTEXT_PROPERTIES=/code/docker-context.properties
11
- CONTEXT_PROPERTIES_OVERRIDES=/code/docker-context.cypress.properties
12
- LOGGING_PROPERTIES=/code/docker-logging.properties
13
- SOLR_URL=http://solr:8080/solr/collection1
14
solr:
15
image: brightspot/solr:8
16
apache:
17
image: brightspot/apache:2-dims3
18
volumes:
19
- storage-data:/var/www/localhost/htdocs/storage
20
mysql:
21
image: brightspot/mysql:mysql5.6
22
cypress:
23
image: cypress
24
build:
25
context: ./cypress
26
dockerfile: Dockerfile
27
environment:
28
- CYPRESS_baseUrl=http://apache
29
entrypoint: npm run docker-test
30
volumes:
31
- ./cypress/cypress:/app/cypress
32
- ./cypress/cypress/config/cypress.config.js:/app/cypress.config.js
33
- ./cypress/reporter-config.json:/app/reporter-config.json
34
volumes:
35
storage-data:

Cypress Tests Java​

Copy and paste contents into CypressTests.java file in /web/src/intTest/java/brightspot/test

1
package brightspot.test;
2
3
import java.io.File;
4
import java.io.IOException;
5
import java.nio.file.DirectoryStream;
6
import java.nio.file.Files;
7
import java.nio.file.Path;
8
import java.nio.file.Paths;
9
import java.util.ArrayList;
10
import java.util.Collection;
11
import java.util.Date;
12
import java.util.List;
13
import java.util.concurrent.atomic.AtomicBoolean;
14
import java.util.concurrent.atomic.AtomicLong;
15
16
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
17
import com.fasterxml.jackson.databind.ObjectMapper;
18
import junit.framework.AssertionFailedError;
19
import org.junit.jupiter.api.DynamicTest;
20
import org.junit.jupiter.api.TestFactory;
21
import org.testcontainers.containers.DockerComposeContainer;
22
23
public class CypressTests {
24
25
// TODO: pull these from runtime properties
26
/* All paths are relative to the build.gradle file that invoked this test */
27
String dockerComposePath = "../docker-compose.cypress.yml";
28
String mochawesomeReportsPath = "../cypress/cypress/reports/mochawesome";
29
int quietLogTimeout = 60_000;
30
String cypressServiceName = "cypress";
31
String tomcatServiceName = "tomcat";
32
private final ObjectMapper objectMapper = new ObjectMapper();
33
34
@TestFactory
35
Collection<DynamicTest> doCypressTests() throws IOException {
36
37
File dockerComposeFile = new File(dockerComposePath);
38
try (DockerComposeContainer<?> container = new DockerComposeContainer<>(dockerComposeFile)) {
39
40
// Print cypress logs to stdout
41
AtomicLong lastLogMessage = new AtomicLong(new Date().getTime());
42
AtomicBoolean done = new AtomicBoolean(false);
43
container.withLogConsumer(cypressServiceName, outputFrame -> {
44
lastLogMessage.set(new Date().getTime());
45
String line = outputFrame.getUtf8String();
46
System.out.print(line);
47
if (line != null && line.isEmpty()) {
48
done.set(true);
49
}
50
});
51
52
// Print tomcat logs to stderr
53
container.withLogConsumer(tomcatServiceName, outputFrame -> {
54
lastLogMessage.set(new Date().getTime());
55
System.err.print(outputFrame.getUtf8String());
56
});
57
58
// Build the container before starting if necessary
59
container.withBuild(true);
60
container.start();
61
62
// If the log is quiet for long enough, exit
63
while (!done.get()) {
64
if (lastLogMessage.get() <= (new Date().getTime() - quietLogTimeout)) {
65
throw new InterruptedException("Exiting after Quiet Timeout!");
66
}
67
Thread.sleep(5_000);
68
}
69
List<DynamicTest> tests = new ArrayList<>();
70
71
if (mochawesomeReportsPath != null && new File(mochawesomeReportsPath).exists()) {
72
try (DirectoryStream<Path> paths = Files.newDirectoryStream(Paths.get(mochawesomeReportsPath), "*.json")) {
73
for (Path path : paths) {
74
MochawesomeSpecRunReport specRunReport = objectMapper.readValue(path.toFile(), MochawesomeSpecRunReport.class);
75
for (Result result : specRunReport.getResults()) {
76
for (Suite suite : result.getSuites()) {
77
for (SuiteTest test : suite.getTests()) {
78
DynamicTest t = DynamicTest.dynamicTest(suite.title + ": " + test.title, () -> {
79
if (test.isFail()) {
80
SuiteTestError error = test.getErr();
81
throw new AssertionFailedError(error.getMessage()
82
+ '\n' + test.getCode()
83
+ '\n' + error.getEstack()
84
+ '\n' + error.getDiff()
85
);
86
}
87
});
88
tests.add(t);
89
}
90
}
91
}
92
}
93
}
94
}
95
96
return tests;
97
} catch (InterruptedException e) {
98
throw new RuntimeException(e);
99
}
100
}
101
102
@JsonIgnoreProperties(ignoreUnknown = true)
103
private static class MochawesomeSpecRunReport {
104
105
private Stats stats;
106
private List<Result> results;
107
108
public Stats getStats() {
109
return stats;
110
}
111
112
public void setStats(Stats stats) {
113
this.stats = stats;
114
}
115
116
public List<Result> getResults() {
117
return results;
118
}
119
120
public void setResults(List<Result> results) {
121
this.results = results;
122
}
123
124
}
125
126
@JsonIgnoreProperties(ignoreUnknown = true)
127
private static class Stats {
128
private int tests;
129
private int passes;
130
private int failures;
131
132
public int getTests() {
133
return tests;
134
}
135
136
public void setTests(int tests) {
137
this.tests = tests;
138
}
139
140
public int getPasses() {
141
return passes;
142
}
143
144
public void setPasses(int passes) {
145
this.passes = passes;
146
}
147
148
public int getFailures() {
149
return failures;
150
}
151
152
public void setFailures(int failures) {
153
this.failures = failures;
154
}
155
}
156
157
@JsonIgnoreProperties(ignoreUnknown = true)
158
private static class Result {
159
private List<Suite> suites;
160
161
public List<Suite> getSuites() {
162
return suites;
163
}
164
165
public void setSuites(List<Suite> suites) {
166
this.suites = suites;
167
}
168
}
169
170
@JsonIgnoreProperties(ignoreUnknown = true)
171
private static class Suite {
172
private String title;
173
private List<SuiteTest> tests;
174
175
public String getTitle() {
176
return title;
177
}
178
179
public void setTitle(String title) {
180
this.title = title;
181
}
182
183
public List<SuiteTest> getTests() {
184
return tests;
185
}
186
187
public void setTests(List<SuiteTest> tests) {
188
this.tests = tests;
189
}
190
}
191
192
@JsonIgnoreProperties(ignoreUnknown = true)
193
private static class SuiteTest {
194
private String title;
195
private String code;
196
private boolean fail;
197
private SuiteTestError err;
198
199
public String getTitle() {
200
return title;
201
}
202
203
public void setTitle(String title) {
204
this.title = title;
205
}
206
207
public String getCode() {
208
return code;
209
}
210
211
public void setCode(String code) {
212
this.code = code;
213
}
214
215
public boolean isFail() {
216
return fail;
217
}
218
219
public void setFail(boolean fail) {
220
this.fail = fail;
221
}
222
223
public SuiteTestError getErr() {
224
return err;
225
}
226
227
public void setErr(SuiteTestError err) {
228
this.err = err;
229
}
230
}
231
232
@JsonIgnoreProperties(ignoreUnknown = true)
233
private static class SuiteTestError {
234
private String message;
235
private String estack;
236
private String diff;
237
238
public String getMessage() {
239
return message;
240
}
241
242
public void setMessage(String message) {
243
this.message = message;
244
}
245
246
public String getEstack() {
247
return estack;
248
}
249
250
public void setEstack(String estack) {
251
this.estack = estack;
252
}
253
254
public String getDiff() {
255
return diff;
256
}
257
258
public void setDiff(String diff) {
259
this.diff = diff;
260
}
261
}
262
}

Was this page helpful?

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.