Home

Awesome

AWS Device Farm Sample App for Android

This is a sample native Android app that contains many of the stock Android components and elements, along with example Appium, Calabash, and Espresso tests. You can use the app and example tests as a reference for your own AWS Device Farm test scripts.

Notes

This project uses Butterknife in order to create Android views and view listeners through annotations.

Getting Started

In order to run this app on Device Farm, you will first need to create a local copy of this repository, open the project, and then build the APK from the source.

Examples for Testing Specific Scenarios

ComponentApp ImplementationEspressoCalabashAppium
Alerts: Toasts and Dialogssource codesource codesource codesource code
Fixturessource codesource codesource codesource code
Static Page: TextViewsource codesource codesource codesource code
Login Pagesource codesource codesource codesource code
Nested Views: Back and Up Navigationsource codesource codesource codesource code
Web Views<ul><li>Hybrid Web Views</li><li>Web View</li></ul><ul><li>Hybrid Web Views</li><li>Web View</li></ul><ul><li>Hybrid Web Views</li><li>Web View</li></ul><ul><li>Web View</li></ul>
An Expected Crashsource codesource codeNot implementedNot implemented

Examples for Native Features

FeatureAndroid ImplementationEspressoCalabashAppium
Camerasource codesource codesource codesource code
Image Collection Gridsource codesource codesource codesource code
Scroll Viewsource codesource codesource codesource code
Out of View Contentsource codesource codesource codesource code
Videosource codesource codesource codesource code

Examples for Inputs

ComponentAndroid ImplementationEspressoCalabashAppium
Checkboxsource codesource codesource codesource code
DatePickersource codesource codesource codeNot implemented (not directly supported by Appium)
EditTextsource codesource codesource codesource code
Gestures Inputsource codesource codesource codesource code
Pull to Refreshsource codesource codesource codesource code
Radio Buttonssource codesource codesource codesource code
TimePickersource codesource codesource codeNot implemented (not directly supported by Appium)
Toggle Buttonsource codesource codesource codesource code
Spinner Inputsource codesource codesource codesource code
Buttonssource codesource codesource codesource code

Examples for Navigation

ComponentAndroid ImplementationEspressoCalabashAppium
Navigation Drawersource codesource codesource codesource code
ViewPagersource codesource codesource codesource code

Android Tips and Tricks

Espresso

Setting Up and Running Espresso Tests

If you're just getting started, first read this guide.

Configuring Android Studio to Run Espresso Locally

You must set a custom Instrumentation run configuration to run your Espresso tests locally. You need to set the instrumentation runner to "android.support.test.runner.AndroidJUnitRunner"

<img src="https://github.com/awslabs/aws-device-farm-sample-app-for-android/blob/master/readme_images/espresso-android-studio-set-up.gif" width="1200">

Building the App and Test APK to Run on Device Farm

You will need two APKs: the app apk and the Espresso (Instrumentation) test apk.

Step 1: Go to your project directory

Open your terminal/command prompt and change your directory to your project folder.

Step 2: Build the project

Linux and OSX

Enter the following command inside the terminal prompt to build the project and test apks:

./gradlew cC

Windows

Enter the following command inside the command prompt to build the project and test apks:

gradlew.bat cC

Step 3: Find the APKS

<img src="https://github.com/awslabs/aws-device-farm-sample-app-for-android/blob/master/readme_images/find-apk.gif" width="400">

The app APK is called app-debug.apk and the test apk is app-debug-androidTest-unaligned.apk.

Follow the Device Farm Directions for Instrumentation in order to upload the APKs into the console and perform a test.

Strategies for Espresso

Waiting for Elements

Use Idling Resources in order to wait for elements within Espresso.

Examples of custom Idling Resources used within the Espresso tests:

<ul><li><a href="https://github.com/awslabs/aws-device-farm-sample-app-for-android/blob/master/app/src/androidTest/java/com/amazonaws/devicefarm/android/referenceapp/IdlingResources/VideoPlayerIdlingResource.java">VideoPlayerIdlingResource</a></li><li><a href ="https://github.com/awslabs/aws-device-farm-sample-app-for-android/blob/master/app/src/androidTest/java/com/amazonaws/devicefarm/android/referenceapp/IdlingResources/ViewPagerIdlingResource.java">ViewPagerIdlingResource</a></li><li><a href="https://github.com/awslabs/aws-device-farm-sample-app-for-android/blob/master/app/src/androidTest/java/com/amazonaws/devicefarm/android/referenceapp/IdlingResources/WebViewIdlingResource.java">WebViewIdlingResource</a></li></ul>

Custom Matchers

Use custom matchers in order to match your views to custom elements within your tests.

Examples of custom Matchers used within the Espresso tests:

<ul><li><a href="https://github.com/awslabs/aws-device-farm-sample-app-for-android/blob/master/app/src/androidTest/java/com/amazonaws/devicefarm/android/referenceapp/RegularExpressionMatcher.java">RegularExpressionMatcher</a></li></ul>

Tips

Using Espresso Recorder

Pre-Requisites

  1. Android Studio 2.3.2 or above
  2. Android Emulator downloaded or a physical Android device.

Steps:

  1. Download the AWS Device Farm Android sample app from GitHub.
  2. Open the project in Android Studio.
  3. We will generate Instrumentation tests using Espresso Recorder.
    • Select Run —> Record Espresso Test —> Select Deployment target as Android emulator or a real physical device </br>
    • Click Ok. This will open up the emulator where gradle will launch the app in the emulator. In addition it will open up a "Record your Test" window. </br>
  4. We will record a test for alert functionality within the app.
    • On the app running on emulator click on Menu button —> Alerts. You will notice that the recorder window records the UI interactions as shown below. </br>

      <img src="https://github.com/awslabs/aws-device-farm-sample-app-for-android/blob/master/readme_images/recorder-ui-interactions.png" width=375 height=500/>
    • We will add an assertion to check for the presence of "Alert" button on the page. In the "Record your Test" window click on "Add Assertion". This will expand the Recorder window with a snapshot of the app running in the emulator as shown below. </br>

      <img src="https://github.com/awslabs/aws-device-farm-sample-app-for-android/blob/master/readme_images/assertion-view.png" width=600 height=500 />
    • Select "Alert" button in the recorder window. This will auto populate the id of Alert button in the edit assertion section as shown below. Keep the assertion selection to default "exists" and click on Save Assertion. </br> <img src="https://github.com/awslabs/aws-device-farm-sample-app-for-android/blob/master/readme_images/recorder-save-assertion.png" width=600 height=500 />

    • After you click on Ok in the recorder window it will ask for a test class name under which your recorded test will be stored. Enter MyAlertTest. This will generate the test code and save it under MyAlertTest.java which you can view in the project explorer.

  5. Now we want to run the recorded tests locally. To do this we first want a configuration that will run Android Instrumented Tests.
    • Go to Run —> Edit Configurations.

    • Click on '+' sign on the left hand top corner and select "Android Instrumented Tests" as shown below. </br>

      <img src="https://github.com/awslabs/aws-device-farm-sample-app-for-android/blob/master/readme_images/edit-configuration.png" width=625 height=500 />
    • Name the configuration as "MyTestConfig". For the Class we provide the name of the class "MyAlertTest" under which we saved our tests. A completed configuration will look like below. Click Ok to save the configuration. </br>

      <img src="https://github.com/awslabs/aws-device-farm-sample-app-for-android/blob/master/readme_images/completed-configuration.png" width=625 height=500 />
  6. Run the tests by selecting "MyTestConfig" as the configuration and clicking on play. This will invoke the emulator and run your tests.
  7. We now want to build the app and tests apks so that you can run them on AWS Device Farm on multiple devices in parallel.
    • Open your terminal/command prompt and change your directory to your project folder.
    • Set JAVA_HOME to Java home on your machine.
    • For Mac/Linux run the following command inside the terminal prompt to build the project and test apks. This will run all the tests under the project on a connected device. Make sure you have at least one or emulator device connected.
./gradlew cC
gradlew.bat cC
  1. We can upload the app and test apks to AWS Device Farm as Instrumentation tests and run them against hundreds of devices in parallel. The pre-built app and tests can be found here.