news 2026/7/21 9:13:44

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

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
安卓端到端测试_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 Router

Useadbto manually test Expo Router screens and components on Android emulators.

When to Use

  • After 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 Android
  • Before opening a PR that touches Android-specific behavior

Prerequisites

An 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 size

Step 1: Build and Launch

Router E2E apps

Package name:dev.expo.routere2e

Each app inapps/router-e2e/__e2e__/has a correspondingyarn android:<name>script. Checkapps/router-e2e/package.jsonfor available scripts.

cdapps/router-e2eyarnandroid:[APP_NAME]# e.g. yarn android:native-navigation

If the app is already built, relaunch it:

adb shell monkey-pdev.expo.routere2e-candroid.intent.category.LAUNCHER1

To find the package name of any installed app:

adb shell pm list packages|grep-i<keyword>

Step 2: Navigate Using UI Dump

CRITICAL: 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 hierarchy

adb shell uiautomator dump /sdcard/ui.xml&&adb shellcat/sdcard/ui.xml

This returns XML with every UI element including:

  • text— displayed text
  • content-desc— accessibility description (useful for icon buttons)
  • bounds— position as[left,top][right,bottom]
  • clickable— whether the element responds to taps
  • class— Android view class

Find and tap an element

  1. Search the XML for your target element bytextorcontent-desc
  2. Extract theboundsattribute:bounds="[left,top][right,bottom]"
  3. Calculate center:x = (left + right) / 2,y = (top + bottom) / 2
  4. Tap:
adb shell input tap<x><y>

Example:Forbounds="[367,498][714,633]":

  • x = (367 + 714) / 2 = 540
  • y = (498 + 633) / 2 = 565
adb shell input tap540565

Wait for navigation to settle

After tapping a navigation element, wait before verifying:

sleep1

For slow transitions or heavy screens, usesleep 2.

Step 3: Interact

Tap items

adb shell input tap<x><y>

Scroll

# Scroll downadb shell input swipe5401500540500300# Scroll upadb shell input swipe5405005401500300# Scroll further (larger distance)adb shell input swipe5401500540200500

Type text

adb shell input text"hello%sworld"# %s = space

Press hardware buttons

adb shell input keyevent4# Backadb shell input keyevent3# Homeadb shell input keyevent82# Menu / React Native dev menu

Long press

adb shell input swipe<x><y><x><y>1000

Step 4: Verify

Visual verification via screenshot

adb shell screencap-p/sdcard/screenshot.png&&adb pull /sdcard/screenshot.png /tmp/screenshot.png

Then use theReadtool to view/tmp/screenshot.png. Screenshots are useful for:

  • Confirming visual appearance (colors, layout, styling)
  • Verifying toolbar/tab positioning
  • Checking selection states and visual feedback

Note:Use screenshots forvisualverification only. For element positions and tapping, always useuiautomator dump.

Programmatic verification via UI dump

adb shell uiautomator dump /sdcard/ui.xml&&adb shellcat/sdcard/ui.xml

Search the XML for expected content:

  • Verify text content appears
  • Checkcontent-descfor accessibility labels
  • Confirm element presence after navigation
  • Verify selection sta
    tes (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-30

Step 5: Report Results

After testing, summarize results in a table:

TestResult
Navigation to screenPASS/FAIL
Component renders correctlyPASS/FAIL
Interaction worksPASS/FAIL
No JS errors in logcatPASS/FAIL

Include 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 Components

Components from@expo/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 container
  • android.widget.HorizontalScrollView— inside toolbar layouts
  • android.widget.Button— Compose buttons
  • android.view.Viewwithcontent-desc— icon buttons with accessibility labels

When testing Compose components:

  1. Look forcontent-descattributes to identify buttons (e.g.,content-desc="Clear selection")
  2. TheComposeViewwrapper may have different bounds than the inner interactive elements
  3. Tap the interactive element’s bounds, not the container’s

Troubleshooting

uiautomator dump fails or returns empty

This can happen during animations or transitions. Wait and retry:

sleep2&&adb shell uiautomator dump /sdcard/ui.xml&&adb shellcat/sdcard/ui.xml

Tap doesn’t register

  • Recalculate coordinates from a fresh UI dump — the layout may have shifted
  • Ensure you’re tapping aclickable="true"element
  • Try tapping the parent element if the child isn’t clickable

App navigated to wrong screen or went to home

  • The Back button (keyevent 4) can exit the app entirely if on the root screen
  • Usemonkeycommand to relaunch:adb shell monkey -p dev.expo.routere2e -c android.intent.category.LAUNCHER 1
  • Wait 2 seconds after launch before interacting

Metro bundler not connecting

adb reverse tcp:8081 tcp:8081

App crashes on launch

# Check crash bufferadb logcat-bcrash-d# Look for fatal exceptionsadb logcat-d|grep-A10"FATAL EXCEPTION"

Reload the app

# Open React Native dev menu and tap Reloadadb shell input keyevent82# Or force-stop and relaunchadb shell am force-stop dev.expo.routere2e&&adb shell monkey-pdev.expo.routere2e-candroid.intent.category.LAUNCHER1

Disable 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_scale0

Re-enable when done:

adb shell settings put global window_animation_scale1adb shell settings put global transition_animation_scale1adb shell settings put global animator_duration_scale1

Complete 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.xml&&adb shellcat/sdcard/ui.xml# Find: content-desc="Android 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.png&&adb pull /sdcard/screenshot.png /tmp/screenshot.png# 5. Dump UI to find toolbar buttonsadb shell uiautomator dump /sdcard/ui.xml&&adb 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.png&&adb pull /sdcard/screenshot.png /tmp/screenshot.png# 8. Check for errorsadb logcat-d-sReactNativeJS:E|tail-20
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/21 9:12:31

算一笔账要跑4个部门:年度TOP客户采购占比,本体语义怎么算出来

某装备制造企业的财务总监&#xff0c;每年年底会被老板问同一个问题&#xff1a;今年我们第一大客户&#xff0c;到底用了我们多少原材料&#xff0c;对应采购金额是多少&#xff1f;听起来不复杂&#xff0c;这位财务总监却答不上来。不是他不尽职&#xff0c;是这笔账在系统…

作者头像 李华
网站建设 2026/7/21 9:10:00

如何快速美化Mac微信界面:5大主题模式终极个性化指南

如何快速美化Mac微信界面&#xff1a;5大主题模式终极个性化指南 厌倦了千篇一律的Mac微信默认界面&#xff1f;想要打造独特个性的聊天环境却不知从何入手&#xff1f;WeChatExtension-ForMac这款开源插件将彻底改变你的微信使用体验&#xff0c;通过简单几步即可实现微信主题…

作者头像 李华
网站建设 2026/7/21 9:09:51

响应式编程与Kafka结合实现高并发消息处理

1. 响应式编程与Kafka的化学反应在当今高并发、低延迟的应用场景中&#xff0c;传统的同步阻塞式架构逐渐暴露出性能瓶颈。我去年参与的一个物联网平台项目就遇到了这样的困境&#xff1a;当设备同时上报数据时&#xff0c;传统的Spring MVC架构在每秒5000消息的压力下&#xf…

作者头像 李华
网站建设 2026/7/21 9:09:29

哔咔漫画下载器终极指南:5个简单步骤打造个人离线漫画图书馆

哔咔漫画下载器终极指南&#xff1a;5个简单步骤打造个人离线漫画图书馆 还在为哔咔漫画的网络加载缓慢而烦恼吗&#xff1f;picacomic-downloader 是一款专为 manhuabika.com&#xff08;哔咔漫画、pica漫画、bika漫画、PicACG&#xff09;设计的专业级多线程下载器&#xff…

作者头像 李华
网站建设 2026/7/21 9:09:26

Vue+SpringBoot健身房管理系统实战:前后端分离项目从零搭建到部署

很多刚入门的开发者都有这样的困惑&#xff1a;学了Vue、SpringBoot这些主流技术&#xff0c;也看了不少教程&#xff0c;但一到自己动手做项目就无从下手。特别是想做一个能跑起来的、有完整前后端交互的“管理系统”时&#xff0c;面对数据库设计、接口联调、权限控制这些环节…

作者头像 李华
网站建设 2026/7/21 9:05:08

中美AI发展路径差异与本土化创新思考

1. 为什么说"中国版Anthropic"是个伪命题最近AI圈有个热门话题——要不要搞"中国版Anthropic"。作为一个在AI领域摸爬滚打多年的从业者&#xff0c;我觉得这事得掰开了说。Anthropic这家公司之所以特别&#xff0c;是因为它从创立之初就带着强烈的使命导向…

作者头像 李华