NashTech Insights

Dockerizing Appium Tests: A Complete Guide

Rahul Kumar
Rahul Kumar
Table of Contents

Hello readers! Welcome back to our blog, where we explore exciting topics that bring immense value to projects involving test containers. In this article, we will delve into the world of Appium testing and discuss how we can efficiently run Android Appium tests within a test container. As Appium has gained significant recognition as a mobile testing tool, I assume most of you are already familiar with its capabilities. For those who are unfamiliar with Appium, I will give links to previous postings where you may obtain more information about this amazing technology. So, let’s begin the article with a quick overview of test containers and Docker.

What is Docker, and why do we need a Docker environment to run the test case?

Docker is a software platform that enables developers to build, test, and deploy applications efficiently. It utilizes containerization to package software into self-contained units called containers. Containers contain all the necessary components and dependencies needed to run the software, including libraries, system tools, code, and runtime.

Here are some points that support the need for a Docker environment to run test cases:

  • Isolation: Docker containers provide a high level of isolation, allowing you to run tests in an isolated environment without interfering with the host system or other containers. This ensures that test results are reliable and reproducible.
  • Consistency: Docker allows you to package the entire testing environment, including the application, dependencies, and configuration, into a single container. This ensures that the same environment is used across different stages of the development pipeline, from development to testing and production.
  • Portability: Docker containers are portable, which means they can operate on any machine with Docker installed, independent of the operating system. This makes it easy to share test cases and run them on different machines without worrying about compatibility issues.
  • Dependency Management: Dependency Management: Docker simplifies dependency management by encapsulating all the required dependencies within the container. This eliminates the need to install and manage dependencies manually on the host system, reducing conflicts and compatibility issues.
  • Reproducibility: Docker allows you to define the entire environment, including the operating system, libraries, and configurations, as code using Dockerfiles. This allows you to version control the environment and quickly replicate it as needed, providing uniform and reproducible test results.
  • Scalability: Docker enables you to easily scale your testing environment by running multiple containers in parallel. This is very beneficial for performing tests in a distributed and parallelized way, lowering total test execution time.
  • Collaboration: Docker simplifies collaboration among team members by providing a standardised environment that can be easily shared and replicated. Team members can work on the same codebase and run tests in identical environments.

What is necessary for running the Appium test within a Docker container?

The following components must be present on the local system in order to launch Docker:-
  1. Docker: Make sure you have Docker installed and running on your machine. Docker provides a platform for containerization, allowing you to run applications and tests in isolated environments.
  2. Docker Image/Docker File: You will need a Docker image that contains the necessary dependencies to run Appium and your tests. This image can be based on a specific operating system or an existing image that already has Appium and other required tools installed.
  3. Mobile App: You will need the APK (Android) file of the mobile app that you want to test.
  4. Test Framework: Choose a test framework compatible with Appium, such as TestNG, JUnit, or Cucumber.
  5. Test Code: Write your Appium test code using the selected test framework and programming language.
These components must be available within a Docker container in order to build up the test environment:-
  1. Appium: An open-source solution for automated mobile app testing is called Appium. To run tests, Appium must be installed and configured in your Docker image.
  2. Android Emulator or iOS Simulator: To execute mobile tests, you will need an Android emulator configured in your Docker image. These emulators/simulators will provide the environment to run your tests on virtual mobile devices.
  3. Configuration Files: Prepare any necessary configuration files for Appium, such as desired capabilities, device configurations, and test-specific settings. These files should be included in the Docker image or mounted as volumes during container execution.
  4. Copy test Suits and Mobile App: Ensure that the test code and app file are accessible inside the Docker container, either by copying it into the container during the build process or by mounting it as a volume when running the container.
  5. Docker Compose (optional): If you have multiple containers or services that need to work together, you can use Docker Compose. It simplifies the process of running multiple containers and allows you to define the dependencies and communication between them.

Dockerfile

Unfortunately, creating the Docker container wasn’t easy because we needed a number of things to run our test case, including an Appium server, an Android VM, and a Docker container with the Android SDK deployed. I chose to start with a base image, budtmo/docker-android-x86-8.1, that set up the emulator and installed Appium, and then we required Maven: 3.5.2-jdk-8 since we needed to perform the mvn command to execute the tests, which was equivalent because I had successfully gotten everything working locally (on Ubuntu). Following that, I installed the APK.


FROM budtmo/docker-android-x86-8.1

# Install Maven
RUN apt-get update && \
    apt-get install -y maven

# Copy the APK file
COPY sample_apk/Android.SauceLabs.Mobile.Sample.app.2.7.1.apk  /app/Android.SauceLabs.Mobile.Sample.app.2.7.1.apk

#Copy files
COPY src  /app/src
COPY pom.xml  /app/pom.xml

# Expose necessary ports
EXPOSE 6080 4723 5554 5555

# Set environment variables
ENV DEVICE="Samsung Galaxy S6" \
    APPIUM=true \
    CONNECT_TO_GRID=true \
    APPIUM_HOST="127.0.0.1" \
    APPIUM_PORT=4723 \
    SELENIUM_HOST="172.17.0.1" \
    SELENIUM_PORT=4444 \
    MOBILE_WEB_TEST=true

How to build a Docker image and a container

This file should be saved as a Dockerfile and placed in the project’s root directory. Using the below command after saving the file will allow you to create your own image.

docker build -t <imageName>  .

This command begins creating the images based on the arguments specified in the Docker file as soon as you execute it. The images may take a while to download from the Docker Hub.

You may check to see if a Docker image was produced once the image-building process is finished, as seen in the screenshot above, by executing the next Docker command in the terminal.

docker images

Now we have everything to set up the docker container, i.e., a docker image that has an emulator and an Appium server, and we also included Maven, which allows us to run our test using the maven command.

docker run --privileged -d -p 6080:6080 -p 4723:4723 -p 5554:5554 -p 5555:5555 -e DEVICE="Samsung Galaxy S6"   --name <containerName> <dockerImageName>

Similarly, the docker run command will construct and execute a docker container, and you can verify whether any container is running or not by following the next docker command.

docker ps

The above result indicates that a docker container is running and linked to both the Appium host and our local host 6080. When you open this local host in a browser, the screen below appears.

Now that you can directly interact with Appium Server and the emulator (the Samsung Galaxy S6), the next step is to install the target mobile application into the emulator, which is inside a container. So we utilise Android Debug Bridge (ADB) for this, and it is also used for a variety of other things, including the following:

1 . Find the available attached device inside a container
docker exec -it <containerName> adb devices   
2. Install the mobile application on Emulator inside a container
docker exec -it <containerName> adb install <path of application inside a container>

The majority of our work is done; the only thing left is to execute the test suites from the maven command inside our running Docker container.

sudo docker exec -it <containerName>   mvn test 

As you can see in the above screenshot, our test suites are executing through the maven command inside our docker container. If our execution is complete, we must terminate our docker container, which is running on our local host 6080, with a small docker command like

docker stop <containerName>

We are all done now!!

Conclusion

I appreciate your patience as I finished. In this blog, we all learned about how we run the Appium test suite within a Docker environment. I’ll also make available a tech hub containing all of my Appium test scripts, a Dockerfile, and a fake mobile app. I’ll also include a Docker compose file to my project that will simplify things for everyone. Share this blog with your friends if you enjoyed it. Moreover, you may reach me at rahul.kumar@nashtechglobal.com if you still have any questions.

Till then, Happy learning, and happy sharing!

Rahul Kumar

Rahul Kumar

I am a self-driven self-starter with strong analytical, problem-solving, and communication skills, and I'm always looking to pick up new skills and develop my interpersonal skills. I am currently employed as a quality assurance software consultant. I am well-versed in creating automation frameworks and am familiar with key QA ideas. I've worked with automation technologies in the past, including postman.io, cypress.io, gatling.io, selenium (java), Appium, Grafana-K6, and core Java development tools, among others. Maven, Version Control GIT (GitLab Project Management Tool).

Leave a Comment

Your email address will not be published. Required fields are marked *

Suggested Article

%d bloggers like this: