Note
This article is a reproduction of a medium article: Run your Appium tests using Docker Android & Genymotion Cloud by Ellinor Kwok.
In this article, we’ll explain what are the main advantages of using Docker-android with Genymotion SaaS and Genymotion Device image (PaaS) to run Android mobile automated tests, then we will provide tutorials.
Genymotion hosts cloud-based Android emulators running in a SaaS fashion or as virtual images on AWS, GCP, Alibaba Cloud or MS Azure (PaaS). The integration so far has been done with SaaS & PaaS (only AWS).
Docker-Android is a docker image built to be used for everything related to Android mobile website or application testing. All the tools needed (devices, framework, Android SDK…) are packaged in the image. It is free and open source and supports other features such as video recording.
Advantages
The main goal is to help the user focus on writing the UI tests by leveraging the advantages of using a docker :
- Having a ready to test infrastructure that will run the same environment everywhere it is deployed (your local machine, server) : You get rid of all the hassle of installing the tools needed (Appium, Selenium Grid, AWS CLIs, Android SDK, Genymotion tools…) but also maintaining them. For example if you update the version of one tool it could break the integration.
- Being able to switch between cloud platforms easily : e.g. from Genymotion Cloud SaaS to AWS. All the integrations are done in one single docker container.
- No knowledge is needed on how to use tools to start Genymotion devices in the cloud and integrate it with Appium and Selenium. All these mechanisms are done in the background, letting the users focus on writing their tests.
Tutorials
Start the devices on which you wish to run your test
Genymotion SaaS (Cloud)
First, you need to define the devices with a JSON file:
where:
template
is the name of the template of the device you wish to startdevice
is the name of the deviceport
(optional) is the adb port if you need a persistent one. This is indeed quite handy for running appium tests without changing the device name in your tests every time you need to run those tests.
You can refer to this device.json file.
Then, start the docker container with the following commands:
$ export USER="xxx"
$ export PASS="xxx"
$ docker run -it --rm -p 4723:4723 -v $PWD/genymotion/example/sample_devices:/root/tmp -e TYPE=SaaS -e USER=$USER -e PASS=$PASS budtmo/docker-android-genymotion
$PWD
is the path where your device.json file is stored. For example, if you pulled the docker-android project, the path is docker-android/genymotion/example/sample_devices
If you’d like to use appium with selenium hub, you can use docker-compose
. Please refer to this geny.yml file. Then use this command to start the docker instead of docker run. Your current path must bedocker-android/genymotion/example
:
export USER="xxx" && export PASS="xxx" && docker-compose -f geny.yml up -d
Genymotion Device image (PaaS - AWS)
First, you need to define the devices with a JSON file:
You need to define for each instance the region (region field e.g eu-west-1 for Ireland) where it will run and the type of instance (instance field e.g t2.small).
Please refer to the product page for the complete list of compatible instance type.
By default, if you fill the android_version field (5.1 / 6.0 / 7.0 / 8.0 / 8.1 / 9.0 / 10.0 / 11.0), it will start the latest image available on the market. If you prefer to start preconfigured images you can use AMI field instead which is the AMI ID of the template.
Regarding security groups, if you don’t put any SG field, it will by default create one with those Inbound SSH, HTTP, HTTPS/WSS & WebRTC and Outbound rules on 0.0.0.0/0.
For a custom security group :
- an existing one, put the group ID in the SG field, e.g
"SG": "sg-12a28a6a"
- non existing one, input blocks like this for inbound and outbound rules
You can refer to this aws.json file
Then start the docker container with this command:
docker run -it --rm -p 4723:4723 -v $PWD:/root/tmp -v $PATH_AWS:/root/.aws -e TYPE=aws butomo1989/docker-android-genymotion
$PWD
is the path where your aws.json file is stored. For example, if you pulled the docker-android project, the path isdocker-android/genymotion/example/sample_devices
$PATH_AWS
is the path where your aws access keys are stored (e.g ~/.aws/ if you already have AWS CLI installed).
If you’d like to use appium with selenium hub, you can use docker-compose. Please refer to this geny.yml file. Then use this command to start the docker instead of docker run. Your current path must be docker-android/genymotion/example
:
docker-compose -f geny.yml up -d
Run your appium tests
Once the docker container is started, devices are launched and connected to adb in the container with the appium server port 4723 or the selenium hub 4444 port exposed depending on which command you used to start the container. No further configurations is needed, just run your tests as usual.
These are the adb serials of the devices started to identify them (UDID DesiredCapability) on which your tests will run against :
- Genymotion SaaS:
If you defined theport
e.g. 38727, the adb serial islocalhost:38727
. If not it will be a random port. - Genymotion Device image (PaaS):
Each device defined in the json file will get a port number starting from 5555. For example, if you start 2 devices, the adb serials will respectively belocalhost:5555
andlocalhost:5556
for the first and second device in the list.
If you want to verify the devices connected to adb, you can always run adb devices
inside the container :
docker exec $ID adb devices
$ID
is the ID of the container
You can refer to our previous blogpost (#SECTION Write your test in Python) or my project sample on Github in Java for more detailed information regarding the appium tests source code.
Stop your devices
Once your tests are finished, stopping the docker will automatically stop the instances. You don’t need to tear them down manually:
docker stop $(docker ps -a -q)