当前位置: 首页 > news >正文

安卓端到端测试_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
http://www.cnnetsun.cn/news/3556040.html

相关文章:

  • 算一笔账要跑4个部门:年度TOP客户采购占比,本体语义怎么算出来
  • 如何快速美化Mac微信界面:5大主题模式终极个性化指南
  • 响应式编程与Kafka结合实现高并发消息处理
  • 哔咔漫画下载器终极指南:5个简单步骤打造个人离线漫画图书馆
  • Vue+SpringBoot健身房管理系统实战:前后端分离项目从零搭建到部署
  • 中美AI发展路径差异与本土化创新思考
  • S7-1200以太网通信配置与优化实战指南
  • 国学启蒙≠背三字经:2026年儿童传统文化学习的新思路
  • QMCDecode终极指南:3步解锁QQ音乐加密音频的免费方案
  • 一文搞懂 ROS2 C++ 订阅节点类成员
  • AI 行为分析反采集系统深度拆解:特征工程、机器学习模型与采集行为优化全链路实战
  • AI编程环境一键安装:从Claude Code到DeepSeek的完整配置指南
  • 如何快速构建离线漫画库:面向哔咔漫画用户的完整指南
  • STM32串口通讯实战:硬件连接与软件配置详解
  • STM32F103开发板程序下载全攻略与避坑指南
  • [GESP202606 八级] 线网建设
  • Claude Code 的 agent-memory 机制,给 subagent 一块真正属于自己的长期记忆
  • 多线程断点续传下载器设计:从状态驱动到工程实践
  • Obsidian 同步有什么简单方法?装个插件就行,小白必用
  • 腾讯云数据智能:构建可信、可控、可演进的 Data Agent
  • C++操作Excel完整指南:从LibXL到OpenXLSX的实战方案
  • 钾离子通道视紫红质稳定性突破及其在光遗传学中的应用
  • Viktor智能体AI编辑工作流:从原理到批量生产实践
  • Android16 蓝牙打开时,状态栏显示蓝牙图标
  • Grok CLI重大更新前瞻:AI命令行工具部署与代码生成实践
  • C2000 HRCAP高分辨率捕获模块:从校准到实战的精密时间测量指南
  • 深入解析TI EVE内存架构:DMEM、WBUF、IBUF与程序缓存协同设计
  • YOLOv11室内办公环境绝缘手套目标检测数据集
  • 深入解析C2000 DSP Bootloader:从启动模式到数据流协议实战
  • 具身智能的TVA-VLA双引擎架构(系列)