Espresso — Mobile Automation Framework for Android — Part 3

App Devops
1 min readNov 9, 2022

Basic Test Sample with Espresso

Hi folks, let’s see how to test an activity and perform some actions with that. First lets create a class under “androidTest ”package.

@RunWith(AndroidJUnit4::class
class BasicSampleTest {
// To add test cases here
}

Use [activityScenarioRule] to create and launch the activity under test before each test, * and close it after each test.

@get:Rule
var activityScenarioRule = activityScenarioRule<MainActivity>()

Now let’s create a test method to type the word and perform a button click action.

val STRING_TO_BE_TYPED = "APP DEVOPS"
@Test
fun changeText_sameMainActivity() {

// Type Text
onView(withId(R.id.name_edt))
.perform(typeText(STRING_TO_BE_TYPED), closeSoftKeyboard())
// Perform same-act Click
onView(withId(R.id.same_act)).perform(click())

// Check that the text was changed.
onView(withId(R.id.name_txt)).check(matches(withText(STRING_TO_BE_TYPED)))
}

Then let’s do the same instead showing text in same activity lets move to other activity[ShowActivity] and show the entered text in that activity.

you can get the Raw code in the github. For more updates stay tuned :)

--

--