Android, Java

Adding A UI Test Case for an Android App

The Need For UI Testing Suites

UI Testing can be boring but it’s still essential for making sure that the app has the proper responses under certain scenarios. That idea inspired me to make an Issue on the TravelMate repository about testing suites and I decided to jump at it by doing a UI Testing Pull Request. This UI testing case actually yielded results because later on I found out that the TravelMate app does not have proper error messages when the email field is invalid (i.e. it does not have the format of “johnsmith@example.com”) and thus the user wouldn’t know why they’re failing to sign up if they didn’t enter their email in the correct format.

Setup For UI Testing

To perform UI testing on the Android platform, we need to use Espresso. Espresso is a testing framework designed for writing UI test cases and it also supports recording certain scenarios in a simulated Android device to make writing test cases easier. The setup for my UI testing is as follows:

build.gradle(module:app)

android {
compileSdkVersion 28
defaultConfig {
applicationId "io.github.project_travel_mate"
minSdkVersion 21
targetSdkVersion 28
versionCode 45
versionName "5.5.2"
testInstrumentationRunner = 'androidx.support.test.runner.AndroidJUnitRunner'
}
dependencies {
...
testImplementation 'junit:junit:4.12'
...

//UI Testing Dependencies
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0'
androidTestImplementation 'androidx.test:runner:1.1.0'
androidTestImplementation 'androidx.test:rules:1.1.0'

}

Once that has been added on your build.gradle file for your Android project, you can begin testing by clicking the ‘Run’ button on the top navigation menu and clicking ‘Record Espresso Test’ on the pop-up layout.

RecordEspressoTestOption

The Test Itself

TestingSetupEspresso

The image above demonstrates my testing setup. I basically performed a sequence of actions to create a scenario where a user would submit a Signup form with empty email and password fields. As it turns out, TravelMate app does not have the proper messages to deal with this kind of scenario. The recorded UI testing scenario also generated a code file called EmptyEmailAndPasswordFieldsTest.java. A snippet of code from that file is as follows:

 @Test
    public void emptyEmailAndPasswordFieldsTest() {
...
 ViewInteraction editText = onView(
                allOf(withId(R.id.input_email_signup), withText("Email id"),
                        childAtPosition(
                                childAtPosition(
                                        IsInstanceOf.instanceOf(android.widget.LinearLayout.class),
                                        0),
                                0),
                        isDisplayed()));
        editText.check(doesNotExist());

        ViewInteraction editText2 = onView(
                allOf(withId(R.id.input_pass_signup), withText("Password"),
                        childAtPosition(
                                childAtPosition(
                                        IsInstanceOf.instanceOf(android.widget.LinearLayout.class),
                                        0),
                                0),
                        isDisplayed()));
        editText2.check(doesNotExist());

        ViewInteraction editText3 = onView(
                allOf(withId(R.id.input_confirm_pass_signup), withText("Confirm Password"),
                        childAtPosition(
                                childAtPosition(
                                        IsInstanceOf.instanceOf(android.widget.LinearLayout.class),
                                        0),
                                0),
                        isDisplayed()));
        editText3.check(doesNotExist());

The @Test is an annotation to indicate that the following code block will contain code for testing scenarios. For example, take the following code snippet. This snippet means that we’re testing for a scenario where the “Email id” field is empty:

 ViewInteraction editText = onView(
                allOf(withId(R.id.input_email_signup), withText("Email id"),
                        childAtPosition(
                                childAtPosition(
                                        IsInstanceOf.instanceOf(android.widget.LinearLayout.class),
                                        0),
                                0),
                        isDisplayed()));
        editText.check(doesNotExist());

The testing itself is run successfully by testing benchmarks – it showed us that TravelMate does not have the proper error messages to deal with empty error messages. However, the testing code itself can be further improved by using more assertions in testing where we check if they display a particular error message.

Discovery Of An Issue

As stated earlier, TravelMate does not have the proper error messages to deal with empty email and passwords fields because they assumed that the user would enter them properly. However, beyond that scenario, I also found out that if the user didn’t enter the proper format for their email address, there’s also no error message that is shown as demonstrated in the GIF below:

NoErrorMessages

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s