End to End tests with Selenium and Docker [The Ultimate Guide]
Automated end to end is the most effective way to test your app. It also requires the least effort to bear fruits if you have no tests at all. And you don’t need a ton of infrastructure or cloud servers to get there.
In this guide we’ll see how we can easily overcome the two main hurdles of end to end testing.
The first hurdle is Selenium. The API you use to write tests is ugly and non intuitive. But it is not that difficult and with a few convenient functions it can become a breeze to write Selenium tests. The effort is well rewarded because you can automatically tests your end users flows end to end.
The second hurdle is putting components together in an isolated environment. We want the frontend, the backend, the database and everything else your app uses. We will use Docker compose to put things together and automate the tests. It is easy even if you have your components in different git repositories.
Writing end to end tests in Selenium
Even if you are an API only business, you have a frontend. An admin or back office frontend. So end to end tests ultimately talk to a frontend application.
The industry standard tool is Selenium. Selenium provides an API to talk to the web browser and interact with the DOM. You can check what elements are displayed, fill inputs and click around. Anything a real user would do with your application, you can automate.
Selenium uses something called the WebDriver API. It is not very handy to use at first glance. But the learning curve is not steep. Creating a few convenience functions will get you productive in no time. I won’t go into the details of the WebDriver API here. You can have a look at this excellent article to dig deeper.
There are libraries to make your life easier. Nightwatch is the most popular.
If you have an Angular application Protractor is your best friend. It integrates with the Angular event loop and allows you to use selectors based on your model. That is gold.
Writing a test for your the most critical user feature or your app should take only a few hours so go ahead. It will run automatically ever after. Let’s see how.
Running your tests in Docker
We need to run our tests in an isolated environment so the outcome is predictable. And so we can enable Continuous Integration easily. We’ll use Docker compose.
Selenium provides Docker images out of the box to test with one or several browsers. The images spawn a Selenium server and a browser underneath. It can work with different browsers.
Let’s start with one browser for now: headless-chrome. The docker-compose.yml file looks as below (commands are from an Angular example).
Note: If you never used Docker you can easily install it on your computer. Docker has the troublesome tendency of forcing you to sign up for an account just to download the thing. But you actually don’t have to. Go to the release notes (link for Windows and link for Mac) and download not the latest version but the one right before. This is a direct download link.
The file above tells Docker to spin up an environment with 3 containers:
- Our app to test: the container uses the myapp image which we’ll build right below
- A container running the tests: the container uses the same myapp image. It uses dockerize to wait for the servers to be up before running the tests
- The Selenium server: the container uses the official Selenium image. Nothing to do here
We could run the tests from the same container as the app. Splitting it makes things more clear. It also allows to separate outputs from the 2 containers in the result logs.
The containers will live inside a private virtual network and see each other as http://the-container-name (more here on networking in Docker).
We need a Dockerfile to build the myapp image used for the containers. It should turn your frontend code into a bundle as close to production as possible. Running unit tests and linting is a good idea at that stage. After all no need to run end to end tests if the basics do not work.
In the Dockerfile below we use a node image as base, install dockerize and bundle the application. It is important to build for production. You don’t want to test a development build that is pre-optimizations. Many things can go wrong there.
Now that we have all the pieces together let’s run the tests using this command:
It is long’ish so script it in your project somehow. What it will do is build the myapp image based on the provided Dockerfile and then start all the containers. Dockerize makes sure your app and Selenium are up before executing the tests.
--abort-on-container-exit option will kill the environment when one container exists. Since only our testing container is meant to exit (the others are servers), that is what we want.
The docker-compose command will have the same exit code as the exiting container. It means you can easily detect from the command line if the tests succeeded or not.
You are now ready to run end to end tests locally and on any server supporting Docker. That’s pretty good!
Tests run with only one browser for now though. Let’s add more.
Testing on different browsers
The standalone Selenium image spins up a Selenium server with the browser you want. To run the tests on different browsers you need to update your tests configuration and use the selenium/hub Docker image.
The image create a hub between your application and the standalone Selenium images.
Replace the selenium container section in your docker-compose.yml as follows:
We now have 3 containers: Chrome, Firefox and the Selenium hub.
All the Docker images provided by Selenium are in this repository.
Careful! There is a tricky timing effect to consider. We use dockerize to have our test container wait for the Selenium hub to be up. That is not enough because we need to wait for the standalone images to be ready. In fact to register themselves to the hub.
We can do this by waiting for the standalone images to expose open a port but that is not a guarantee. It is easier to wait a few seconds before starting the tests. Update your test script to wait 5 seconds before the tests start (add a sleep command after the dockerize call).
Now you are sure that your tests won’t start until all the browser are ready. Waiting is not ideal but a few seconds are worth it. There is nothing more annoying than failing tests because of unstable automations.
Good. We have now covered the frontend part. Let’s add the backend.
Add the backend as containers or git modules
The above might seem overkill to test only the front end part of your app. But we aim for much more. We want to test the whole system.
Let’s add a database and a backend to our Docker compose environment.
If you are a front end developer you might think “we are the frontend team we don’t care about testing the backend”.
Are you sure?
The frontend is always the last part to integrate all the other pieces. That means crunch time. Crunch time that would no longer be if you were able to test the frontend with the backend continuously and catch errors sooner.
The technique I describe here is very easy to apply even if your backend is in a different repository.
This is how the docker-compose.yml looks like:
In this example we added a postgres database and a container for the backend to run. Dockerize synchronizes containers commands.
If your system has more than one backend component add as many containers as you need. You need to wire the container dependencies properly. It means proper hostnames as environment variables on your components. And order of startup if some components depend on others.
The Selenium tests you have written should not need any modifications. You might need to put test data in the database. To keep this step in the testing area we added the seeding script before the backend startup script. This way we are sure that things happen in the proper order:
- The DB starts and is ready to accept connections
- A script seeds the DB data
- The backend and the frontend start
- The tests can start
If you look at the backend container you can see there is a catch. It uses an image called mybackend built from a file located at backend/Dockerfile. This implies that your backend is in the same git repository in a folder called “backend”. The name is just an example of course.
If your backend and frontend are in the same repository you are good. Define the Dockerfile to build your backend and adjust the startup command to what you need.
That’s all good but usually the backend is not in the same repository. Or you can have many backend components in different repositories. What do you do then?
The super clean solution is to have a CI process on each backend component repository. If you don’t have any you can checkout the API end to end testing with Docker guide to get started. It uses the same techniques as this article so you have a consistent setup across your whole project.
The CI process for each component runs automated tests. Upon success it pushes a docker image with the component to a private Docker registry. The backend container in our docker-compose.yml file above would use this image.
This solution requires a private Docker registry to store your images. You can use Docker Hub but then it becomes public. If you don’t have one already and don’t plan to do so, it is not worth the effort.
The other solution is to use the submodules feature in git. Your backend repository becomes a virtual child of your frontend repository. You just need to add the file .gitmodules like this to your frontend repository:
Run the command
git submodule update --remote will pull the specified branch of the backend repository into a folder called “backend”. Add as many submodules as you need if you have more than one backend component.
That’s it. Have your CI run the submodule command and from a file system perspective you are as in a monorepository. If you don’t want the backend code locally while developing the frontend just don’t run the command. You’ll have an empty backend folder.
Versioning and backend/frontend incompatibilities
The 2 techniques above test the frontend with the latest “CI tests passed” version of your backend. That may lead to broken builds if your components are no compatible at times.
If they are compatible more often than not, stick to the “always test with the latest versions” approach. You’ll fix the occasional incompatibilities on the fly.
That won’t work though if incompatibilities are business as usual. In this case you need to manually control version updates. That is very easy to do.
You can lock the version of a component in the docker-compose.yml file or in the .gitmodules file. When pushing to the Docker registry you would tag the component image with the commit number of the corresponding code. The relevant docker-compose.yml file section becomes:
Similarly the .gitmodules file would not target a branch head but a given commit:
Bonus: version updates are versioned with your code. You can track which version was used for each build. This is useful when fixing failed builds or trying to reproduce old bugs.
We could push the approach to the next level. You could have a dedicated repository that would wire all your components as git modules. Bumping the versions could be a form of delivery and handover to the test/QA team.
In theory it is best to keep the latest versions of components work together more often than not. And drop the need for manual versioning. If that is not the case that is OK. Ignore the purists who will tell you that you are not following best practices and so on.
Pick what works best for you to enjoy the benefits of automated testing right now. Then keep improving your process along the way.
Bonus on writing maintainable Selenium tests
Back to Selenium with 3 important advices to write good UI tests.
First, avoid CSS selectors if you can. Selenium works on the DOM and can identify elements by IDs or CSS or XPath. Use IDs as much as possible even if you have to add them to your app code for this only purpose. CSS and XPath selectors are shaky. As soon as your application structure changes, they will be broken.
Second, use the Page Objects approach. It is about encapsulating your application so selectors are not directly used in tests. If your page HTML/CSS change, your tests will have to be rewritten to use new selectors. Page Objects abstract selectors and turn them into user actions. Here is a great article on how to use Page Objects properly.
Third, don’t build long user journeys in your tests. If your tests fail at the 50th action it’s going to be difficult to reproduce and fix. Create test suites that play part of scenarios starting from the login page. This way you are always a few clicks away from the bug your tests would catch.
Also you don’t risk writing tests that rely on state from previous actions. Tests suites coupling is something you want to avoid. Let’s take a practical example.
Say you are testing a SaaS application for schools. The use cases could be:
- Create a class
- Register kids and parents data
- Setup the weekly plan for the class
- Check presence/absence
- Input grades
Along the way you will have the login process and some navigation checks.
You could write a test that goes through the whole chain as described above. And this would be convenient because to declare kids you need a class to exist. To check presence/absence you need the weekly plan in place. And you need kids to input grades. It’s a quick win to build 1 test suite that does all these things at first.
If you have nothing at the moment and want to achieve good test coverage quick: go for it! Done is better than perfect if it allows you to catch errors in your application now.
The cleaner solution would be to use a baseline scenario to start smaller test suites. In the example above the baseline scenario should to create a class and register kids data.
Create a test suite that does exactly that: create a class and registered kids and parents data. Run it always first. If this stops working then you don’t need to move further on. This version of the code will never reach end users anyway.
Then create a function that encapsulates the baseline scenario. It will be duplicate code to some extent with the previous test suite. But it will allow you to have a one line function to use as a setup hook for all the other test suites. This is the best of both worlds. Tests scenarios starting from a fresh state in the application with minimal effort.
I hope this gave you a good insight on how you can quickly setup end to end tests for a complex system. Multiple components in multiple repositories should not be a barrier. Docker compose makes it easy to put things together.
End to end tests are the best way to avoid crunch times. In complex systems late deliveries of some components put a burden on other teams. Integrations are done in a rush. Code quality drops. That’s a vicious circle. Testing often and catching cross components errors early is the solution.
Selenium tests can be done quick and dirty to get going fast. That is perfectly OK. Automate things. Then improve. Remember:
Done is better than perfect any day of the year.
Thanks for reading!
Published on by