Automated E2E testing with Testcontainers and Selenium

Luiz Gustavo De O. Costa
7 min readJul 2, 2022

Having tests is better than nothing, but having E2E (End to End) testing is fantastic, and in this article, I’ll show you how to do it using Testcontainers and Selenium. At the end of this article, you’ll know to write tests to have the same result as the video below 👇

E2E Selenium test

1. Definition

Testcontainers is a Java library that supports JUnit tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container. [Testcontainers]

Selenium is an open source umbrella project for a range of tools and libraries aimed at supporting browser automation.[3] It provides a playback tool for authoring functional tests across most modern web browsers, without the need to learn a test scripting language (Selenium IDE) [Wikipedia]

End-to-end testing is a methodology that assesses the working order of a complex product in a start-to-finish process. End-to-end testing verifies that all components of a system are able to run and perform optimally under real-world scenarios. However, it’s not accurate to claim that end-to-end testing is a form of testing a completed product. This is because there are other intricacies and forms of end-to-end tests. [TechTarget]

End-to-end testing, or E2E testing, involves verifying an entire application to ensure that the primary flows work from start to finish. Unlike other kinds of tests that only check a portion of your application, end-to-end tests go through the functionality as a whole. For applications that interact with different services or require complex actions, this type of testing simulates real-world scenarios and validates that what your customers see works as intended. [Telerik]

2. Why?

E2E testing is often applied as a software testing methodology. Both forms of end-to-end testing simulate real-world scenarios like the interaction with hardware, networks, and databases. End-to-end testing assumes the advantages of white and black box testing. [TechTarget]

Software systems nowadays are complex and interconnected with numerous subsystems. If any of the subsystems fails, the whole software system could crash. This is a major risk and can be avoided by end-to-end testing. [Katalon]

3. Benefits?

  • Expand test coverage
  • Ensure the correctness of the application
  • Reduce time to market
  • Detect bugs
  • Services that aren’t under our control work as intended with our application

4. How easy is writing E2E testing?

Definitely is not easy, and is more expensive and you take more time to write it than IT (Integration tests) or UT (Unit tests) [Martin Fowler].

It has those limitations should I write an E2E? Yes, as was mentioned in section 3, there are more benefits than drawbacks, and remember you don’t need to cover 100% of the system in E2E testing.

5. Hands-on

To write the E2E testing we are going to use the project Movies tag 0.0.4, since this tag doesn’t have the entire code. If you want to jump to code directly, access the tag 0.0.5.

The below diagram shows the steps to have the E2E up and running and recording 😮.

How to read the diagram? Starting from the number 1 to 4, the containers are created, then our test will be run and the Selenium 4 and 5 will interact with the UI and record the test, written in Java.

The icons on 7,8 represent boba fett, tony stark, and james bond users.

E2E

5.1. Tools

  • Docker Desktop
  • IntelliJ
  • Git

5.2. E2E requirement

Using the Gherkin language (Cucumber will not be used in this project), let’s write 2 scenarios, Login with valid credentials and Authorization to create a new movie.

Scenario: Login with valid credentials
Given I am on the login page
When I fill the field User with <user>
And I fill the field Password with <password>
And I press SIGN IN button
Then I should be on the Movies list page
Scenario: Authorization to create a new movie
Given I'm a valid user
When I'm on the Movies list page
And I have editor access
Then I should see the Add button

5.3. Code

5.3.1. AS-IS

This is the project, using the tag 0.0.4, without any references to E2E tests.

Project as-is

5.3.2. TO-BE

This final project is available on my GitHub under the tag 0.0.5.

Should it be a Java project? Yes, since we’re using Testcontainers for it 😉. Find below how the project is configured.

All the tests will be recorded 📹 and saved on build folder after finished, having success or failure, don't forget it.

Project to-be
  • 1 — The project to store the E2E test code
  • 2 — Utility class to read the properties
  • 3 — Test class
  • 4 — Application configuration file
  • 5 — Required for use Gradle as build application tool

5.3.3. Understanding the code

Hang tight, let’s start 🚀

As I mentioned previously here, all application configuration should be externalized, following the 12 factor app. Below we can find all we need to run the E2E tests properly, such as docker images, ports, users and profiles.

The users boba, tony, and james are users registered into the system, using LDAP in-memory, you can check the configuration here.

5.3.3.1. Build.gradle

In this field, we don’t need to add any Java Framework, such as Spring, Quarkus, DropWizard, since this is a E2E testing, the only required are to handle the tests.

build.gradle

5.3.3.2. Application properties

This file is responsible for store que test configuration.

application-e2e.properties

5.3.3.3. Containers

After setting the configuration it’s time to define the containers, there are 4 containers to be defined and started by dependency order.

All containers are declared as static to avoid restarting them in each test.

  • Database — cockroachdb/cockroach:v22.1.0
  • Back-end application — 16bits/zero2hero-be:0.0.3
  • Front-end application — 16bits/zero2hero-fe:0.0.3
  • Selenium — selenium/standalone-chrome

🔑 ✴️ Key point!!

Declare the first container with Network.SHARED, in order to create a docker container network, line 4.

The next containers will use the same network of the first container, on lines 14, 29, 41.

Containers

5.3.3.4. Login with valid credentials

Using parameterized tests gives us more flexibility and less code, since we can execute the same tests with different entries. The entries for these tests are declared on application.properties file and are the same as our in-memory LDAP. If you add another user not registered in our LDAP, for instance darth/vader the login will fail, in this case it is expected to fail.

Test

5.3.3.5. Authorization to create a new movie

Using the same strategy for the first test, ie, parameterized test, we are going to check if the user is authorized to create a new movie, since this is possible through the ADD button.

Test

5.3.3.6. E2E entire class

Find below the complete test class

EndToEndTest class

6. Tech stack

  • OpenJDK 11
  • JUnit5
  • Testcontainers
  • Docker
  • Selenium
  • Gradle

If you like this article, please subscribe and give your claps 👏 :).

7. Conclusion

Having the Testcontainers library is a win-win to Java projects to E2E tests or even in development time, in order to identify problems, issues and more common in a microservice world, integration problems.

Remember that E2E testing is only a part of systems tests, and tests should be written not to prove how right you’re but how far from an error the system is.

8. References

https://www.broadleafcommerce.com/blog/selenium-acceptance-testing-multi-container-docker-project

--

--

Luiz Gustavo De O. Costa

Hey friend!! I’m Luiz Gustavo, a Java developer and I’m here to learn and write about Java, tests and good practices