Appium Testing ReactNative for Android Emulator in CircleCI, Feb 2021
Couple of years back, I was tasked with investigating how to move my Appium based automated tests into the CircleCI build system, but sadly the platform didn’t allowed me to do that back then..
Come 2021, I re-evaluated CircleCI and setup an iOS simulator for my Appium tests, Tasting success there I knew with some patchwork, theoretically, Android Emulator can also be made happen.
So, here comes the good news — Yes, We can! Of course, not out of the box, but with few hacks and tricks.
So Let’s Jump to Code/ TLDR, Here’s the details of my android job in circleci config.
....android:macos: # indicate that we are using the macOS executorxcode: 11.7.0environment:JAVA_HOME: /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/HomeANDROID_SDK_ROOT: /usr/local/Caskroom/android-sdk/4333796steps:- checkout- run: npm install -g react-native-cli- run: |brew install --cask homebrew/cask-versions/adoptopenjdk8brew install gradlebrew install android-sdkbrew install android-platform-toolsbrew install --cask intel-haxm- run: |export PATH=$JAVA_HOME/bin:$PATHecho "y" | sdkmanager --install 'system-images;android-28;google_apis;x86_64'echo "no" | avdmanager create avd -n test_device -k 'system-images;android-28;google_apis;x86_64' --forceemulator -list-avdsls -la /Users/$USER/.android/avdsdkmanager "platform-tools" "platforms;android-28"- run:name: Starting emulator in backgroundcommand: export ANDROID_AVD_HOME=/Users/$USER/.android/avd && export ANDROID_HOME=$ANDROID_SDK_ROOT && $ANDROID_HOME/emulator/emulator -avd test_device -skin 1080x1920 -memory 1024 -engine qemu2 -netfast -no-audio -no-snapshot -accel onbackground: true- run: |adb wait-for-deviceadb devicesecho "Emulator started"- run: |rm -rf node_modulesnpm cache clean --forcerm -rf package-lock.json- run: npm install --save-dev- run: npm audit fix- run: npm install --save-dev chai- run:name: Starting metro in backgroundcommand: npm start --reset-cache &> metro-android.logbackground: true- run: |react-native run-android &> build-android.logsleep 30- run:name: Run E2E Tests - Androidcommand: npm run e2e-test:android -- Sanity &> test-execution-console-android.logno_output_timeout: 30m- store_artifacts:path: metro-android.logdestination: metro-android.log- store_artifacts:path: build-android.logdestination: build-android.log- store_artifacts:path: test-execution-console-android.logdestination: test-execution-console-android.log- store_artifacts:path: __tests__/e2e/test-resultsdestination: test-results-android....
Explanation
- The MacOS node doesn’t come with Android Studio and Intel Hardware Acceleration, unlike Github Actions. So manually we have to install them using homebrew and configure the corresponding environment variables — ANDROID_SDK_ROOT, ANDROID_HOME etc
- Android Studio, Gradle casks installs Open JDK 11 which is not compatible with Appium, so we have to explicity install the JDK8 and make it the default JAVA_HOME
- The ANDROID_SDK_ROOT is installed to /usr/local/Caskroom/android-sdk/<VERSION> by homebrew, /usr/local/Caskroom/android-sdk/4333796 as of January 2021, but this value will subsequently change with never versions. This hack needs to be reapplied then
- Lastly, the emulator command installed in PATH by homebrew is from Android SDK/ Tools folder which can’t really boot the test emulators, there we need to explicitly call the $ANDROID_HOME/emulator/emulator
Vola! The rest of the code are pretty intuitive where I install my app and since this is a webdriverIO project install a few dependencies.
The complete code can be found at https://github.com/sandeepqaops/HelloWorld
Happy Android Test Automation! Now even in CircleCI with Android Emulators and Appium!!!