Slide 1

Slide 1 text

Efficient UI testing Android apps by example @Alex_Zhukovich

Slide 2

Slide 2 text

Android tests Local Instrumentation UI Non-UI Robolectric Espresso UiAutomator Appium Instrumentation API

Slide 3

Slide 3 text

Layout Inspector UiAutomator Viewer

Slide 4

Slide 4 text

Installing and running test cases on device adb shell Instrumentation am instrument Application Test Application

Slide 5

Slide 5 text

Application overview

Slide 6

Slide 6 text

@RunWith(AndroidJUnit4::class) class SignInActivityTest { private val correctEmail = "[email protected]" @Rule @JvmField val activityRule = ActivityTestRule(SignInActivity::class.java) @Test fun shouldDisplayPasswordErrorWhenPasswordIsEmpty() { onView(withId(R.id.email)) .perform(replaceText(correctEmail)) onView(withId(R.id.signIn)) .perform(click()) onView(withText(R.string.error_password_should_not_be_empty)) .check(matches(isDisplayed())) } }

Slide 7

Slide 7 text

Espresso Test Recorder

Slide 8

Slide 8 text

Espresso Test Recorder

Slide 9

Slide 9 text

Espresso Test Recorder @LargeTest @RunWith(AndroidJUnit4::class) class SignInActivityTest { @Rule @JvmField var mActivityTestRule = ActivityTestRule(SignInActivity::class.java) @Test fun signInActivityTest() { Thread.sleep(7000) val appCompatEditText = onView( allOf(withId(R.id.email), childAtPosition( allOf(withId(R.id.signInRoot), childAtPosition( withId(android.R.id.content), 0)), 4), isDisplayed())) appCompatEditText.perform(click()) val appCompatEditText2 = onView( allOf(withId(R.id.email), childAtPosition( allOf(withId(R.id.signInRoot), childAtPosition( withId(android.R.id.content), 0)), 4), isDisplayed())) appCompatEditText2.perform(replaceText("[email protected]"), closeSoftKeyboard()) Thread.sleep(7000) pressBack() val appCompatButton = onView( allOf(withId(R.id.signIn), withText("Sing In"), childAtPosition( allOf(withId(R.id.signInRoot), childAtPosition( withId(android.R.id.content), 0)), 6), isDisplayed())) appCompatButton.perform(click()) } private fun childAtPosition( parentMatcher: Matcher, position: Int): Matcher { return object : TypeSafeMatcher() { override fun describeTo(description: Description) { description.appendText("Child at position $position in parent ") parentMatcher.describeTo(description) } public override fun matchesSafely(view: View): Boolean { val parent = view.parent return parent is ViewGroup && parentMatcher.matches(parent) && view == parent.getChildAt(position) } } } }

Slide 10

Slide 10 text

@RunWith(AndroidJUnit4::class) class SmokeTests { private val correctEmail = "[email protected]" private val correctPassword = "test123" @Rule @JvmField val chain: RuleChain = RuleChain .outerRule(GrantPermissionRule.grant(android.Manifest.permission.ACCESS_FINE_LOCATION)) .around(ActivityTestRule(SplashActivity::class.java, true, false)) @Test fun shouldVerifySuccessfulLogin() { val mapVisibilityIdlingResource = ViewVisibilityIdlingResource(R.id.mapContainer, View.VISIBLE) splashActivityE2ETestRule.launchActivity(null) onView(withId(R.id.signIn)) .perform(click()) onView(withId(R.id.email)) .perform(replaceText(correctEmail), closeSoftKeyboard()) onView(withId(R.id.password)) .perform(replaceText(correctPassword), closeSoftKeyboard()) onView(withId(R.id.signIn)) .perform(click()) IdlingRegistry.getInstance().register(mapVisibilityIdlingResource) onView(withId(R.id.mapContainer)) .check(ViewAssertions.matches(isDisplayed())) IdlingRegistry.getInstance().unregister(mapVisibilityIdlingResource) openActionBarOverflowOrOptionsMenu(getActivityInstance()) onView(withText(R.string.nav_sign_out_title)) .check(ViewAssertions.matches(isDisplayed())) .perform(click()) } }

Slide 11

Slide 11 text

Domain-specific language

Slide 12

Slide 12 text

onView(withId(R.id.email)) .perform(replaceText(email), closeSoftKeyboard()) onView(withId(R.id.password)) .perform(replaceText(password), closeSoftKeyboard()) onView(withId(R.id.signIn)) .perform(click())

Slide 13

Slide 13 text

open class BaseTestRobot { fun enterText(viewId: Int, text: String): ViewInteraction = onView(withId(viewId)) .perform(replaceText(text), closeSoftKeyboard()) fun clickView(viewId: Int): ViewInteraction = onView(withId(viewId)) .perform(click()) } onView(withId(R.id.email)) .perform(replaceText(email), closeSoftKeyboard()) onView(withId(R.id.password)) .perform(replaceText(password), closeSoftKeyboard()) onView(withId(R.id.signIn)) .perform(click())

Slide 14

Slide 14 text

class SignInScreenRobot : BaseTestRobot() { fun signIn(email: String, password: String) { enterText(R.id.email, email) enterText(R.id.password, password) clickView(R.id.signIn) } } open class BaseTestRobot { fun enterText(viewId: Int, text: String): ViewInteraction = onView(withId(viewId)) .perform(replaceText(text), closeSoftKeyboard()) fun clickView(viewId: Int): ViewInteraction = onView(withId(viewId)) .perform(click()) } onView(withId(R.id.email)) .perform(replaceText(email), closeSoftKeyboard()) onView(withId(R.id.password)) .perform(replaceText(password), closeSoftKeyboard()) onView(withId(R.id.signIn)) .perform(click())

Slide 15

Slide 15 text

class SignInScreenRobot : BaseTestRobot() { fun signIn(email: String, password: String) { enterText(R.id.email, email) enterText(R.id.password, password) clickView(R.id.signIn) } } open class BaseTestRobot { fun enterText(viewId: Int, text: String): ViewInteraction = onView(withId(viewId)) .perform(replaceText(text), closeSoftKeyboard()) fun clickView(viewId: Int): ViewInteraction = onView(withId(viewId)) .perform(click()) } fun signInScreen(func: SignInScreenRobot.() -> Unit) = SignInScreenRobot().apply { func() } onView(withId(R.id.email)) .perform(replaceText(email), closeSoftKeyboard()) onView(withId(R.id.password)) .perform(replaceText(password), closeSoftKeyboard()) onView(withId(R.id.signIn)) .perform(click())

Slide 16

Slide 16 text

class SignInScreenRobot : BaseTestRobot() { fun signIn(email: String, password: String) { enterText(R.id.email, email) enterText(R.id.password, password) clickView(R.id.signIn) } } open class BaseTestRobot { fun enterText(viewId: Int, text: String): ViewInteraction = onView(withId(viewId)) .perform(replaceText(text), closeSoftKeyboard()) fun clickView(viewId: Int): ViewInteraction = onView(withId(viewId)) .perform(click()) } fun signInScreen(func: SignInScreenRobot.() -> Unit) = SignInScreenRobot().apply { func() } signInScreen { signIn(email, password) } onView(withId(R.id.email)) .perform(replaceText(email), closeSoftKeyboard()) onView(withId(R.id.password)) .perform(replaceText(password), closeSoftKeyboard()) onView(withId(R.id.signIn)) .perform(click())

Slide 17

Slide 17 text

@RunWith(AndroidJUnit4::class) class SmokeTests { private val correctEmail = "[email protected]" private val correctPassword = "test123" @Rule @JvmField val chain: RuleChain = RuleChain .outerRule(GrantPermissionRule.grant(android.Manifest.permission.ACCESS_FINE_LOCATION)) .around(ActivityTestRule(SplashActivity::class.java, true, false)) @Test fun shouldVerifySuccessfulLogin() { val mapVisibilityIdlingResource = ViewVisibilityIdlingResource(R.id.mapContainer, View.VISIBLE) splashActivityE2ETestRule.launchActivity(null) onView(withId(R.id.signIn)) .perform(click()) onView(withId(R.id.email)) .perform(replaceText(correctEmail), closeSoftKeyboard()) onView(withId(R.id.password)) .perform(replaceText(correctPassword), closeSoftKeyboard()) onView(withId(R.id.signIn)) .perform(click()) IdlingRegistry.getInstance().register(mapVisibilityIdlingResource) onView(withId(R.id.mapContainer)) .check(ViewAssertions.matches(isDisplayed())) IdlingRegistry.getInstance().unregister(mapVisibilityIdlingResource) openActionBarOverflowOrOptionsMenu(getActivityInstance()) onView(withText(R.string.nav_sign_out_title)) .check(ViewAssertions.matches(isDisplayed())) .perform(click()) } } @Test fun shouldVerifySuccessfulLogin() { splashScreen { display() } loginScreen { openSignIn() } signInScreen { signIn(email, password) } homeScreen { isMapDisplayed() signOut() } }

Slide 18

Slide 18 text

Internal DSL(Domain-specific language) Readability Reuse Error recovery Building complexity Flexibility

Slide 19

Slide 19 text

Testing interaction with a server

Slide 20

Slide 20 text

Testing interaction with a server Mock layer Interaction with server(s) Mocking interaction with server WireMock RestMock MockWebServer

Slide 21

Slide 21 text

Authorization of the user - scenarios Test scenario #1: Enter correct auth data Test scenario #2: Enter incorrect auth data Test scenario #3: Enter incorrect auth data and handle them on client side

Slide 22

Slide 22 text

User Authorization End To End @Test fun shouldVerifyUserAuthorization() { loginScreen { display() } loginScreen { openSignIn() } signInScreen { signIn(email, password) } homeScreen { isMapDisplayed() signOut() } }

Slide 23

Slide 23 text

Authorization of the users DATA UI with mocking @Test fun shouldOpenMapScreenAfterSuccessfulSignIn() { prepare(testScope) { mockLocationProvider() mockSuccessfulSignIn(email, password) } signInScreen { signIn(email, password) } homeScreen { isSuccessfullyLoaded() } }

Slide 24

Slide 24 text

Authorization of the user – differences End-To-End test cases UI tests with mocking B Interaction with server Verification interaction with a server C Fast UI verification Fast and independent on resources tests A Entry point Start tests from the main screen B No interaction with server Verification UI and interaction with mock object A Entry point Start test from any screen of the app

Slide 25

Slide 25 text

Search notes - scenarios Test scenario #1: Display all notes Test scenario #2: Handle error during loading notes Test scenario #3: Display search results

Slide 26

Slide 26 text

Display search results End To End @Test fun shouldVerifyAddingAndSearchNote() { val noteText = "test note ${Date().time}" splashScreen { display() } loginScreen { openSignIn() } signInScreen { signIn(email, password) } homeScreen { isMapDisplayed() openAddNoteFragment() addNote(noteText) openSearchFragment() searchNoteByText(noteText) isNoteInSearchResult(noteText) signOut() } }

Slide 27

Slide 27 text

Display search results DATA UI with mocking @Test fun shouldSearchByNotesAndDisplayResult() { val expectedItemCount = testNotes.size prepare(testScope) { mockLoadingEmptyListOfNotes() mockSearchNoteByAnyText(testNotes) } homeScreen { searchNoteByText(searchInput) verifySearchResultsByItemCount(expectedItemCount) verifySearchResults(testNotes) } }

Slide 28

Slide 28 text

Handle error during loading notes !

Slide 29

Slide 29 text

Search notes – differences End-To-End test cases UI tests cases with mocking B Interaction with server Verification interaction with a server A Entry point Start tests from the main screen C Data from the server Depend on data from the server E Fast UI verification Fast and independent on resources tests B No interaction with server Verification UI and interaction with mock object A Entry point Start test from any screen of the app D App architecture Architecture should support mocking C UI component verification Testing only fragments, view without main Activity

Slide 30

Slide 30 text

Should we use UI test with mocking everywhere?

Slide 31

Slide 31 text

Should we use UI tests with mocking everywhere? https://gph.is/1bkaInz

Slide 32

Slide 32 text

Scope of Regression testing

Slide 33

Slide 33 text

E2E test cases in scope of regression tests Specifications Analytical data

Slide 34

Slide 34 text

Efficient UI testing End To End tests UI tests +

Slide 35

Slide 35 text

Flaky tests

Slide 36

Slide 36 text

Production and testing tools Tools Emulator

Slide 37

Slide 37 text

Test cases and test execution process Dependencies Test data Long running test Parallel execution

Slide 38

Slide 38 text

Handling Flaky tests on CI

Slide 39

Slide 39 text

Handling Flaky tests on CI

Slide 40

Slide 40 text

Handling Flaky tests on CI FLAKY TESTS

Slide 41

Slide 41 text

Handling Flaky tests on CI FLAKY TESTS FAILED TESTS

Slide 42

Slide 42 text

Testing tips

Slide 43

Slide 43 text

The name matters

Slide 44

Slide 44 text

Learn existing test cases and maintain them

Slide 45

Slide 45 text

Avoid redundant test data

Slide 46

Slide 46 text

Verify positive and negative test cases

Slide 47

Slide 47 text

Verify business and navigation flows

Slide 48

Slide 48 text

Test only your code

Slide 49

Slide 49 text

Stop testing everything with one type of tests

Slide 50

Slide 50 text

Stop collecting flaky tests

Slide 51

Slide 51 text

Write test case base on specification, not on implementation

Slide 52

Slide 52 text

Covering production bugs in test cases

Slide 53

Slide 53 text

Stop testing manually, just automate it

Slide 54

Slide 54 text

Care about testability in the code

Slide 55

Slide 55 text

Q&A Espresso: https://developer.android.com/training/testing/espresso/ UiAutomator: https://developer.android.com/training/testing/ui-automator/ Appium: http://appium.io/ Android Testing codelab: https://codelabs.developers.google.com/codelabs/android-testing/ Instrumentation Testing Robots https://academy.realm.io/posts/kau-jake-wharton-testing-robots/ MapNotes: https://github.com/AlexZhukovich/MapNotes Blog: http://alexzh.com/ @Alex_Zhukovich