安卓端到端测试_android-e2e-testing

安卓端到端测试_android-e2e-testing 以下为本文档的中文说明该技能用于使用ADB在Android模拟器上测试Expo Router功能涵盖安装APK、截图、坐标点击、滑动、键盘输入和文本提取等操作。主要功能是提供一套在Android模拟器上手动测试Expo Router屏幕和组件的端到端测试方法。使用场景包括在实现或修改原生Android UI组件后工具栏、标签页、菜单在验证Jetpack Compose组件时在运行native-navigation或其他E2E应用的Android版本时在提交影响Android特定行为的PR之前。前置条件包括Android模拟器必须正在运行推荐使用Pixel模拟器而非平板模拟器进行标准手机尺寸测试。典型测试步骤包括验证模拟器连接状态检查模拟器屏幕分辨率对坐标计算很重要构建并安装测试APK执行截图、点击、滑动等交互操作并验证结果。该技能提供了一套轻量级的Android E2E测试方案适合在开发过程中快速验证UI行为。Android E2E Testing for Expo RouterUseadbto manually test Expo Router screens and components on Android emulators.When to UseAfter implementing or modifying native Android UI components (toolbars, tabs, menus)When verifying Jetpack Compose components (expo/ui/jetpack-compose)When running thenative-navigationor other E2E apps on AndroidBefore opening a PR that touches Android-specific behaviorPrerequisitesAn Android emulator must be running. PreferPixel emulatorsover tablet ones for standard phone-sized testing.# Verify emulator is connectedadb devices# Check which emulator is runningadb-sDEVICE_ID emu avd name# Check screen resolution (important for coordinate calculations)adb-sDEVICE_ID shell wm sizeStep 1: Build and LaunchRouter E2E appsPackage name:dev.expo.routere2eEach app inapps/router-e2e/__e2e__/has a correspondingyarn android:namescript. Checkapps/router-e2e/package.jsonfor available scripts.cdapps/router-e2eyarnandroid:[APP_NAME]# e.g. yarn android:native-navigationIf the app is already built, relaunch it:adb shell monkey-pdev.expo.routere2e-candroid.intent.category.LAUNCHER1To find the package name of any installed app:adb shell pm list packages|grep-ikeywordStep 2: Navigate Using UI DumpCRITICAL: Always useuiautomator dumpfor element coordinates.Screenshot pixel coordinates have display scaling factors that make them unreliable foradb shell input tap. The UI dump provides actual device coordinates.Dump the view hierarchyadb shell uiautomator dump /sdcard/ui.xmladb shellcat/sdcard/ui.xmlThis returns XML with every UI element including:text— displayed textcontent-desc— accessibility description (useful for icon buttons)bounds— position as[left,top][right,bottom]clickable— whether the element responds to tapsclass— Android view classFind and tap an elementSearch the XML for your target element bytextorcontent-descExtract theboundsattribute:bounds[left,top][right,bottom]Calculate center:x (left right) / 2,y (top bottom) / 2Tap:adb shell input tapxyExample:Forbounds[367,498][714,633]:x (367 714) / 2 540y (498 633) / 2 565adb shell input tap540565Wait for navigation to settleAfter tapping a navigation element, wait before verifying:sleep1For slow transitions or heavy screens, usesleep 2.Step 3: InteractTap itemsadb shell input tapxyScroll# Scroll downadb shell input swipe5401500540500300# Scroll upadb shell input swipe5405005401500300# Scroll further (larger distance)adb shell input swipe5401500540200500Type textadb shell input texthello%sworld# %s spacePress hardware buttonsadb shell input keyevent4# Backadb shell input keyevent3# Homeadb shell input keyevent82# Menu / React Native dev menuLong pressadb shell input swipexyxy1000Step 4: VerifyVisual verification via screenshotadb shell screencap-p/sdcard/screenshot.pngadb pull /sdcard/screenshot.png /tmp/screenshot.pngThen use theReadtool to view/tmp/screenshot.png. Screenshots are useful for:Confirming visual appearance (colors, layout, styling)Verifying toolbar/tab positioningChecking selection states and visual feedbackNote:Use screenshots forvisualverification only. For element positions and tapping, always useuiautomator dump.Programmatic verification via UI dumpadb shell uiautomator dump /sdcard/ui.xmladb shellcat/sdcard/ui.xmlSearch the XML for expected content:Verify text content appearsCheckcontent-descfor accessibility labelsConfirm element presence after navigationVerify selection states (look for checkmarks, content-desc changes)Check for errors# React Native JS errorsadb logcat-d-sReactNativeJS:E|tail-20# Crash logsadb logcat-bcrash-d# All recent errorsadb logcat-d*:E|tail-30Step 5: Report ResultsAfter testing, summarize results in a table:TestResultNavigation to screenPASS/FAILComponent renders correctlyPASS/FAILInteraction worksPASS/FAILNo JS errors in logcatPASS/FAILInclude details for any failures: what was expected vs what happened, relevant logcat output, and screenshots.Preferably attach screenshots for features you tested.Testing Jetpack Compose ComponentsComponents fromexpo/ui/jetpack-compose(likeHorizontalFloatingToolbar,IconButton,Host) render as Compose views inside React Native. In UI dumps they appear as:androidx.compose.ui.platform.ComposeView— the Compose containerandroid.widget.HorizontalScrollView— inside toolbar layoutsandroid.widget.Button— Compose buttonsandroid.view.Viewwithcontent-desc— icon buttons with accessibility labelsWhen testing Compose components:Look forcontent-descattributes to identify buttons (e.g.,content-descClear selection)TheComposeViewwrapper may have different bounds than the inner interactive elementsTap the interactive element’s bounds, not the container’sTroubleshootinguiautomator dump fails or returns emptyThis can happen during animations or transitions. Wait and retry:sleep2adb shell uiautomator dump /sdcard/ui.xmladb shellcat/sdcard/ui.xmlTap doesn’t registerRecalculate coordinates from a fresh UI dump — the layout may have shiftedEnsure you’re tapping aclickabletrueelementTry tapping the parent element if the child isn’t clickableApp navigated to wrong screen or went to homeThe Back button (keyevent 4) can exit the app entirely if on the root screenUsemonkeycommand to relaunch:adb shell monkey -p dev.expo.routere2e -c android.intent.category.LAUNCHER 1Wait 2 seconds after launch before interactingMetro bundler not connectingadb reverse tcp:8081 tcp:8081App crashes on launch# Check crash bufferadb logcat-bcrash-d# Look for fatal exceptionsadb logcat-d|grep-A10FATAL EXCEPTIONReload the app# Open React Native dev menu and tap Reloadadb shell input keyevent82# Or force-stop and relaunchadb shell am force-stop dev.expo.routere2eadb shell monkey-pdev.expo.routere2e-candroid.intent.category.LAUNCHER1Disable Animations (Recommended for Testing)Disabling animations prevents flaky UI dumps and makes testing more reliable:adb shell settings put global window_animation_scale0adb shell settings put global transition_animation_scale0adb shell settings put global animator_duration_scale0Re-enable when done:adb shell settings put global window_animation_scale1adb shell settings put global transition_animation_scale1adb shell settings put global animator_duration_scale1Complete Example: Testing a Toolbar Screen# 1. Launch appadb shell monkey-pdev.expo.routere2e-candroid.intent.category.LAUNCHER1sleep2# 2. Dump UI to find navigation buttonadb shell uiautomator dump /sdcard/ui.xmladb shellcat/sdcard/ui.xml# Find: content-descAndroid Toolbar bounds[367,498][714,633]# Center: (540, 565)# 3. Navigate to screenadb shell input tap540565sleep1# 4. Take screenshot to verify visual appearanceadb shell screencap-p/sdcard/screenshot.pngadb pull /sdcard/screenshot.png /tmp/screenshot.png# 5. Dump UI to find toolbar buttonsadb shell uiautomator dump /sdcard/ui.xmladb shellcat/sdcard/ui.xml# Find buttons by content-desc: Clear selection, Select all, Delete, Add# 6. Test toolbar interactionsadb shell input tap4572233# Select all button centersleep1# 7. Verify state changedadb shell screencap-p/sdcard/screenshot.pngadb pull /sdcard/screenshot.png /tmp/screenshot.png# 8. Check for errorsadb logcat-d-sReactNativeJS:E|tail-20