Testcontainers-cypress release 0.4.0

Posted at — Feb 11, 2020
Riekpil logo
Learn how to test real-world applications with the Testing Spring Boot Applications Masterclass. Comprehensive online course with 8 modules and 130+ video lessons to master well-known Java testing libraries: JUnit 5, Mockito, Testcontainers, WireMock, Awaitility, Selenium, LocalStack, Selenide, and Spring's Outstanding Test Support.

Testcontainers-cypress 0.4.0 is released with support for Cypress 4.0.1 which has Firefox support.

Multiple browsers

With testcontainers-cypress 0.4.0, it is now possible to specify which browser to use:

try (CypressContainer container = new CypressContainer().withLocalServerPort(port)
                                                        .withBrowser("firefox")) {
    container.start();
    CypressTestResults testResults = container.getTestResults();
    ...
}

If we combine this with JUnit 5 DynamicTest support, we can do this:

@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));
    }
}
1 Run the tests in Chrome
2 Run the tests in Firefox
3 Convert the results of the Cypress tests into DynamicContainer and DynamicTest objects

When running the test in IntelliJ, we see this nice hierarchy of the tests, grouped per browser:

intellij test results multiple browsers

Specify what tests to run

With this release, a new withSpec(String spec) method is added. This allows to specify a single spec file to run or to run multiple by specifying a wildcard.

For example to specify a single spec file:

try (CypressContainer container = new CypressContainer().withLocalServerPort(port)
                                                        .withSpec("cypress/integration/todos.spec.js")) {
    container.start();
    CypressTestResults testResults = container.getTestResults();

Example with wildcards:

try (CypressContainer container = new CypressContainer().withLocalServerPort(port)
                                                        .withSpec("cypress/integration/login/**")) {
    container.start();
    CypressTestResults testResults = container.getTestResults();

Conclusion

See testcontainers-cypress-0.4.0-example for the full source of this example.

If you want to be notified in the future about new articles, as well as other interesting things I'm working on, join my mailing list!
I send emails quite infrequently, and will never share your email address with anyone else.