UI for Multiple Application



Android supports user interface testing that involves more than one application. Let us consider our application have an option to move from our application to messaging application to send a message and then comes back to our application. In this scenario, UI automator testing framework helps us to test the application. UI automator can be considered as a good companion for espresso testing framework. We can exploit the intending() option in espresso testing framework before opting for UI automator.

Setup Instruction

Android provides UI automator as a separate plugin. It needs to be configured in the app/build.gradle as specified below,

dependencies {
   ...
   androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
}

Workflow for Writing Test Case

Let us understand how to write a UI Automator based test case,

  • Get UiDevice object by calling the getInstance() method and passing the Instrumentation object.

myDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
myDevice.pressHome();
  • Get UiObject object using the findObject() method. Before using this method, we can open the uiautomatorviewer application to inspect the target application UI components since understanding the target application enables us to write better test cases.

UiObject button = myDevice.findObject(new UiSelector()
   .text("Run")
   .className("android.widget.Button"));
  • Simulate user interaction by calling UiObject’s method. For example, setText() to edit a text field and click() to fire a click event of a button.

if(button.exists() && button.isEnabled()) {
   button.click();
}
  • Finally, we check whether the UI reflects the expected state.

Advertisements