AWS Mobile Blog

Getting started with Android testing on AWS Device Farm using Espresso – Part 1: Building a sample application

by Richard Threlkeld | on | Permalink | Comments |  Share

AWS Device Farm is a service that allows you to test your Android, Fire OS, and iOS apps on real devices (not emulators or simulators) in the AWS Cloud. You can upload your apps to the cloud and run Fuzz tests to simulate random activity or leverage the built-in app explorer, which crawls your app and captures screenshots as well as performance metrics and logs. To take further advantage of the service you’ll need to leverage one of the supported test automation frameworks. That’s where this series of blog posts comes in.

In this three-part series we will show you how to create a basic Android application that leverages Espresso as a testing framework and runs tests across a pool of devices in the AWS Cloud. The application has a text entry field where the user can type information and a button so that when pressed the entered information shows on the screen. This can be thought of as a basic application feature that you’ll write an automated test in Espresso to validate.

In Part One you will create the application and run it locally. Part Two covers test creation and Part Three will cover cloud execution with Device Farm.

Prerequisites

This guide assumes that you have Android Studio installed with the Android SDK configured and you have a working emulator from the Android Virtual Device (AVD) Manager.

Open the SDK Manager and scroll down to “Extras” at the bottom. Ensure that the “Android Support Repository” is installed.

Gradle

If you are not already familiar with Gradle please take a moment to familiarize yourself before going any further: https://gradle.org/getting-started-android/

Android Studio

Start by creating a new Project in Android Studio. Name the application SimpleDemoApp.

Important: For this demo ensure the Company Domain is exactly like the image above.

For this example we used KitKat as the minimum SDK version but note Espresso will support previous versions as well. Select the Blank Activity template and change the Activity name to “SimpleDemoActivity”.

Click finish and the project will be created. Open the application’s main layout file (activity_simple_demo.xml).

If you don’t see the above go to View->Tool Windows->Project and use the navigation bar on the left.

Click the text view towards the bottom of Android Studio and replace the XML with the following:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView android:text="Some Text!"
        android:id="@+id/updatedText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click to update text"
        android:id="@+id/buttonUpdate"
        android:layout_below="@+id/updatedText"
        android:layout_alignParentStart="true" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText"
        android:layout_below="@+id/buttonUpdate"
        android:layout_alignParentStart="true"
        android:layout_marginTop="52dp"
        android:layout_alignParentEnd="true" />

</RelativeLayout>

Save the file. This will create an area to input text, a button, and an area to display the text. Now we need to wire up these objects to a controller. Open the main Activity class (SimpleDemoActivity.java) from the navigation pane.

Replace the contents with the following code:

package com.example.simpledemoapp;

import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class SimpleDemoActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_simple_demo);

        Button updateButton = (Button)findViewById(R.id.buttonUpdate);
        updateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView textToUpdate =
                    (TextView)findViewById(R.id.updatedText);
                EditText enteredText = (EditText)findViewById(R.id.editText);
                textToUpdate.setText(enteredText.getText().toString());
            }
        });

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if
        // it is present.
        getMenuInflater().inflate(R.menu.menu_simple_demo, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

Save the file. Now run your application  either on an emulator or real device. When starting up an emulator this can take a few minutes. Once running, you should be able to input some text, click the button and have it show up on the screen.

Conclusion

That’s it for Part One! Now join us for Part Two of this series where you will modify this application to use Espresso for building test cases and take screen shots. If you have questions please join us in the comment section below.