Android UT and UI Testing basics Part — 3

Writing simple UI test using Espresso

SURYA N
5 min readAug 9, 2018

Human beings are prone to mistakes and this very nature of humans make software vulnerable to bugs, defects and errors. So before shipping your software, testing

  • can detect the errors and bugs made during the development
  • makes sure of the Customer’s reliability and their satisfaction in the application
  • ensures the quality of the product which helps in gaining the confidence of the customer

This article is about testing the UI of the Android applications.

“Testing leads to failure, and failure leads to Understanding”

Espresso UI Library

Espresso is an instrumentation Testing framework made available by Google for the ease of UI Testing.

To know more about the Espresso library checkout the below link.

The hierarchy flow of test execution remains same for the UI test as UT,

class Test {

@JvmField
@Rule

//TestRule of either ActivityTestRule or IntentTestRule
@BeforeClass
fun setUpClass(){
// run once before any of the tests
}
@Before
fun setUp(){
// run before each test
}
@Test
fun testCase(){
// respective validating the test
}
@After
fun tearDown(){
// run after each test
}
@AfterClass
fun tearDownClass(){
// run once after all tests
}
}

Important things to note is that

  • The activity will be launched using the @Rule before test code begins
  • By default the rule will be initialised and the activity will be launched(onCreate, onStart, onResume) before running every @Before method
  • Activity will be Destroyed(onPause, onStop, onDestroy) after running the @After method which in turn is called after every @Test Method
  • The activity’s launch can be postponed by setting the launchActivity to false in the constructor of ActivityTestRule ,in that case you will have to manually launch the activity before the tests

The @Rule that can be used in Espresso,

To start the Activity or Fragment UI test we should call TestRule, we have two types of TestRule they are,

  • ActivityTestRule
  • IntentTestRule.

ActivityTestRule

This rule provides functional testing of a single Activity. When launchActivity is set to true in the constructor, the Activity under test will be launched before each test annotated with Test and before methods annotated with Before, and it will be terminated after the test is completed and methods annotated with After are finished.

The Activity can be manually launched with launchActivity(Intent), and manually finished with finishActivity(). If the Activity is running at the end of the test, the test rule will finish it. To get more info checkout the link below,

IntentTestRule

This rule makes it easy to use Espresso-Intents APIs in functional UI tests. This class is an extension of ActivityTestRule, which initializes Espresso-Intents before each test annotated with Test and releases Espresso-Intents after each test run. The Activity will be terminated after each test and this rule can be used in the same way as ActivityTestRule To get more info checkout the link below,

The espresso test of a view contains

Steps involved in espresso tests
  1. Finding a View using a ViewMatcher
VIew Matchers

Espresso uses onView (Matcher<View> viewMatcher) method to find a particular view among the View hierarchy. onView() method takes a Matcher as argument. Espresso provides a number of these ViewMatchers which can be found in this Espresso Cheat sheet.

Every UI element contains properties or attributes which can be used to find the element.Suppose for finding an element with

android:id=“+id/login_button

We can write

Espresso.onView(withId(R.id.login_button))

2.Performing actions on the View

After finding the View you can perform actions on the View or on its Descendant using ViewActions of the Espresso.Some of the Most common actions are click(),clearText() etc.

Note: In cases where the View isn’t directly visible such as in a case where the view is in a scroll view and isn’t you will have to perform the scrollTo() function first and then perform the action.

Espresso.onView(withId(R.id.login_button)).perform(click())

3.Checking the ViewAssertions

After performing an action on the View we will want to see if the view behaves as we want, This can be done using check (ViewAssertion viewAssert)

Espresso.onView(withId(R.id.login_result)).check(matches(withText(R.string.login_success)))

Note : The flow of UI test execution is random it will not run in the order of code written hierarchy.

Sometimes the test will ran to execution faster or fails because of animation so I recommend to turn off the animation and transition of the target module/device under test.

Toggle the below fields to Animation off for successful test execution,

Settings -> Developer Options

  • Window animation scale
  • Transition animation scale
  • Animation duration scale
Simple UI test example

Checkout the full source code on this UI-UT project link below,

Thanks for reading!

Be sure to click claps below to recommend this article if you liked it.

Feel free to ask any doubts regarding this.

You can connect with me on Github, Linkedin :)

--

--

SURYA N

I am a Software Developer under Android domain in an organization. I have the knowledge on Android with JAVA, Kotlin and UI/UT for the Android development.