Espresso Introduction
Espresso is an open source instrumentation Testing framework made available by Google for the ease of UI Testing.
It is used to automate manual test case for android application.
Espresso automatically syncronizes your test actions with the ui of your application.The framework also ensures that your activity is started before the tests run.It also let the test wait until all observed background activities have finished.
The main components of Espresso include the following:
- Espresso – Entry point to interactions with views (via onView() and onData()). Also exposes APIs that are not necessarily tied to any view, such as pressBack().
- ViewMatchers –(Match a view) Allows you to find view in the current view hierarchy.
- ViewActions – (Perform an action)Allows you to perform an action on a
View.
- ViewAssertions – (Assert and verify the result)Check the state of the
View
to see if it reflects the expected state or behavior defined by the assertion.
Example:
onView(withId(R.id.my_view)) // withId(R.id.my_view) is a ViewMatcher
.perform(click()) // click() is a ViewAction
.check(matches(isDisplayed())); // matches(isDisplayed()) is a ViewAssertion
Espresso setup instructions
To use Espresso, you must already have the Android Support Repository installed with Android Studio. You may also need to configure Espresso in your project.
Check for the Android Support Repository
1. Open the project in Android Studio, and choose Tools > Android > SDK Manager.
The Android SDK Default Preferences pane appears.
2. Click the SDK Tools tab and expand Support Repository.
3. Look for Android Support Repository in the list.
If Installed appears in the Status column then click Cancel.
If Not installed or Update Available appears, click the checkbox next to Android Support Repository. A download icon should appear next to the checkbox. Click OK.
4. Click OK again, and then Finish when the support repository has been installed.
Configure Espresso for the project
When you start a project for the phone and tablet form factor using API 15: Android 4.0.3 (Ice Cream Sandwich) as the minimum SDK, Android Studio automatically includes the dependencies you need to use Espresso.
If you have created your project in a previous version of Android Studio, you may have to add the dependencies and instrumentation runner yourself. To add the dependencies yourself, follow these steps:
- Open your project in Android Studio in which you want to configure Espresso .
- Open the build.gradle (Module: app) file.
- Check if the following is included (along with other dependencies) in the dependencies section:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
If the file doesn’t include the above dependency statements, enter them into the dependencies section.
- Android Studio also adds the following instrumentation statement to the end of the defaultConfig section of a new project:
defaultConfig {
applicationId "com.c1ctech.instrumentationtestdemo"
minSdkVersion 21
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
If the file doesn’t include the above instrumentation statement, enter it at the end of the defaultConfig section.
- If you modified the build.gradle (Module: app) file, click the Sync Now link in the notification about Gradle files in top right corner of the window.
Example Gradle build file
apply plugin: 'com.android.application'
android {
compileSdkVersion 27
defaultConfig {
applicationId "com.c1ctech.instrumentationtestdemo"
minSdkVersion 15
targetSdkVersion 27
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
Turn off animations on your test device
Android phones and tablets display animations when moving between apps and screens. The animations are attractive when using the device, but they slow down performance, and may also cause unexpected results or may lead your test to fail. So it’s a good idea to turn off animations on your virtual or physical devices used for testing. On your device, go to Settings > Developer options>Drawing and disable the following 3 settings:
- Window animation scale
- Transition animation scale
- Animator duration scale
Add the first test
- In android studio it is important to write instrumented functional test cases in src/androdTest/java folder and unit tests in src/Test/java.
-
Open your project then go to src/androidTest/java/com.c1ctech.instrumentationtestdemo(package name)->New->Java Class give Class Name TestClass and click on OK.
After creating the TestClass you are ready to start writing your test cases.
@RunWith(AndroidJUnit4.class)
@LargeTest
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class HelloWorldEspressoTest {
@Rule
public ActivityTestRule<MainActivity> mActivityRule =
new ActivityTestRule<>(MainActivity.class);
@Before
public void setUp() throws Exception {
//Before Test case execution
}
@Test
public void CheckHelloWorldDisplay() {
onView(withId(R.id.hello_world)).check(matches(isDisplayed()));
}
@After
public void tearDown() throws Exception {
//After Test case Execution
}
}
Run the example test
Android Studio creates a blank Espresso test class when you create the project.
- In the Project > Android pane, open java >com.c1ctech.instrumentationtestdemo(androidTest), and open ExampleInstrumentedTest.
The project is supplied with this example test. You can create as many tests as you wish in this folder.
- To run the test, right-click (or Control-click) ExampleInstrumentedTest and choose Run ExampleInstrumentedTest from the pop-up menu. You can then choose to run the test on the emulator or on your device.
The Run pane at the bottom of Android Studio shows the progress of the test, and when finishes, it displays “Tests ran to completion.” In the left column Android Studio displays “All Tests Passed”.
I hope this tutorial will help you in understanding the basics of espresso testing and also how to do UI testing using espresso in our applications.