Tag: AWS Device Farm
Getting started with Android testing on AWS Device Farm using Espresso – Part 2: Setting up Espresso and taking screenshots
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 Part One of this series you created a basic application. Now, in Part Two, you will create test cases in Espresso to validate functionality and take screenshots before uploading to the AWS Cloud. In Part Three you will run your app and tests in the AWS Cloud with AWS Device Farm. Let’s get started.
Gradle
Open the build.gradle for the app in the main navigation bar.

Replace the text with the following:
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.example.simpledemoapp"
minSdkVersion 19
targetSdkVersion 22
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
packagingOptions{
exclude 'LICENSE.txt'
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.1'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.0'
}
Save your changed file. On the right hand side of Android Studio you will see a tab called "Gradle." Click it and then right-click the root item and select "Refresh external project."
Expand SimpleDemoApp/SimpleDemoApp/Tasks/build and double-click “assembleAndroidTest."

Next, switch to the "Tests" view in the Project navigator.

Expand app/app/src/androidTest/java until you get to the com/example/simpledemoapp package structure.

Right-click the package and select New->Java Class. Type “EspressoTest” for the name and click OK.
Replace the contents of the file with the below code:
package com.example.simpledemoapp;
import android.test.ActivityInstrumentationTestCase2;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.action.ViewActions.click;
import static android.support.test.espresso.action.ViewActions.typeText;
import static android.support.test.espresso.assertion.ViewAssertions.matches;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.test.espresso.matcher.ViewMatchers.withText;
public class EspressoTest extends ActivityInstrumentationTestCase2 {
public EspressoTest() {super(SimpleDemoActivity.class);}
@Override
public void setUp() throws Exception {
super.setUp();
getActivity();
}
public void testEspresso(){
onView(withId(R.id.editText)).perform(typeText("Testing with AWS Device Farm
onView(withId(R.id.buttonUpdate)).perform(click());
onView(withId(R.id.updatedText)).check(matches(withText("Testing with AWS Device Farm")));
}
}
Save the file.
In the top navigation bar select Run->Edit Configurations.

Click the plus (+) sign to add an Android Test configuration node. Click on that node and press plus again to add a new configuration for EspressoTest. Ensure that the Class matches and the Instrumentation Runner is android.support.test.runner.AndroidJUnitRunner.

Now Right-Click the EspressoTest class in the navigation pane->Run->EspressoTest.

If everything runs successfully you will see the test run in the emulator. You might need to click fast to bring it in focus.

Also you should see “Done 1 of 1” in the Run section of the console for successful test execution with Espresso.

IMPORTANT: If you created a test in your Class and your output doesn’t match the above screenshot, or you get a message such as “No test events received,” it may be due to how you named the test. Tests must start with the name “test” such as “testEspresso()” in the above example or the test runner will not recognize them.
Screenshots
At this point we have a very basic application that technically can be uploaded and tested in Device Farm. However, according to the documentation we need to save screenshots to the SD Card in order for them to be seen after a Run. If the Espresso .check() call fails we would like to see a screenshot of what the screen looked like in order to troubleshoot.
To accomplish this task we’ll create a utility class that will save PNG files to /sdcard/test-screenshots/ as per the Device Farm documentation. The class will take an input of the Activity and a string identifier for the file name.
Select File->New->Java Class and when prompted name it "ScreenShot." IMPORTANT: This name must be precise including capitalization so use a capital "S."

Paste in the following code:
package com.example.simpledemoapp;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class ScreenShot {
private static final String TAG = "SCREENSHOT_TAG";
public static void take(Activity activity, String name) {
final String dir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/test-screenshots/";
final String path = dir + name;
File filePath = new File(dir); // Create directory if not present
if (!filePath.isDirectory()) {
Log.i(TAG, "Creating directory " + filePath);
filePath.mkdirs();
}
Log.i(TAG, "Saving to path: " + path);
View phoneView = activity.getWindow().getDecorView().getRootView();
phoneView.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(phoneView.getDrawingCache());
phoneView.setDrawingCacheEnabled(false);
OutputStream out = null;
File imageFile = new File(path);
try {
out = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
}
catch (FileNotFoundException e) {
Log.e(TAG, e.toString());
}
catch (IOException e) {
Log.e(TAG, e.toString());
}
finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
Log.e(TAG, e.toString());
}
}
}
}
Now modify testEspresso()in the EspressoTest.java class to make a call to the newly created ScreenShot class:
public void testEspresso() throws IOException {
Log.i("TESTESPRESSO", "Starting the Espresso Test");
onView(withId(R.id.editText)).perform(typeText("Testing with AWS Device Farm
onView(withId(R.id.buttonUpdate)).perform(click());
onView(withId(R.id.updatedText)).check(matches(withText("Testing with AWS Device Farm")));
// Take the screenshot
ScreenShot.take(getActivity(),"Homepage");
}
You will also need to modify the AndroidManifest.xml file so that your application has permission to write and read from the SD card. In the Project Navigator expand app/src/main and double-click AndroidManifest.xml. Add in the "uses-permission" entries as in the example XML below.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.simpledemoapp" >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".SimpleDemoActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Run the test again in the emulator but this time after it finishes open the Android Device Monitor from the Android Studio task bar.

In the Android Device Monitor you will find a tab called "File Explorer." Select this and navigate to /storage/sdcard/test-screenshots. If you see a file called "Homepage" then you have run everything successfully. If you wish you can click on the file and download the image for viewing.

Conclusion
That’s it for Part Two! Now join us for Part Three of this series where you will upload your application to AWS Device Farm and see your tests run across multiple devices in the AWS Cloud. If you have questions please join us in the comment section below.
Getting started with Android testing on AWS Device Farm using Espresso – Part 1: Building a sample application
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.
Test user flows through your Android app with AWS Device Farm’s built-in app explorer. No scripts required.
Starting today, you can view details about how your Android app performs on real devices in the AWS Cloud without writing your own test scripts. AWS Device Farm’s new Built-in: Explorer test deploys an app explorer that crawls your application, analyzes each view it encounters, and interacts with each view’s controls as an end user would. The app explorer captures screenshots, logs, and performance data as it explores your app. All results and artifacts are collated into a Device Farm report and also made available through the API.
The app explorer works with both native and web controls, and will tap buttons, insert placeholder text into text inputs, and open and interact with menus. It captures a screenshot and logs each interaction that was performed. In addition, if your app requires an account login, you can provide credentials that the app explorer will use if it finds the login form.
Getting started
1. First, login to the AWS Device Farm console. If you’re new to Device Farm, don’t worry. You can get started with a one-time free trial of 250 minutes.
2. From within the Device Farm console, go to a Device Farm project and start the “Create a new run” wizard by clicking the “Create a new run” button. If it’s a brand new project, the wizard will start automatically.
3. Next, upload your Android app (.apk).
4. On the Configure Test step, select “Built-in: Explorer” from the test types. If you have a login form within your app, you can use the username and password fields to specify the credentials the app explorer should use.
5. Click “Next” until you’re on the Review and Start Run step. If everything looks okay, click “Confirm and start run.”
Analyzing results
As each device completes its Built-In: Explorer test, the results will be pulled into the report. You can view all screenshots in a single place on the report’s summary view by clicking on the “Screenshots” tab. You can also drill down to a specific device by clicking on the desired device from the report summary view in the "Devices" section.

View logs, screenshots, and performance data for a specific device by selecting it from the report summary view
From within the device view, you can analyze logs, performance data, and screenshots collected by the app explorer for the specified device.

Viewing logs captured by the app explorer on a specific device

Viewing screenshots captured by the app explorer on a specific device
Conclusion
The app explorer enables you to test basic user flows through your Android application without writing test scripts, allowing you to get started with automated testing faster and test more often during development. In addition to starting the Built-in: Explorer test from the console and SDKs, you can automatically kick off app explorer tests in your Jenkins continuous integration environment with the Device Farm Jenkins plugin.
You can also augment the app explorer with your own custom tests, which allow you to exercise specific functionality within your app and take screenshots exactly where you want. Custom tests are complimentary to the app explorer, which crawls your app without consideration for your app’s business logic. You can write custom tests in a number of frameworks like Espresso, Calabash, and Appium. Learn about supported framworks in the Device Farm documentation and get started quickly using the Device Farm Android app and sample scripts.
Please let us know if you have feedback or questions about the app explorer or AWS Device Farm in general in the comments or in the developer forum.