Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

android
ui
communitycreator

What is Android UI testing?

Adisa Abass

As with all software products, the importance of testing in Android development can’t be overemphasized. Users interact with Android applications on a variety of levels, from pressing a button to downloading information onto their device. So, a defect in an application’s functionalities may lead to problems for the user and user dissatisfaction. Hence, it is of the utmost importance that an Android application goes through appropriate testing before release.

Types of Testing in Android Development

  • UI testing

  • Integration Testing

  • Unit testing

UI (User Interface) testing will be discussed in this shot.

As stated earlier, users interact with Android applications on a variety of levels. However, a user is really just concerned about their interaction with the user interface elements that trigger various functionalities of the application. Therefore, it is important to test for the functionalities of all UI components in an Android application, from the click of a button, to navigation, to various screens, etc.

Therefore, we can say that UI testing in Android development ensures that an application’s user interface components meet its functional requirements and achieve a high standard of quality. This way, it is more likely to be successfully adopted by users.

Programmers often shy away from carrying out tests, because of their complexity, and default to manual human testing to perform a set of user operations on the target app. This procedure is often time-consuming and error-prone. A much better approach is to write UI tests, where user actions are performed in an automated manner, as this approach is more reliable.

To write UI tests in Android, the Espresso framework and an Emulator/Physical Android device are utilized.

Espresso has three basic components, namely:

  • View Matchers: This class allows to find views in the current view hierarchy.
  • View Actions: This class allows to perform actions on the views.
  • View Assertions: This class aids view state assertions.

To write a simple UI test:

  • Import Espresso dependencies in your Application’s app.gradle file.
  • Generate a test class in the java/androidTest package.
  • Declare and initialize dummy variables to use for tests in a setup() function preceded with @Before annotation
  • Declare a test function preceded with the @Test annotation
  • You can find what UI components you want to test using the View Interaction and View Matchers methods (for example, onView and withId)
  • User specific interactions can be simulated using the the View Interaction and View Action methods. For example, .perform(click())means performs a click on a UI component
  • Finally, View Assertion methods are used to validate the behavior of the UI components after simulating user specific interactions. This generates a passed or failed result.

The code snippet below shows how to write and simulate a basic UI test:

@RunWith(AndroidJUnit4ClassRunner::class)
class MainActivityTest{

    lateinit var firstName: String
    lateinit var surname: String
    lateinit var firstNameIcon: String
    lateinit var phone: String
    lateinit var email: String

    @Before
    fun setup(){
        firstName = "Abass"
        surname = "Adisa"
        firstNameIcon = firstName.first().uppercaseChar().toString()
        phone = "08094145784"
        email = "abasstoy14@gmail.com"
        val activityScenario = ActivityScenario.launch(MainActivity::class.java)
    }


    //This is a sample of a UI test to validate whether all UI components in an activity are displaying as intended
    @Test
    fun ui_test_for_mainActivity_components_displays(){
        onView(withId(R.id.toolBar_main)).check(matches(isDisplayed()))
        onView(withId(R.id.addBtn)).check(matches(isDisplayed()))
        onView(withId(R.id.topView_main)).check(matches(isDisplayed()))
        onView(withId(R.id.navBtn_main)).check(matches(isDisplayed()))
        onView(withId(R.id.nextBtn)).check(matches(isDisplayed()))
    }


    //This is a sample of a simulated User specific interaction on a form
    @Test
    fun ui_test_for_contact_app_to_insert_contact(){
        onView(withId(R.id.addBtn)).perform(click())
        onView(withId(R.id.firstNameTxt)).perform(typeText(firstName), closeSoftKeyboard())
        onView(withId(R.id.surNameTxt)).perform(typeText(surname), closeSoftKeyboard())
        onView(withId(R.id.phoneTxt)).perform(typeText(phone), closeSoftKeyboard())
        onView(withId(R.id.mailTxt)).check(matches(isDisplayed()))
        onView(withId(R.id.save)).perform(click())
    }

}

RELATED TAGS

android
ui
communitycreator
RELATED COURSES

View all Courses

Keep Exploring