Appium Testing ReactNative for Android Emulator in Github Actions Self-Hosted Runners, Dec 2020

Sandeep Dinesh
4 min readDec 14, 2020

--

In one of my earlier posts I discussed setting up Github Actions for Appium Testing for React Native Android apps, and that still works. But as with everything else, in course of time, we realize the limitations of this.

Important: This post is a continuation of that post. Please re-visit the same once.

Why to set up a MacMini? — Github Actions Virtual Machines’s hardware configurations are fixed, so the App, Metro Builder, Appium and CodeceptJS running together literally slows down the system immensely. Since all developers & managers want quicker Sanity test results and high coverage test suites for regression; this configuration had become a bottleneck for us.

From my end, I reached out to Github Actions to see if I can get a bigger VM, even gave them the greed that my company can pay for this config! But Alas, the ticket is still lying around in their repo unattended.

So, I decided to come down from the Cloud, and on to a physical device — got my favourite MacMini and set up a self-hosted runner. If you have read my posts for setting up Android and iOS Simulator tests in Github Actions, we need a system with Hardware Acceleration, that’s the main motivation for this technology.

Here goes the steps/ TLDR -

  • Create an admin user on the MacMini
  • Login to the MacMini and do the installations as this user
  • Ensure the connectivity to GitHub action servers are fine, else work with your IT team to open them for you.
  • Install Java 8, Add $JAVA_HOME environment variable into bash profile
  • For e.g. /Library/Java/JavaVirtualMachines/jdk1.8.0_231.jdk/Contents/Home
  • Install Android Studio, Add $ANDROID_HOME environment variable into bash profile
  • For e.g. /Users/<user>/Library/Android/sdk
  • Have $PATH modified as follows
  • PATH=$PATH:$JAVA_HOME/bin:$ANDROID_HOME/emulator:$ANDROID_HOME/platform-tools:$ANDROID_HOME/tools/bin
  • Install node, brew, and npm (Nothing fancy, the defaults would be enough!)
  • Do all the steps mentioned in adding Self-hosted runners for Mac, 64-bit architecture
  • Important: Keep the terminal window (where git hub action runner command line is running) open.
  • Change your workflow to ‘self-hosted

Psuedo Code is provided below, only highlighting the key points pertaining to custom runner and emulators. Please modify the workflow for individual build step, test run and uploading results; those aspects as omitted in this pseudo code.

# E2E Smoke Tests in Android 9 on 'develop' branch (CUSTOM-RUNNER)
name: E2E Smoke Tests in Android 9 on 'develop' branch (CUSTOM-RUNNER)
# Controls when the action will run.
on:
schedule:
# Trigger at 00:00 EST
- cron: '0 4 * * *'
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
build:
# The type of runner that the job will run on
runs-on: self-hosted
# Set timeout
timeout-minutes: 115
# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v2
with:
ref: develop
... # Delete Existing Emulators
- name: Delete Existing Emulators
run: |
$ANDROID_HOME/emulator/emulator -list-avds | while read emulator ; do
echo "Deleting $emulator"
$ANDROID_HOME/tools/bin/avdmanager delete avd -n $emulator
done
echo "Deleted Existing Emulators"
# Run Android Emulator
- name: Run Android Emulator
run: |
echo "y" | $ANDROID_HOME/tools/bin/sdkmanager --install 'system-images;android-28;google_apis;x86_64'
echo "no" | $ANDROID_HOME/tools/bin/avdmanager create avd -n test_device -k 'system-images;android-28;google_apis;x86_64' --force
echo $ANDROID_HOME/emulator/emulator -list-avds
echo "Starting emulator"
nohup $ANDROID_HOME/emulator/emulator -avd test_device -skin 1080x1920 -memory 1024 -engine qemu2 -netfast -no-audio -no-snapshot -accel on > /dev/null 2>&1 &
$ANDROID_HOME/platform-tools/adb wait-for-device shell 'while [[ -z $(getprop sys.boot_completed | tr -d '\r') ]]; do sleep 1; done; input keyevent 82'
$ANDROID_HOME/platform-tools/adb devices
echo "Emulator started"
...

Debug Tip:

One disadvantage of coming down from cloud computing into a MacMini, is the configuration and cache data from repeated test runs, especially, if it involves deep links to different environments.
To avoid this issue, as you might have noticed, the workflow script above deletes any emulator present in the MacMini and creates a new one per run.
In case you still face the issue, please add the following commands

watchman watch-del-all
rm -rf $TMPDIR/react-native-packager-cache-*
rm -rf $TMPDIR/metro-bundler-cache-*
cd; cd .gradle/caches; rm -rf *
npm cache clean — force
cd android && bash gradlew clean && cd ..

Happy Test Automation! See you soon with steps for iOS Simulator.

--

--

Sandeep Dinesh
Sandeep Dinesh

Written by Sandeep Dinesh

Test Automation, CI/CD, DevOps & SRE

No responses yet