Testing Dockerized Webapp With Cucumber And Selenium

My task is to test a dockerized web application using Selenium. It is important that the tests are defined with Gherkin and of course run headless on Jenkins. Here is what I did to achieve this task. Cucumber And Testcontainer Specifics This is a snippet from the class responsible to pull and instantiate the Webapp container which contains the build of my webapp to test: @Slf4j public class WebappContainer { private static WebappContainer instance = new WebappContainer(); private static final String NETWORK_ALIAS = "WEBAPP"; private static final int EXPOSED_PORT = 7654; private static final DockerImageName dockerImageName = DockerImageName .parse("myecr.amazonaws.com/webapp/my-little-webapp:" + getWebappImageVersion()); public static final GenericContainer<?> container = new GenericContainer<>(dockerImageName) .withNetwork(NetworkUtils.getNetwork()) .withNetworkAliases(NETWORK_ALIAS) .waitingFor(Wait.forHttp("/").forStatusCode(200).forPort(PORT) .withStartupTimeout(Duration.ofSeconds(STARTUP_TIMEOUT))) .withExposedPorts(EXPOSED_PORT) .withLogConsumer(new Slf4jLogConsumer(log)) // next line maybe specific to my setup: Mount Spring Boot test config into container .withClasspathResourceMapping("application-test.yml", "/etc/config/application.yml", BindMode.READ_ONLY); private WebappContainer() { } public static WebappContainer getInstance() { return instance; } public void start() { container.start(); } public void stop() { container.stop(); } public boolean isRunning() { return container.isRunning(); } public int getHttpPort() { return container.getMappedPort(EXPOSED_PORT); } } For completeness sake here the static helper method to get the image ...

June 9, 2021 · 3 min · Jens