Android Espresso Testing Framework
- General
Android Espresso Testing Framework
As mobile developers, we spend most of our time creating new screens or changing screens that already exist and we need to know that the code works. That is why in our development process we must use tools to verify that our application continues to work as expected and that our development meets the quality of the desired product according to the user story. And in this article, we will talk about Espresso Testing.
Testing in Android majorly involves two types:
1.Unit Tests
2.Instrumentation Tests
Espresso comes under the second type. It is used to do automated UI testing by writing short and concise Android UI tests.
Getting Started With Espresso Tests
Add the following dependencies in the build.gradle file:
1 2 3 |
androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test:rules:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' |
Add the following in the defaultConfig block :
1 2 3 4 5 6 7 8 |
android ... defaultConfig{ ... testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } .. } |
Espresso has basically three components:
1.ViewMatchers -Finds the view
2.ViewActions – Performs an action on the view
3.ViewAssertions – Validates a assertioin
1 2 3 |
onView(ViewMatcher) .perform(ViewAction) .check(ViewAssertion) |
Inside the onView we look for the id of the View.
It can be done using methods like:
1 2 |
withId() – Pass the unique id withText() – Pass the text of the view. It searches for a view with the specified text. |
Inside perform we pass the action to be done on the View. Example:
1 2 3 4 |
click() – Clicks the view passed in onView. typeText() – Pass the string to be entered in the View. This is especially used in EditText. replaceText() – Replaces the current text with the string that’s passed. closeSoftKeyboard() – Dismisses the keyboard. |
Inside check we assert the states mainly using methods:
1 |
matches<br>doesNotExist |
Writing Espresso Tests
Espresso tests are written inside src | androidTest folder files.
By default, ExampleInstrumentedTest.java is created with a default test.
Let’s write one test:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
@RunWith(AndroidJUnit4.class) public class ExampleInstrumentedTest { public static final String USERNAME_TYPED = "Pankaj"; public static final String LOGIN_TEXT = "Hello Pankaj"; @Test public void useAppContext() { // Context of the app under test. Context appContext = InstrumentationRegistry.getTargetContext(); assertEquals("com.auriga", appContext.getPackageName()); } @Rule public ActivityTestRule<MainActivity> mActivityRule = new ActivityTestRule<>( MainActivity.class); @Test public void loginClickedSuccess() { onView(withId(R.id.inUsername)) .perform(typeText(USERNAME_TYPED)); onView(withId(R.id.inNumber)) .perform(typeText("12345")); onView(withId(R.id.inConfirmNumber)) .perform(typeText("12345")); onView(withId(R.id.btnLogin)).perform(click()); onView(withId(R.id.txtLoginResult)).check(matches(withText(LOGIN_TEXT))); } } |
@Rule annotation is used to launch the MainActivity.
Inside loginClickedSuccess we wrote a successful login test case.
Now let’s write two more tests in which we will test whether the Toast is shown correctly or not.
Add the following methods inside the above class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
@Test public void shouldShowToastError() { onView(withId(R.id.inUsername)) .perform(typeText(USERNAME_TYPED)); onView(withId(R.id.inNumber)) .perform(typeText("123456")); onView(withId(R.id.inConfirmNumber)) .perform(typeText("12345"), closeSoftKeyboard()); onView(withId(R.id.btnLogin)).perform(click()); onView(withText(R.string.toast_error)).inRoot(withDecorView(not(is(mActivityRule.getActivity().getWindow().getDecorView())))).check(matches(isDisplayed())); } @Test public void shouldShowToastUsernameEmpty() { onView(withId(R.id.inNumber)) .perform(typeText("12345")); onView(withId(R.id.inConfirmNumber)) .perform(typeText("12345")); onView(withId(R.id.btnLogin)).perform(click()); onView(withText(R.string.username_empty)).inRoot(withDecorView(not(is(mActivityRule.getActivity().getWindow().getDecorView())))).check(matches(isDisplayed())); } |
The last line is used to check whether Toast is displayed with the correct string or not.
To run the espresso test. Right click on the ExampleInstrumentationTest.java and press run to start the automated test on the emulator/physical device.
Espresso Test Recording
You can record Espresso tests manually as well:
1.Click on Run.
2.Agter that Click Record Espresso Test.
It will automatically create a new file with the espresso tests containing the manual clicks and actions you did in the device.
Espresso resources
https://developer.android.com/training/testing/espresso
https://developer.android.com/training/testing/ui-testing/espresso-testing
https://developer.android.com/studio/test/espresso-test-recorder
Related content
Auriga: Leveling Up for Enterprise Growth!
Auriga’s journey began in 2010 crafting products for India’s