0% found this document useful (0 votes)
9 views39 pages

06.1 UI Testing

Uploaded by

bmwli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views39 pages

06.1 UI Testing

Uploaded by

bmwli
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39

Android Developer Fundamentals V2

Testing your
UI

Lesson 6

This work is licensed under a


Android Developer UI testing Creative Commons Attribution 4.0 1
Fundamentals V2 International License
6.1 UI testing

This work is licensed under a


Android Developer UI testing Creative Commons Attribution 4.0 2
Fundamentals V2 International License
Contents
● UI testing overview
● Test environment and Espresso setup
● Creating Espresso tests
● Espresso test examples
● Recording tests

This work is licensed under a


Android Developer UI testing Creative Commons Attribution 4.0 3
Fundamentals V2 International License
UI testing
overview

This work is licensed under a


Android Developer UI testing Creative Commons Attribution 4.0 4
Fundamentals V2 International License
UI testing
● Perform all user UI actions with View elements
○ Tap a View, and enter data or make a choice
○ Examine the values of the properties of each View

● Provide input to all View elements


○ Try invalid values

● Check returned output


○ Correct or expected values?
○ Correct presentation?
This work is licensed under a
Android Developer UI testing Creative Commons Attribution 4.0 5
Fundamentals V2 International License
Problems with testing manually

● Time consuming, tedious, error-prone


● UI may change and need frequent retesting
● Some paths fail over time
● As app gets more complex, possible sequences of
actions may grow non-linearly

This work is licensed under a


Android Developer UI testing Creative Commons Attribution 4.0 6
Fundamentals V2 International License
Benefits of testing automatically

● Free your time and resources for other work


● Faster than manual testing
● Repeatable
● Run tests for different device states and
configurations

This work is licensed under a


Android Developer UI testing Creative Commons Attribution 4.0 7
Fundamentals V2 International License
Espresso for single app testing
● Verify that the UI behaves as expected
● Check that the app returns the correct UI output in
response to user interactions
● Navigation and controls behave correctly
● App responds correctly to mocked-out
dependencies

This work is licensed under a


Android Developer UI testing Creative Commons Attribution 4.0 8
Fundamentals V2 International License
UI Automator for multiple apps
● Verify that interactions between different user
apps and system apps behave as expected
● Interact with visible elements on a device
● Monitor interactions between app and system
● Simulate user interactions
● Requires instrumentation

This work is licensed under a


Android Developer UI testing Creative Commons Attribution 4.0 9
Fundamentals V2 International License
What is instrumentation?
● A set of hooks in the Android system
● Loads test package and app into same process,
allowing tests to call methods and examine fields
● Control components independently of app’s
lifecycle
● Control how Android loads apps

This work is licensed under a


Android Developer UI testing Creative Commons Attribution 4.0 10
Fundamentals V2 International License
Benefits of instrumentation

● Tests can monitor all interaction with Android


system
● Tests can invoke methods in the app
● Tests can modify and examine fields in the app
independent of the app’s lifecycle

This work is licensed under a


Android Developer UI testing Creative Commons Attribution 4.0 11
Fundamentals V2 International License
Test
environmen
t
And
Espresso
setup

This work is licensed under a


Android Developer UI testing Creative Commons Attribution 4.0 12
Fundamentals V2 International License
Install Android Support Library

1. In Android Studio choose Tools > Android > SDK


Manager
2. Click SDK Tools and look for Android Support
Repository
3. If necessary, update or install the library

This work is licensed under a


Android Developer UI testing Creative Commons Attribution 4.0 13
Fundamentals V2 International License
Add dependencies to build.gradle

● Android Studio templates include dependencies


● If needed, add the following dependencies:

testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation
'com.android.support.test.espresso:espresso-
core:3.0.1'

This work is licensed under a


Android Developer UI testing Creative Commons Attribution 4.0 14
Fundamentals V2 International License
Add defaultConfig to build.gradle
● Android Studio templates include defaultConfig
setting
● If needed, add the following to defaultConfig
section:

testInstrumentationRunner
"android.support.test.runner.AndroidJUnitRunner"

This work is licensed under a


Android Developer UI testing Creative Commons Attribution 4.0 15
Fundamentals V2 International License
Prepare your device
1. Turn on USB Debugging
2. Turn off all animations in Developer Options >
Drawing
○ Window animation scale
○ Transition animation scale
○ Animator duration scale

This work is licensed under a


Android Developer UI testing Creative Commons Attribution 4.0 16
Fundamentals V2 International License
Create tests

● Store in module-name/src/androidTests/java/
○ In Android Studio: app > java > module-name
(androidTest)

● Create tests as JUnit classes

This work is licensed under a


Android Developer UI testing Creative Commons Attribution 4.0 17
Fundamentals V2 International License
Creating
Espresso
tests

This work is licensed under a


Android Developer UI testing Creative Commons Attribution 4.0 18
Fundamentals V2 International License
Test class definition

@RunWith(AndroidJUnit4.class) — Required annotation for tests


@LargeTest — Based on resources the test uses and time to run
public class ChangeTextBehaviorTest {}

@SmallTest — Runs in < 60s and uses no external resources


@MediumTest — Runs in < 300s, only local network
@LargeTest — Runs for a long time and uses many resources

This work is licensed under a


Android Developer UI testing Creative Commons Attribution 4.0 19
Fundamentals V2 International License
@Rule specifies the context of
testing
@Rule
public ActivityTestRule<MainActivity> mActivityRule =
new ActivityTestRule<>(MainActivity.class);

@ActivityTestRule — Testing support for a single specified activity


@ServiceTestRule — Testing support for starting, binding, shutting
down a service

This work is licensed under a


Android Developer UI testing Creative Commons Attribution 4.0 20
Fundamentals V2 International License
@Before and @After set up and
tear down
@Before
public void initValidString() {
mStringToBetyped = "Espresso";
}

@Before — Setup, initializations


@After — Teardown, freeing resources

This work is licensed under a


Android Developer UI testing Creative Commons Attribution 4.0 21
Fundamentals V2 International License
@Test method structure

@Test
public void changeText_sameActivity() {
// 1. Find a View
// 2. Perform an action
// 3. Verify action was taken, assert
result
}

This work is licensed under a


Android Developer UI testing Creative Commons Attribution 4.0 22
Fundamentals V2 International License
"Hamcrest" simplifies tests
● “Hamcrest” an anagram of “Matchers”
● Framework for creating custom matchers and
assertions
● Match rules defined declaratively
● Enables precise testing
● The Hamcrest Tutorial

This work is licensed under a


Android Developer UI testing Creative Commons Attribution 4.0 23
Fundamentals V2 International License
Hamcrest Matchers

● ViewMatcher — find Views by id, content, focus,


hierarchy
● ViewAction — perform an action on a view
● ViewAssertion — assert state and verify the result

This work is licensed under a


Android Developer UI testing Creative Commons Attribution 4.0 24
Fundamentals V2 International License
Basic example test
@Test
public void changeText_sameActivity() {
// 1. Find view by Id
onView(withId(R.id.editTextUserInput))
// 2. Perform action—type string and click button
.perform(typeText(mStringToBetyped),
closeSoftKeyboard());
onView(withId(R.id.changeTextBt)).perform(click());
// 3. Check that the text was changed
onView(withId(R.id.textToBeChanged))
.check(matches(withText(mStringToBetyped)));
This work is licensed under a
} Android Developer UI testing Creative Commons Attribution 4.0
International License
25
Fundamentals V2
Finding views with onView
● withId() — find a view with the specified Android id
○ onView(withId(R.id.editTextUserInput))

● withText() — find a view with specific text


● allOf() — find a view to that matches multiple conditions
● Example: Find a visible list item with the given text:
onView(allOf(withId(R.id.word),
withText("Clicked! Word 15"),
isDisplayed()))
This work is licensed under a
Android Developer UI testing Creative Commons Attribution 4.0 26
Fundamentals V2 International License
onView returns ViewInteraction object
● If you need to reuse the View returned by onView
● Make code more readable or explicit
● check() and perform() methods

ViewInteraction textView = onView(


allOf(withId(R.id.word), withText("Clicked! Word 15"),
isDisplayed()));
textView.check(matches(withText("Clicked! Word 15")));

This work is licensed under a


Android Developer UI testing Creative Commons Attribution 4.0 27
Fundamentals V2 International License
Perform actions
● Perform an action on the View found by a
ViewMatcher
● Can be any action you can perform on the View
// 1. Find view by Id
onView(withId(R.id.editTextUserInput))
// 2. Perform action—type string and click button
.perform(typeText(mStringToBetyped),
closeSoftKeyboard());
onView(withId(R.id.changeTextBt)).perform(click());
This work is licensed under a
Android Developer UI testing Creative Commons Attribution 4.0 28
Fundamentals V2 International License
Check result

● Asserts or checks the state of the View

// 3. Check that the text was changed


onView(withId(R.id.textToBeChanged))
.check(matches(withText(mStringToBetyped)));

This work is licensed under a


Android Developer UI testing Creative Commons Attribution 4.0 29
Fundamentals V2 International License
When a test fails
Test
onView(withId(R.id.text_message))
.check(matches(withText("This is a failing test.")));

Result snippet
android.support.test.espresso.base.DefaultFailureHandler$Ass
ertionFailedWithCauseError: 'with text: is "This is a
failing test."' doesn't match the selected view.
Expected: with text: is "This is a failing test."
Got: "AppCompatTextView{id=2131427417, res-name=text_message
...
This work is licensed under a
Android Developer UI testing Creative Commons Attribution 4.0 30
Fundamentals V2 International License
Recording
tests

This work is licensed under a


Android Developer UI testing Creative Commons Attribution 4.0 31
Fundamentals V2 International License
Recording an Espresso test
● Use app normally, clicking through the UI
● Editable test code generated automatically
● Add assertions to check if a view holds a certain
value
● Record multiple interactions in one session, or
record multiple sessions

This work is licensed under a


Android Developer UI testing Creative Commons Attribution 4.0 32
Fundamentals V2 International License
Start recording an Espresso test
1. Run > Record Espresso
Test
2. Click Restart app, select
target, and click OK
3. Interact with the app to do
what you want to test

This work is licensed under a


Android Developer UI testing Creative Commons Attribution 4.0 33
Fundamentals V2 International License
Add assertion to Espresso test recording

4. Click Add Assertion and


select a UI element
5. Choose text is and enter
the text you expect to see
6. Click Save Assertion and
click Complete Recording

This work is licensed under a


Android Developer UI testing Creative Commons Attribution 4.0 34
Fundamentals V2 International License
Learn more from developer
docs
Android Studio Documentation
Android Developer Documentation
● Test Your App ● Best Practices for Testing
● Getting Started with Testing
● Espresso basics
● Testing UI for a Single App
● Espresso cheat sheet ● Building Instrumented Unit Te
sts
● Espresso Advanced Samples
● The Hamcrest Tutorial
● Hamcrest API and Utility Class
es
● Test Support APIs
This work is licensed under a
Android Developer UI testing Creative Commons Attribution 4.0 35
Fundamentals V2 International License
Learn even more

Android Testing Support Library


● Espresso documentation
● Espresso Samples

Videos
● Android Testing Support - Android Testing Patterns #1 (introduction)
● Android Testing Support - Android Testing Patterns #2 (onView view
matching)
● Android Testing Support - Android Testing Patterns #3 (onData &
adapter views)
This work is licensed under a
Android Developer UI testing Creative Commons Attribution 4.0 36
Fundamentals V2 International License
Learn even more

● Google Testing Blog: Android UI Automated Testing


● Atomic Object: “Espresso – Testing RecyclerViews at Specific Positions

● Stack Overflow: “How to assert inside a RecyclerView in Espresso?”
● GitHub: Android Testing Samples
● Google Codelabs: Android Testing Codelab

This work is licensed under a


Android Developer UI testing Creative Commons Attribution 4.0 37
Fundamentals V2 International License
What's Next?

● Concept Chapter: 6.1 UI testing


● Practical: 6.1 Espresso for UI testing

This work is licensed under a


Android Developer UI testing Creative Commons Attribution 4.0 38
Fundamentals V2 International License
END

Android Developer Fundamentals V2 39

You might also like