Here is a crazy idea: let’s improve git push to make our CI run Espresso tests on a real device, powered by Sauce Labs Real Device Cloud (RDC). That’s right. Every time we have a change in our app code, a real Google Pixel phone will launch our test suite and show us the results, all without leaving the comfort of our GitHub repo. This is how we test our mobile SDKs, and it is how you should test your app too!
Sauce Labs data centers host a vast number of real mobile devices, accessible by developers to ensure their apps can confidently run across all Android phones in the wild. You can harness Real Device Cloud devices with any testing framework of your choice.
As of 2021, Google Play shows there are at least 15,000 different Android device models people use all around the world. We can’t simply buy all of those devices and test TestFairy SDK in each one of them. It is not a race we can ever hope to win without the help of a robust service. If we want to be confident that TestFairy SDK will run without bugs on any device, going cloud is our best shot. Below, you will find the exact code for how we achieve confidence for our SDK with the help of Sauce Labs RDC and GitHub Actions.
Let’s start with configuring our repo for saucectl. It is the tool we use under the hood to get access to RDC. saucectl cli takes a look at our current directory (i.e., in CI) to decide what kind of tests to run, on which devices with the criteria we define.
Create a new folder named “.sauce” in the project root and put a configuration file named “config.yml” in it.
We will need to define the following items: testing framework, data center region, app paths, real phone specification.
1apiVersion: v1alpha2kind: espresso3sauce:4region: us-west-15# Controls how many suites are executed at the same time (sauce test env only).6concurrency: 27metadata:8name: Testing My Demo App Android with espresso9tags:10- e2e11- release team12- other tag13build: Release $CI_COMMIT_SHORT_SHA14espresso:15app: ./app/build/outputs/apk/debug/app-debug.apk16testApp: ./app/build/outputs/apk/androidTest/debug/app-debug-androidTest.apk17suites:18- name: "Main suite"19devices:20- name: "Google Pixel 3a"21platformVersion: 1122- id: Google_Pixel_3a_real23# Controls what artifacts to fetch when the suite on Sauce Cloud has finished.24artifacts:25download:26when: always27match:28- junit.xml29directory: ./artifacts/
Once you finish, you can execute saucectl run locally and see your test results on your Sauce Labs dashboard in real time.
This is already really cool but we want to do more. Let’s use GitHub Actions to run these tests, so that every time a developer makes an update to our repo, the tests are automatically executed.
Our GitHub repo doesn’t know that we use Sauce Labs yet, so let’s add that.
Navigate to your project secrets: https://github.com/MY_ORGANIZATION/MY_APP/settings/secrets/actions
Add a repository secret named SAUCE_USERNAME and enter your saucelabs.com username in it.
Similarly, add another repository secret named SAUCE_ACCESS_KEY and paste your access key in it.
Now that our repo knows about our Sauce credentials, we can connect to RDC inside all of our GitHub workflows. For a basic “test on push” scenario, we define the following workflow file (.github/workflows/test-with-saucectl.yml).
1name: Test with saucectl2on: [push]3env:4SAUCE_USERNAME: ${{secrets.SAUCE_USERNAME}}5SAUCE_ACCESS_KEY: ${{secrets.SAUCE_ACCESS_KEY}}6jobs:7main:8timeout-minutes: 209runs-on: ubuntu-latest10steps:11- name: Checkout12uses: actions/checkout@v213- name: Setup Java14uses: actions/setup-java@v115with:16java-version: 1117- name: Setup Android SDK18uses: android-actions/setup-android@v219- name: Compile20run: |21./gradlew app:assembleDebug22./gradlew app:assembleDebugAndroidTest23shell: bash24- name: Test espresso with saucectl25uses: saucelabs/saucectl-run-action@v126with:27testing-environment: ""28concurrency: 1029
Finally, we commit our changes and push.
Once our changes are merged to the main branch for the first time, GitHub will be able to start scheduling actions whenever a new contribution arrives.
If you’d like to see this in action right now, just fork our open source demo project on GitHub and update your repo secrets.
This post was originally published on October 27, 2021 and has been updated in April 2023.