UI Test in Android

UI testing in Android refers to the process of verifying the functionality and appearance of the User Interface (UI) of an Android app. It ensures that the app behaves as expected and meets the specified requirements in terms of functionality, usability, and accessibility.

UI testing in Android is done by creating test cases that run on an emulator or a real device, interact with the app, and check for any unexpected behavior or appearance issues. It's an important aspect of software development as it helps in catching bugs and fixing them early in the development cycle, thereby reducing the cost and time required to fix them in the later stages.

UI testing in Android can be performed using various tools and frameworks such as Espresso, Roboelectric, and UI Automator. Espresso is a popular testing framework that provides APIs to write UI tests for Android apps. It's designed to be fast and reliable and is used by Google to test its own Android apps.

In Espresso, tests are written in Java or Kotlin and can be run on both emulators and real devices. A test case in Espresso typically consists of the following steps:

  1. Launch the app and navigate to the desired screen.
  2. Perform actions on the UI elements, such as clicking a button, scrolling, or typing in a text field.
  3. Verify the results of the actions, such as checking if a certain text is displayed or a certain screen is displayed.
  4. Clean up after the test, such as closing the app.


To write a UI test using Espresso, you will need to:

  1. Create an Android Test project: This will contain your test code and resources.
  2. Add the Espresso library to your project: You can do this by adding the following dependency in your app's build.gradle file:

androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'         

  1. Write test cases: You can write tests that interact with your app's UI by performing actions such as clicking on buttons or entering text into fields.
  2. Run the tests: You can run your tests on an emulator or a physical device. You can also run tests in parallel on multiple devices using Firebase Test Lab.

Here's an example of a simple UI test using Espresso:


@Test
fun testLoginSuccess() {
    // launch the activity
    val activityScenario = ActivityScenario.launch(MainActivity::class.java)

    // enter username
    onView(withId(R.id.username))
        .perform(typeText("test_user"), closeSoftKeyboard())

    // enter password
    onView(withId(R.id.password))
        .perform(typeText("password"), closeSoftKeyboard())

    // click the login button
    onView(withId(R.id.login)).perform(click())

    // check if the login is successful
    onView(withId(R.id.welcome_message))
        .check(matches(withText("Welcome, test_user!")))
}        

This test launches the MainActivity, enters the username and password, clicks the login button, and finally checks if the login was successful by checking if the welcome message is displayed.

To view or add a comment, sign in

More articles by Ali Ahmed

Others also viewed

Explore content categories