@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class TodoControllerCypressIntegrationTest {
@LocalServerPort
private int port;
@TestFactory
List<DynamicContainer> runCypressTests() throws InterruptedException, IOException, TimeoutException {
Testcontainers.exposeHostPorts(port);
DynamicContainer chromeTests = DynamicContainer.dynamicContainer("Chrome",
runCypressTestsOnBrowser("chrome")); (1)
DynamicContainer firefoxTests = DynamicContainer.dynamicContainer("Firefox",
runCypressTestsOnBrowser("firefox")); (2)
return List.of(chromeTests, firefoxTests);
}
@NotNull
private List<DynamicContainer> runCypressTestsOnBrowser(String browser) throws InterruptedException, TimeoutException, IOException {
try (CypressContainer container = new CypressContainer().withLocalServerPort(port)
.withBrowser(browser)) {
container.start();
CypressTestResults testResults = container.getTestResults();
return convertToJUnitDynamicTests(testResults); (3)
}
}
@NotNull
private List<DynamicContainer> convertToJUnitDynamicTests(CypressTestResults testResults) {
List<DynamicContainer> dynamicContainers = new ArrayList<>();
List<CypressTestSuite> suites = testResults.getSuites();
for (CypressTestSuite suite : suites) {
createContainerFromSuite(dynamicContainers, suite);
}
return dynamicContainers;
}
private void createContainerFromSuite(List<DynamicContainer> dynamicContainers, CypressTestSuite suite) {
List<DynamicTest> dynamicTests = new ArrayList<>();
for (CypressTest test : suite.getTests()) {
dynamicTests.add(DynamicTest.dynamicTest(test.getDescription(), () -> assertTrue(test.isSuccess())));
}
dynamicContainers.add(DynamicContainer.dynamicContainer(suite.getTitle(), dynamicTests));
}
}