Share
Embed
Android Espresso UI Testing & UI/Application Exerciser Monkey
An overview on how Espresso works for Android instrumentation testing, also there is an overview of Application Exerciser Monkey, a utility tool from the Android SDK.
Jorge E. Hernández
July 16, 2020
Transcript
81 slides
-
Android Espresso UI Testing & UI/Application Exerciser Monkey Jorge E.
Hernández @lalongooo -
Espresso - Testing framework for Android UI testing - Used
by devs familiar with the Android app codebase - Lets you interact with your Android app just like any user would do - Runs on emulators or physical devices -
// Find the EditText onView(withId(R.id.editTextMessage)) // ... then type some
text .perform(typeText(TEXT_TO_BE_TYPED)) -
// Find the EditText onView(withId(R.id.editTextMessage)) // ... then type some
text & close the soft keyboard .perform(typeText(TEXT_TO_BE_TYPED)) -
// Find the EditText onView(withId(R.id.editTextMessage)) // ... then type some
text & close the soft keyboard .perform(typeText(TEXT_TO_BE_TYPED), closeSoftKeyboard()) -
// Find the EditText onView(withId(R.id.editTextMessage)) // ... then type some
text & close the soft keyboard .perform(typeText(TEXT_TO_BE_TYPED), closeSoftKeyboard()) // Find the button -
// Find the EditText onView(withId(R.id.editTextMessage)) // ... then type some
text & close the soft keyboard .perform(typeText(TEXT_TO_BE_TYPED), closeSoftKeyboard()) // Find the button onView(withId(R.id.buttonChangeText)) -
// Find the EditText onView(withId(R.id.editTextMessage)) // ... then type some
text & close the soft keyboard .perform(typeText(TEXT_TO_BE_TYPED), closeSoftKeyboard()) // Find the button, then click on it onView(withId(R.id.buttonChangeText)) -
// Find the EditText onView(withId(R.id.editTextMessage)) // ... then type some
text & close the soft keyboard .perform(typeText(TEXT_TO_BE_TYPED), closeSoftKeyboard()) // Find the button, then click on it onView(withId(R.id.buttonChangeText)) .perform(click()) -
// Find the EditText onView(withId(R.id.editTextMessage)) // ... then type some
text & close the soft keyboard .perform(typeText(TEXT_TO_BE_TYPED), closeSoftKeyboard()) // Find the button, then click on it onView(withId(R.id.buttonChangeText)) .perform(click()) // Find the TextView with the message -
// Find the EditText onView(withId(R.id.editTextMessage)) // ... then type some
text & close the soft keyboard .perform(typeText(TEXT_TO_BE_TYPED), closeSoftKeyboard()) // Find the button, then click on it onView(withId(R.id.buttonChangeText)) .perform(click()) // Find the TextView with the message onView(withId(R.id.textViewMessage)) -
// Find the EditText onView(withId(R.id.editTextMessage)) // ... then type some
text & close the soft keyboard .perform(typeText(TEXT_TO_BE_TYPED), closeSoftKeyboard()) // Find the button, then click on it onView(withId(R.id.buttonChangeText)) .perform(click()) // Find the TextView with the message, then check text was changed onView(withId(R.id.textViewMessage)) -
// Find the EditText onView(withId(R.id.editTextMessage)) // ... then type some
text & close the soft keyboard .perform(typeText(TEXT_TO_BE_TYPED), closeSoftKeyboard()) // Find the button, then click on it onView(withId(R.id.buttonChangeText)) .perform(click()) // Find the TextView with the message, then check text was changed onView(withId(R.id.textViewMessage)) .check(matches(withText(TEXT_TO_BE_TYPED))) -
@RunWith(AndroidJUnit4::class) @LargeTest class ChangeTextBehaviorTest { @get:Rule var activityScenarioRule = activityScenarioRule<>().apply
{ } Where should we place this code? -
@RunWith(AndroidJUnit4::class) @LargeTest class ChangeTextBehaviorTest { @get:Rule var activityScenarioRule = activityScenarioRule<MainActivity>()
@Test fun testTextIsChangedWhenButtonIsClicked() { } } Where should we place this code? -
@RunWith(AndroidJUnit4::class) @LargeTest class ChangeTextBehaviorTest { @get:Rule var activityScenarioRule = activityScenarioRule<MainActivity>()
@Test fun testTextIsChangedWhenButtonIsClicked() { // ... your test code here } } Where should we place this code? -
How to test a RecyclerView? // Find the RecyclerView onView(withId(R.id.myRecyclerView))
// ... perform an action indicating the item position -
How to test a RecyclerView? // Find the RecyclerView onView(withId(R.id.myRecyclerView))
// ... perform an action indicating the item position .perform( ) -
How to test a RecyclerView? // Find the RecyclerView onView(withId(R.id.myRecyclerView))
// ... perform an action indicating the item position .perform( RecyclerViewActions.actionOnItemAtPosition<>() ) -
How to test a RecyclerView? // Find the RecyclerView onView(withId(R.id.myRecyclerView))
// ... perform an action indicating the item position .perform( RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>() ) -
How to test a RecyclerView? // Find the RecyclerView onView(withId(R.id.myRecyclerView))
// ... perform an action indicating the item position .perform( RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>( 30, click() ) ) -
How to test a RecyclerView? // Find the RecyclerView onView(withId(R.id.myRecyclerView))
// ... perform an action indicating the item position .perform( RecyclerViewActions.actionOnItemAtPosition<RecyclerView.ViewHolder>( 30, // the position of the item in the RecyclerView click() ) ) -
What if we don’t know the position? // Find the
RecyclerView onView(withId(R.id.myRecyclerView)) // ... perform an action indicating the item position .perform( RecyclerViewActions.actionOnItem<RecyclerView.ViewHolder>( hasDescendant(withText(containsString("Your order total is"))), click() ) ) -
What if we don’t know the position? // Find the
RecyclerView onView(withId(R.id.myRecyclerView)) // ... perform an action indicating the item position .perform( RecyclerViewActions.actionOnItem<RecyclerView.ViewHolder>( hasDescendant(withText(containsString("Your order total is"))), click() ) ) -
scrollTo() Scrolls to the matched View. scrollToHolder() Scrolls to the
matched View Holder. scrollToPosition() Scrolls to a specific position. actionOnHolderItem() Performs a View Action on a matched View Holder. actionOnItem() Performs a View Action on a matched View. actionOnItemAtPosition() Performs a ViewAction on a view at a specific position. More RecyclerViewActions -
scrollTo() Scrolls to the matched View. scrollToHolder() Scrolls to the
matched View Holder. scrollToPosition() Scrolls to a specific position. actionOnHolderItem() Performs a View Action on a matched View Holder. actionOnItem() Performs a View Action on a matched View. actionOnItemAtPosition() Performs a ViewAction on a view at a specific position. That’s it for Espresso… 🤓 More RecyclerViewActions -
UI/Application Exerciser Monkey • Program that runs on your emulator
or device • Generates pseudo-random streams of events such as clicks, touches, or gestures, as well as a number of system-level events -
UI/Application Exerciser Monkey • Program that runs on your emulator
or device • Generates pseudo-random streams of events such as clicks, touches, or gestures, as well as a number of system-level events • Use it to stress-test applications that you are developing, in a random yet repeatable manner -
UI/Application Exerciser Monkey Basic usage Example # Will send 500
random events to the entire system adb shell monkey 500 adb shell monkey [options] <event-count> -
Stress testing your application # Will launch your application adb
shell monkey -p com.example.app -
Stress testing your application # Will launch your application #
Then, will send 500 random events adb shell monkey -p com.example.app -
Stress testing your application # Will launch your application #
Then, will send 500 random events adb shell monkey -p com.example.app 500 -
# Level 0 (the default) provides little information beyond startup
notification, test completion, and final results. Verbosity levels -
# Level 0 (the default) provides little information beyond startup
notification, test completion, and final results. # Level 1 provides more details about the test as it runs, such as individual events being sent to your activities. Verbosity levels -
# Level 0 (the default) provides little information beyond startup
notification, test completion, and final results. # Level 1 provides more details about the test as it runs, such as individual events being sent to your activities. # Level 2 provides more detailed setup information such as activities selected or not selected for testing. Verbosity levels -
Verbosity levels # Level 0 (the default) provides little information
beyond startup notification, test completion, and final results. # Level 1 provides more details about the test as it runs, such as individual events being sent to your activities. # Level 2 provides more detailed setup information such as activities selected or not selected for testing. adb shell monkey -p com.example.app -v -v -v 500 -
Verbosity levels # Level 0 (the default) provides little information
beyond startup notification, test completion, and final results. # Level 1 provides more details about the test as it runs, such as individual events being sent to your activities. # Level 2 provides more detailed setup information such as activities selected or not selected for testing. adb shell monkey -p com.example.app -v -v -v 500 -
# Pass a seed value, then use it again #
it will generate the same sequence of events. adb shell monkey -p com.example.app -s 7 500 How to reproduce? -
# Pass a seed value, then use it again #
it will generate the same sequence of events. adb shell monkey -p com.example.app -s 7 500 How to reproduce? -
Throttling # Will send an event each second # Events
include launching activities from ignored packages $ The --throttle argument is expressed in milliseconds adb shell monkey --throttle 1000 20 -
Throttling # Will send an event each second # Events
include launching activities from ignored packages $ The --throttle argument is expressed in milliseconds adb shell monkey --throttle 1000 20 -
Touch events # Will send 20 events # All of
them will be down-up events in a single place on the screen # Pretty much like a single tap on the screen adb shell monkey --pct-touch 100 20 -
# Will send 20 events # All of them will
be like swipes in a random manner adb shell monkey --pct-motion 100 20 Motion events -
# Will send 20 events, 10 touch events & 10
motion events adb shell monkey --pct-touch 50 --pct-motion 50 20 Touch & Motion events combined -
# Trackball events adb shell monkey --pct-trackball 100 500 #
Basic navigation events adb shell monkey --pct-nav 100 500 Additional monkey options -
# Trackball events adb shell monkey --pct-trackball 100 500 #
Basic navigation events adb shell monkey --pct-nav 100 500 # "Major" navigation events like # center button in a 5-way pad, the back key, or the menu key adb shell monkey --pct-majornav 100 500 Additional monkey options -
# Trackball events adb shell monkey --pct-trackball 100 500 #
Basic navigation events adb shell monkey --pct-nav 100 500 # "Major" navigation events like # center button in a 5-way pad, the back key, or the menu key adb shell monkey --pct-majornav 100 500 # System events # Home, Back, Start Call, End Call, Volume controls adb shell monkey --pct-syskeys 100 500 Additional monkey options -
# Trackball events adb shell monkey --pct-trackball 100 500 #
Basic navigation events adb shell monkey --pct-nav 100 500 # "Major" navigation events like # center button in a 5-way pad, the back key, or the menu key adb shell monkey --pct-majornav 100 500 # System events # Home, Back, Start Call, End Call, Volume controls adb shell monkey --pct-syskeys 100 500 # Will launch your app via startActivity() adb shell monkey --pct-appswitch 100 500 Additional monkey options