Dockerizing Appium Tests

Nishant Sharma
TestVagrant
Published in
5 min readOct 22, 2019

--

This article will help you understand how to dockerize Appium tests on both devices and emulators.

Pre-requisites

Just make sure you have
1. docker
2.
VirtualBox + Extension Pack

Let's get started:

For Real Devices

  1. Pull docker images
$ docker pull thenishant/android$ docker pull thenishant/appium

2. Make sure you have the latest docker installed on mac.

$ docker-machine --version

docker-machine version 0.16.1, build cce350d7

3. Create a docker-machine
- You can choose to work directly on your machine or inside some VM, here in this example, I’ll be doing everything inside a VM.

$ docker-machine create --driver virtualbox appium-machine.....Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: docker-machine env appium-machine

4. Enable USB in created docker-machine

$ docker-machine stop appium-machine
Machine "appium-machine" was stopped.
$ vboxmanage modifyvm appium-machine --usb on --usbehci on$ docker-machine start appium-machine
Starting "appium-machine"...
(appium-machine) Check network to re-create if needed...
(appium-machine) Waiting for an IP...
Machine "appium-machine" was started.
Waiting for SSH to be available...
Detecting the provisioner...
Started machines may have new IP addresses. You may need to re-run
the `docker-machine env` command.

Note: You need to install Extension Pack depends on your VirtualBox version, in case you get an Error “Implementation of the USB 2.0 controller not found”

5. Kill ADB server from the base machine

$ adb kill-server

6. SSH into the docker-machine created

$ docker-machine ssh appium-machine

( '>')
/) TC (\ Core is distributed with ABSOLUTELY NO WARRANTY.
(/-_--_-\) www.tinycorelinux.net
docker@appium-machine:~$

7. Run the android docker image which consists of all the android tools

$ docker run -it --rm -v $(pwd)/sdk:/sdk thenishant/android bash -c
'cp -a $ANDROID_HOME/. /sdk'
Digest: sha256:c70d2a40xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Status: Downloaded newer image for thenishant/android:latest

8. Run the Appium docker image which will be used to dockerizing the Appium test

$ docker run --privileged -d -p 5901:5901 -p 2222:22 -p 5037:5037 -p 4723:4723 -v $(pwd)/sdk:/root -v /dev/kvm:/dev/kvm -v /dev/bus/usb:/dev/bus/usb thenishant/appium tail -f /dev/nullDigest: sha256:8c8377exxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Status: Downloaded newer image for thenishant/appium:latest
36da20dexxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

9. Run ADB devices command inside the container to verify ADB can detect the connected Android device.

$ docker exec -it <container-name> adb devices* daemon not running; starting now at tcp:5037
* daemon started successfully
List of devices attached

10. Run this command inside the container to copy the apk file from the host machine to the container

$ docker cp /Users/local_path_to_apk/appName.apk container:/opt

11. Run this command to get the IP address of the docker-machine outside the container.

$ docker-machine ip <your-docker-container-name>192.xxx.xx.xxx

12. Now Run your Appium test by using the app location from step 10 and with from step 11.

For Emulators

  1. Create a VMware Fusion machine.
    The reason I choose to work on VMware fusion, as it supports CPU virtualization.
$ docker-machine create --driver vmwarefusion appiumRunning pre-create checks...
Creating machine...
(appium1) Copying /Users/nishant/.docker/machine/cache/boot2docker.iso to /Users/nishant/.docker/machine/machines/appium/boot2docker.iso...
(appium1) Creating SSH key...
(appium1) Creating VM...
(appium1) Creating disk '/Users/nishant/.docker/machine/machines/appium/appium.vmdk'
(appium1) Virtual disk creation successful.
(appium1) Starting appium1...
Error creating machine: Error in driver during machine creation: exit status 255

2. Regenerate certificate for your docker-machine only if get certificate signed by unknown authority error

$ docker-machine regenerate-certs appiumRegenerate TLS machine certs?  Warning: this is irreversible. (y/n): y
Regenerating TLS certificates
Waiting for SSH to be available...
Detecting the provisioner...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...

3. Stop the docker in order to enable virtualization on VMware machine

$ docker-machine stop appiumStopping "appium"...
Machine "appium" was stopped.

4. Enable virtualization on VMware machine

5. Start the VM machine

$ docker-machine start appiumStarting "appium"...
Machine "appium" was started.
Waiting for SSH to be available...
Detecting the provisioner...
Started machines may have new IP addresses. You may need to re-run the `docker-machine env` command.

6. Install kvm_intel into your VM machine

$ docker-machine ssh appium   ( '>')
/) TC (\ Core is distributed with ABSOLUTELY NO WARRANTY.
(/-_--_-\) www.tinycorelinux.net

$ sudo modprobe kvm_intel

7. Run the android docker image which consists of all the android tools

$ docker run -it --rm -v $(pwd)/sdk:/sdk thenishant/android bash -c 'cp -a $ANDROID_HOME/. /sdk'Digest: sha256:c70d2a40xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Status: Downloaded newer image for thenishant/android:latest

8. Run the Appium docker image which will be used to dockerizing the Appium test

$ docker run --privileged -d -p 5901:5901 -p 2222:22 -p 5037:5037 -p 4723:4723 -v $(pwd)/sdk:/root -v /dev/kvm:/dev/kvm -v /dev/bus/usb:/dev/bus/usb thenishant/appium tail -f /dev/nullDigest: sha256:8c8377exxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Status: Downloaded newer image for thenishant/appium:latest
36da20dexxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

10. SSH into the running container

$ docker exec -it <container name> bash#

11. Run below commands to download emulator components

# echo y | sdkmanager "platform-tools" "platforms;android-24" "emulator"done# echo y | sdkmanager "system-images;android-24;default;x86_64"done

Different images available for android-24 version

system-images;android-24;default;arm64-v8a
system-images;android-24;default;armeabi-v7a
system-images;android-24;default;x86
system-images;android-24;default;x86_64
system-images;android-24;google_apis;arm64-v8a
system-images;android-24;google_apis;armeabi-v7a
system-images;android-24;google_apis;x86
system-images;android-24;google_apis;x86_64
system-images;android-24;google_apis_playstore;x86

12. Create emulator with the above-used system image

# echo no | avdmanager create avd -n emuTest -k "system-images;android-24;default;x86_64"Auto-selecting single ABI x86
Do you wish to create a custom hardware profile? [no]

13. Now run the emulator by running this command

# emulator/emulator -avd emuTest -noaudio -gpu off

Note: You may want to run these commands only if you get an error regarding /dev/kvm inside the container.

umount /dev/kvm
rm -rf /dev/kvm
mknod /dev/kvm c 10 232

14. Copy the apk file from the host machine to the container

docker cp /Users/local_path_to_apk/appName.apk container:/opt

15. Run this command to get the IP address of the docker-machine

docker-machine ip <your-docker-container-name>

15 Now Run your Appium test by using the app location from step 14 and with from step 15

VNC Server

In order to see what's happening with your emulator inside the container, you can use vncviewer from here and then:

vnc://<docker-machine ip <machine-name>>:5901

--

--

Nishant Sharma
TestVagrant

Currently learning everything, plays video games too.