Elif Sezer
inventiv
Published in
3 min readOct 2, 2019

--

User interface (UI) testing is a process used for testing if the application is functioning correctly. UI testing can be performed manually by a human tester, or it can be performed automatically with the use of a software program. Android UI example which have used to test Espresso framework can be found below.

Getting Started

I will write test for a Login app. When we click the button in this application it shows information of the whether toast message successful or not.

Let we create test case for login screen. The login scenario, in my case,

  • When the user opens the app sees splash screen. Then login activity is loaded.

• The user enters email address, password and then clicks the button

  • The login screen shows correct, wrong or empty toast message.
androidTestImplementation ‘androidx.test:runner:1.1.1’
androidTestImplementation ‘androidx.test:rules:1.1.0’
androidTestImplementation ‘androidx.test.espresso:espresso-core:3.1.1’

By default, a created Android project includes some Espresso dependencies and test runner. (build.gradle module app)

Writing Espresso Tests

You might see the com.example.login espresso (android Test) folder when you switch to project view, something like below. You can create a test file named as MainActivityTest.

Add an empty class into your file.

class MainActivityTest {}

Above the class name, add the @RunWith(AndroidJUnit4::class). In order to run the suite test, you need to annotate a test class using below-mentioned annotations:

@RunWith(AndroidJUnit4::class)
class MainActivityTest {
@Rule
@JvmFieldvar activityRule = ActivityTestRule<MainActivity>(MainActivity::class.java)
}

The main three components of Espresso include the following:

1-ViewMatchers : A collection of objects that implement the interface. You can pass one or more of these to the onView() method to locate a view within the current view hierarchy.

2-ViewActions : A collection of ViewAction objects that can be passed to the ViewInteraction.perform() method, such as click().

3-ViewAssertions : A collection of ViewAssertion objects that can be passed the ViewInteraction.check() method. Most of the time, you will use the matches assertion, which uses a View matcher to assert the state of the currently selected view.

You need to add an annotation ‘@test’ , in order to test the case”@Test” add for the test case . All actions must be followed step by step.

Add the following test to verify that when the screen is opened there is a field for entering the information. The login information Accuracy is tested with below case;

@Test

fun clickLoginButton_Successful() { onView(withId(R.id.edtUserName)).perform(ViewActions.typeText(username)) onView(withId(R.id.edtPassword)).perform(ViewActions.typeText(password))
onView(withId(R.id.btn_submit)).perform(ViewActions.pressBack(), click())

}

Build and run your tests;

After setting steps for a screen, all cases can be created.

@Test

fun clickLoginButton_Unsuccesful() { onView(withId(R.id.edtUserName)).perform(ViewActions.typeText(fakeusername))
onView(withId(R.id.edtPassword)).perform(ViewActions.typeText(fakepassword))onView(withId(R.id.btn_submit)).perform(ViewActions.pressBack(), click())

}
@Test
fun clickLoginButton_Empty() {
onView(withId(R.id.btn_submit)).perform(click())
}

You can see all source code of this sample on Github.

References:

--

--