For Android development teams using the Espresso test framework, the Perfecto REST API enables execution of tests on real devices in the Perfecto cloud. In this example, we’ll show how to introduce Espresso testing into a post-build action in Jenkins, though the concepts apply equally in any CI system.

Prerequisites

  1. Running Jenkins instance (and a user with Project creation permissions).

    Or you can run an isolated version of this example on Docker by following these steps.

  2. Install the Github plugin on Jenkins
    https://wiki.jenkins-ci.org/display/JENKINS/GitHub+Plugin
  3. Install Android SDK build tools 24.0.1 on Jenkins build node used to compile project

    android update sdk --no-ui --all --filter platform-tools, android-24,build-tools-24.0.1,tools,extra-android-support,extra-android-m2repository

The Big Picture
If you’re into flow diagrams, this is what the continuous integration process will look like:

Create a new Jenkins project

  1. From your Jenkins home screen, click ‘New Item
  2. Enter ‘AndroidDemoApp’ for a project name, select Freestyle project, then click ‘OK’.

  3. Under the ‘Source Code Management’ section, select ‘Git’, paste in the following URL:
    https://github.com/PerfectoCode/AndroidDemoApp.git


     

  4. Under the ‘Build’ section, add a new Build Step to ‘Invoke Gradle Script

     

  5. Under the new ‘Invoke Gradle script’ section, select ‘Use Gradle Wrapper’.

    Enter ‘assembleDebug assembleAndroidTest’ for Switches, ‘${workspace}’ for Root Build script, and ‘build.gradle’ for Build File.

  6. Kick off the UI tests after build by adding an additional ‘Execute shell’ build step with the following commands:

#!/bin/bash
echo ${WORKSPACE}
cd ${WORKSPACE}
curl -s https://raw.githubusercontent.com/paulsbruce/AndroidDemoApp/master/testE... > ./Perfecto_API.sh
chmod u+x ./Perfecto_API.sh
bash Perfecto_API.sh "${WORKSPACE}" "app-debug" "com.example.perfecto.tipcalculator.test" "<your_cloud>.perfectomobile.com" "<your_perfecto_username>" "<your_perfecto_password>" "/" "<num_of_devices>"
exit $?

The above shell commands retrieve an additional script that contains the logical API calls to upload the compiled app binaries to your private Perfecto artifact repository, run the Espresso tests, and produce a report containing a summary of the execution details.

You will need to replace the values wrapped in angle brackets with your own cloud details and credentials using an account whose role has been granted automation permissions.

  1. Press save, then click ‘Build Now’

  2. In the Build History window, under the latest build drop-down menu, select ‘Console Output’ to review successful build results.

  3. You can now view the interactive console output of the build (and Espresso test) process while it is still running:

  4. Click on the execution report URL in the console output to open the final report up in your browser:

Congratulations! You have successfully built an Android app in Jenkins and run Espresso tests to verify the build on real devices in the Perfecto Lab. This is a huge step in speeding up the feedback loop between making code changes and making sure an app is working brilliantly!