Slide 1

Slide 1 text

Ef fi cient Android UI Testing @alex_zhukovich https://alexzh.com/

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Multiple languages Light and Dark themes Accessibility options RTL and LTR support

Slide 4

Slide 4 text

Let's Run The Tests

Slide 5

Slide 5 text

ANDROID TESTS LOCAL INSTRUMENTATION

Slide 6

Slide 6 text

ANDROID TESTS LOCAL INSTRUMENTATION UI* Non-UI

Slide 7

Slide 7 text

ANDROID TESTS LOCAL INSTRUMENTATION UI* Non-UI UI Non-UI

Slide 8

Slide 8 text

ANDROID TESTS LOCAL INSTRUMENTATION UI* Non-UI UI Non-UI SHARED CODE

Slide 9

Slide 9 text

androidTest Requires device or emulator test Executes in local JVM

Slide 10

Slide 10 text

androidTest test commonTest android { ... sourceSets { androidTest { java.srcDirs += "src/commonTest/java" } test { java.srcDirs += "src/commonTest/java" } } }

Slide 11

Slide 11 text

User Interface tests Snapshot tests

Slide 12

Slide 12 text

app.apk tests.apk BUILD APKs

Slide 13

Slide 13 text

app.apk tests.apk >_ adb shell am instrument

Slide 14

Slide 14 text

app.apk tests.apk >_ adb shell am instrument

Slide 15

Slide 15 text

ACTIVITY VIEW / COMPOSABLE FRAGMENT

Slide 16

Slide 16 text

launchActivity launchFragmentInContainer ComposeContentTestRule ActivityScenario.launch( MainActivity::class.java )

Slide 17

Slide 17 text

launchActivity launchFragmentInContainer ComposeContentTestRule val activityOptions = bundleOf( Pair("param-1", 1), Pair("param-2", 2), Pair("param-3", 3), ) val scenario = ActivityScenario.launch( MainActivity::class.java, activityOptions ) scenario.moveToState(Lifecycle.State.STARTED) ActivityScenario.launch(

Slide 18

Slide 18 text

launchActivity launchFragmentInContainer ComposeContentTestRule val scenario = launchFragmentInContainer( fragmentArgs: Bundle? = null, @StyleRes themeResId: Int = 
 R.style.FragmentScenarioEmptyFragmentActivityTheme, initialState: Lifecycle.State = 
 Lifecycle.State.RESUMED, factory: FragmentFactory? = null ) scenario.moveToState(Lifecycle.State.STARTED) debugImplementation "androidx.fragment:fragment-testing:1.4.0-rc01"

Slide 19

Slide 19 text

launchActivity launchFragmentInContainer ComposeContentTestRule Activity Fragment

Slide 20

Slide 20 text

launchActivity launchFragmentInContainer ComposeContentTestRule androidx.fragment.app.testing.FragmentScenario$EmptyFragmentActivity Activity Fragment Compilation Added to AndroidManifest

Slide 21

Slide 21 text

launchActivity launchFragmentInContainer ComposeContentTestRule @get:Rule val composeTestRule = createComposeRule() composeTestRule.apply { setContent { LoginScreen() } onNodeWithTag("email") .performTextInput("[email protected]") onNodeWithTag("password") .performTextInput("password") onNodeWithTag("login") .performClick() ... } debugImplementation "androidx.compose.ui:ui-test-manifest:1:0:5"

Slide 22

Slide 22 text

launchActivity launchFragmentInContainer ComposeContentTestRule Component Activity @Composable

Slide 23

Slide 23 text

launchActivity launchFragmentInContainer ComposeContentTestRule androidx.activity.ComponentActivity Component Activity @Composable Compilation Added to AndroidManifest

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

What Is a Good Test Case?

Slide 26

Slide 26 text

Readable Stable Extendable Fast

Slide 27

Slide 27 text

Readable Stable Extendable Fast

Slide 28

Slide 28 text

onView(withId(R.id.emailEditText)) .perform(replaceText(email)) onView(withId(R.id.passwordEditText)) .perform(replaceText(password), closeSoftKeyboard()) onView(withId(R.id.loginButton)) .perform(click()) val progressBarIR = ViewVisibilityIdlingResource(...) IdlingRegistry.getInstance().register(progressBarIR) onView(withId(R.id.recyclerView)) .check(matches(withItemCount(13))) IdlingRegistry.getInstance().unregister(progressBarIR) onView(withId(R.id.navigation_profile)) .perform(click()) ...

Slide 29

Slide 29 text

// Login screen onView(withId(R.id.emailEditText)) .perform(replaceText(email)) onView(withId(R.id.passwordEditText)) .perform(replaceText(password), closeSoftKeyboard()) onView(withId(R.id.loginButton)) .perform(click()) // Home screen val progressBarIR = ViewVisibilityIdlingResource(...) IdlingRegistry.getInstance().register(progressBarIR) onView(withId(R.id.recyclerView)) .check(matches(withItemCount(13))) IdlingRegistry.getInstance().unregister(progressBarIR) onView(withId(R.id.navigation_profile)) .perform(click()) ...

Slide 30

Slide 30 text

onView(withId(R.id.email)) .perform(replaceText(EMAIL) onView(withId(R.id.password)) .perform(replaceText(PASSWORD) onView(withId(R.id.login)) .perform(click())

Slide 31

Slide 31 text

onView(withId(R.id.email)) login(EMAIL, PASSWORD) }

Slide 32

Slide 32 text

onView(withId(R.id.email)) login(EMAIL, PASSWORD) } open class BaseTestRobot { fun enterText(viewId: Int, text: String) { onView(withId(viewId)) .perform(replaceText(text)) } fun clickOnView(viewId: Int) { onView(withId(viewId)) .perform(click()) } }

Slide 33

Slide 33 text

onView(withId(R.id.email)) login(EMAIL, PASSWORD) } open class BaseTestRobot { fun login(email: String, password: String) { enterText(R.id.email, email) enterText(R.id.password, password) clickOnView(R.id.loginButton) } }

Slide 34

Slide 34 text

onView(withId(R.id.email)) login(EMAIL, PASSWORD) } open class BaseTestRobot { LoginScreenRobot().apply { func() }

Slide 35

Slide 35 text

BASIC OPERATIONS SCREEN ROBOTS TEST CASES @RunWith(AndroidJUnit4::class) class LoginActivityTest { @Test fun shouldBeDisplayedEmptyEmailErrorsWhenValueIsEmpty() { loginScreen { enterEmail(EMPTY_VALUE) emptyEmailErrorDisplayed() } } @Test fun shouldBeDisplayedEmailErrorsWhenValueIsNotEmail() { loginScreen { enterEmail(INCORRECT_EMAIL) incorrectEmailErrorDisplayed() } } @Test fun shouldBeDisplayedEmptyPasswordErrorsWhenValueIsEmpty() { loginScreen { enterPassword(EMPTY_VALUE) emptyPasswordErrorDisplayed() } } ... } class LoginScreenRobot : BaseTestRobot() { fun enterEmail(email: String) = enterText(R.id.email, email) fun enterPassword(email: String) = 
 enterText(R.id.password, password) ... } open class BaseTestRobot { fun enterText(viewId: Int, text: String) { onView(withId(viewId)) .perform(replaceText(text)) } fun clickOnView(viewId: Int) { onView(withId(viewId)) .perform(click()) } 
 ... } ESPRESSO

Slide 36

Slide 36 text

BASIC OPERATIONS SCREEN ROBOTS TEST CASES @RunWith(AndroidJUnit4::class) class LoginActivityTest { @Test fun shouldBeDisplayedEmptyEmailErrorsWhenValueIsEmpty() { loginScreen { enterEmail(EMPTY_VALUE) emptyEmailErrorDisplayed() } } @Test fun shouldBeDisplayedEmailErrorsWhenValueIsNotEmail() { loginScreen { enterEmail(INCORRECT_EMAIL) incorrectEmailErrorDisplayed() } } @Test fun shouldBeDisplayedEmptyPasswordErrorsWhenValueIsEmpty() { loginScreen { enterPassword(EMPTY_VALUE) emptyPasswordErrorDisplayed() } } ... } class LoginScreenRobot : BaseTestRobot() { fun enterEmail(email: String) = enterText(R.id.email, email) fun enterPassword(email: String) = 
 enterText(R.id.password, password) ... } open class BaseTestRobot { fun enterText(viewId: Int, text: String) { val view = device.findObject(By.res(resId(viewId))) view.text = text } fun clickOnView(viewId: Int) { device.findObject(By.res(resIf(viewId))) 
 .click() } 
 ... } UI AUTOMATOR

Slide 37

Slide 37 text

open class BaseTestRobot { @get:Rule val composeTestRule = createComposeRule() fun enterText(tag: String, text: String) { composeTestRule.onNodeWithTag(tag) .performTextInput(text) } fun clickOnView(tag: String) { composeTestRule.onNodeWithTag(tag) .performClick() } } @RunWith(AndroidJUnit4::class) class LoginActivityTest { @Test fun shouldBeDisplayedEmptyEmailErrorsWhenValueIsEmpty() { loginScreen { enterEmail(EMPTY_VALUE) emptyEmailErrorDisplayed() } } @Test fun shouldBeDisplayedEmailErrorsWhenValueIsNotEmail() { loginScreen { enterEmail(INCORRECT_EMAIL) incorrectEmailErrorDisplayed() } } @Test fun shouldBeDisplayedEmptyPasswordErrorsWhenValueIsEmpty() { loginScreen { enterPassword(EMPTY_VALUE) emptyPasswordErrorDisplayed() } } ... } BASIC OPERATIONS SCREEN ROBOTS TEST CASES COMPOSE UI TESTS class LoginScreenRobot : BaseTestRobot() { fun enterEmail(email: String) = enterText("email", email) fun enterPassword(email: String) = 
 enterText("password", password) ... }

Slide 38

Slide 38 text

Challenges 
 In Mobile Testing

Slide 39

Slide 39 text

Light and Dark themes

Slide 40

Slide 40 text

YES NO RTL support

Slide 41

Slide 41 text

Accessibility support Accessibility checks Hello … FONT SIZE Small, Default, Large, Largest DISPLAY SCALING Small, Default, Large, Largest

Slide 42

Slide 42 text

Testing permissions

Slide 43

Slide 43 text

Testing permissions UI interaction GrantPermissionRule ADB commands

Slide 44

Slide 44 text

Testing permissions UI interaction GrantPermissionRule ADB commands Android Test Orchestrator

Slide 45

Slide 45 text

Test Case Test Case Test Case Clean up data after test execution

Slide 46

Slide 46 text

/data/data/PACKAGE/ databases fi les shared_prefs Clean up data after test execution

Slide 47

Slide 47 text

databases fi les shared_prefs @get:Rul e val clearDatabaseRule = ClearDatabaseRule( ) @get:Rul e val clearFileRule = ClearFilesRule( ) @get:Rul e val clearPreferencesRule = ClearPreferencesRule( ) https://github.com/AdevintaSpain/Barista Clean up data after test execution

Slide 48

Slide 48 text

Server Database Pre-populating the database

Slide 49

Slide 49 text

Test Case Database DATA LAYER Query Dependencies UI interaction Pre-populating the database

Slide 50

Slide 50 text

Real data vs Fake data Prod server Dev server Mock/Fake

Slide 51

Slide 51 text

Real data vs Fake data Prod server Dev server DATA SYNCHRONIZATION We need to synchronize data between the production and dev servers. INTERACTION WITH THE SERVER We make requests to the production server. The connection can require certificates, VPN, etc. PRODUCTION BACK-END We always use the latest available production environment.

Slide 52

Slide 52 text

Real data vs Fake data Mock/Fake DATA SYNCHRONIZATION We need to synchronize predefined responses with responses from the production server. NO INTERACTION WITH THE SERVER Responses from the production server can differ from the predefined data. We can use predefined fake responses instead of calling the production server. MAKE TESTS MORE STABLE

Slide 53

Slide 53 text

End-To-End vs Functional tests

Slide 54

Slide 54 text

INTERACTION WITH THE SERVER Interaction with the production server. ENTRY POINT Similar to the entry point of an app. END-TO-END TESTS

Slide 55

Slide 55 text

FUNCTIONAL TESTS INTERACTION WITH THE SERVER Interaction with the non-production server. NAVIGATION Usually, navigation is not needed. ENTRY POINT We can start the test from a required Activity/Fragment.

Slide 56

Slide 56 text

END-TO-END (E2E) TESTS FUNCTIONAL TESTS ENTRY POINT Similar to the entry point of an app. APP VERIFICATION Slow veri fi cation of the app. NAVIGATION Navigate to the required screen. SERVER INTERACTION Interaction with the production server. ENTRY POINT Start the required screen of the app. SERVER INTERACTION Usually interaction with non- production server. NAVIGATION Usually no navigation. UI VERIFICATION Fast veri fi cation of components or screens.

Slide 57

Slide 57 text

END-TO-END TESTS FUNCTIONAL TESTS

Slide 58

Slide 58 text

Flaky Tests

Slide 59

Slide 59 text

External dependencies Framework Test case execution Device and emulator

Slide 60

Slide 60 text

External dependencies - Network connection (VPN) - Network speed - Back-end

Slide 61

Slide 61 text

Framework - Framework issues - Toast, Snackbar, etc - Custom Views

Slide 62

Slide 62 text

Device and emulator - Performance - Noti fi cations - Device memory

Slide 63

Slide 63 text

Test case execution - Simulate User actions - Incorrect state before/after a test case - Toast, Snackbar, etc

Slide 64

Slide 64 text

- Network connection (VPN) - Network speed - Back-end - Framework issues - Toast, Snackbar, etc - Custom Views - Simulate User actions - Incorrect state before/after a test case - Toast, Snackbar, etc - Performance - Noti fi cations - Device memory External dependencies Framework Test case execution Device and emulator

Slide 65

Slide 65 text

Best Practices

Slide 66

Slide 66 text

PAY ATTENTION TO NAMING

Slide 67

Slide 67 text

NO "SLEEP" IN TESTS

Slide 68

Slide 68 text

ALL TEST CASES SHOULD BE INDEPENDENT

Slide 69

Slide 69 text

USE MULTIPLE SMALL TESTS INSTEAD OF ONE BIG TEST

Slide 70

Slide 70 text

DO NOT SPEND TIME NAVIGATING TO THE REQUIRED SCREEN

Slide 71

Slide 71 text

FOLLOW THE 
 "NO FLAKY TESTS" POLICY

Slide 72

Slide 72 text

DO NOT RELY ONLY ON 
 UI TEST AUTOMATION

Slide 73

Slide 73 text

LEARN YOUR TOOLS

Slide 74

Slide 74 text

@alex_zhukovich https://alexzh.com/ THANK YOU FOR LISTENING!