How to run Selenium tests with Maven and Docker
In this guide we'll describe how to create a delivery pipeline that will:
- build a Docker image with a web application
- launch the application in a Docker container
- run Selenium tests with Maven from a separate container
Actions used in this guide:
Pipeline flow chart
On every change to the repository, the pipeline will perform the following tasks:
- Build a Docker image of web application
- Run 3 Docker containers:
- container from the image built in the previous step
- container with a Selenium server
- container with Maven that will run the tests
- Depending on test results, the pipeline will:
- [Passed] Push the image to the Docker registry & run the image on the server
- [Failed] Notify the team via Slack
Image loading...
Pipeline configuration
First, we need to authenticate Buddy in the source code repository:
Image loading...
Next, we add a new pipeline and configure it to run on every push to the master branch:
Image loading...
Actions
Now that we specified the target branch and trigger conditions for the pipeline, it's time to add the actions that will run our tasks.
Build Docker image
On the actions view, look up Build Image in the Docker section.
Image loading...
Define the Dockerfile and the context if it's different than the directory with the Dockerfile. In our case, it's front
:
Image loading...
Configure Selenium tests with Maven
With the Docker image built and waiting, we need something to run the tests. Look up Maven in the builds & task runners section:
Image loading...
Enter the command that will run the tests, e.g. mvn clean install
:
Image loading...
Now we need to add two microservices in the Services tab. Those are essentially Docker containers attached and linked to the main build container.
The first should be the Selenium service for your type of browser. In this example we'll use Selenium Chrome: Image loading...
Now, click Attach another and select Custom service. Configure it to use the Docker container that we built in the previous step: Image loading...
80
). This way Maven will not start the tests until the application has successfully started.
Configure Selenium details
Once configured, the action will run 3 interlinked containers: Maven, Selenium, and a custom container. In order to make it work, we need to configure the Selenium server to which the tests will connect and the address of the application that will be run.
In our application, we defined them in lines 23 and 30 respectively. The hostnames should reflect those entered in the services of the Maven action:
Image loading...
hostname
can be defined in the pipeline as an environment variables. If a variable is not resolved in runtime, you can use the Find & Replace action that will swap its values.
Push Docker image to registry
If all tests have passed successfully, the image can be pushed to a Docker registry:
Image loading...
Buddy has native integrations with Docker Hub, Amazon ECR and Google GCR. You can also push images to your own private registry:
Image loading...
Release application
The image is stored in the registry and ready to be released on the server. With Buddy, you can deploy it to a Kubernetes, Amazon EKS or Azure AKS cluster, or you can simply run docker-compose on your own server.
Image loading...
Notifications
Letting your team know that the tests have failed – or your client that a new version is waiting for review – is key to any software development business, which is basically one continuous build-test-release operation. Buddy natively integrates with the most popular notification services, including Slack, Discord, and Telegram:
Image loading...
Jarek Dylewski
Customer Support
A journalist and an SEO specialist trying to find himself in the unforgiving world of coders. Gamer, a non-fiction literature fan and obsessive carnivore. Jarek uses his talents to convert the programming lingo into a cohesive and approachable narration.