name: android-ui-journey-testing
description: “XML-specified Android UI journey testing, interactive step execution, assertion verification, and JSON outcome reporting.”
category: testing
risk: critical
source: self
source_type: self
date_added: “2026-06-18”
author: Owais
tags: [android, journey-testing, ui-verification, testing, adb, automation]
tools: [claude, cursor, gemini, antigravity]
Android 用户界面旅程测试
概览
此技能概述了在 Android 应用上运行 XML 指定的用户旅程测试的标准工作流程。“旅程” 是一组按顺序排列的用户操作和状态断言,旨在验证端到端功能。旅程 XML 作为应用行为的事实来源。执行器按顺序进行,执行 UI 交互、检查状态断言,并编写标准化的 JSON 结果报告。
何时使用
- 在根据 XML 测试规范评估 Android 应用程序的用户界面行为时使用。
- 在自动化多步骤用户流程(例如登录、结账、导航)并验证预期时使用。
- 在运行验证测试和生成标准化 JSON 测试报告时使用。
- 在使用 ADB 命令调试物理设备或模拟器上的应用程序流程时使用。
运作方式
第1步:解析旅程规范
读取并解析 XML 测试套件结构。根节点<journey>定义测试用例名称,<actions>块包含测试步骤的顺序。
<journeyname="Search and Cart Flow"><description>Verify that searching for an item and adding it to the cart succeeds.</description><actions><action>Search for soda</action><action>Tap the first search result</action><action>Verify that the product detail screen is shown</action></actions></journey>步骤 2:顺序步骤评估
按照指定的确切顺序处理每个<action>元素。测试步骤分为两组:
A. 互动操作(点击、滑动、文本输入)
使用 ADB 执行物理 UI 交互。
- 轻触:轻触目标元素边界的中心:
adb shell input tap<x><y> - 滑动/滚动:从一个坐标滑动到另一个坐标,持续时间为:
adb shell input swipe<x1><y1><x2><y2><duration_ms> - 文本输入:在活动输入字段中输入文本:
adb shell input text"<string>"
如果元素缺失或无法执行操作,则该操作和流程将失败。
B. 陈述断言(期望验证)
以“verify”“check”或“ensure”开头的步骤表示状态断言。
- 检查当前屏幕(使用截图或
uiautomator dump),无需互动或滚动。 - 确认所有子断言都已满足。例如,“验证应用程序是否在主屏幕上并且徽标可见”如果主屏幕未显示或徽标缺失,则会失败。
步骤3:处理失败和崩溃
如果应用程序崩溃、退出、冻结或断言失败:
- 立即停止旅程执行。
- 将失败的步骤标记为
FAILED。 - 将任何后续步骤标记为
跳过。 - 记录失败的确切原因。
第4步:生成JSON报告
将执行结果格式化为标准化的 JSON 模式并写入输出日志。
例子
示例 1:完整行程 XML 规范
<journeyname="Login and Profile Edit"><description>Logs into the app, navigates to settings, and changes user profile information.</description><actions><action>Verify that the username input field is visible</action><action>Tap the username input field</action><action>Type "testuser" into the input</action><action>Tap the password input field</action><action>Type a redacted test password into the input</action><action>Tap the "Login" button</action><action>Verify that the Home dashboard is visible and user profile photo is shown</action></actions></journey>示例 2:标准 JSON 结果报告
{"journey":"Login and Profile Edit","results":[{"action":"Verify that the username input field is visible","status":"PASSED","commands":[],"comment":"Username input detected at bounds [100,200][980,300] via UI dump."},{"action":"Tap the username input field","status":"PASSED","commands":["adb shell input tap 540 250"],"comment":"Tapped center coordinates of username input."},{"action":"Type \"testuser\" into the input","status":"PASSED","commands":["adb shell input text \"testuser\""],"comment":"Username typed successfully."},{"action":"Tap the password input field","status":"PASSED","commands":["adb shell input tap 540 370"],"comment":"Tapped center of password input."},{"action":"Type a redacted test password into the input","status":"PASSED","commands":["adb shell input text \"[REDACTED_PASSWORD]\""],"comment":"Password typed successfully. The actual input value was not stored in the report."},{"action":"Tap the \"Login\" button","status":"PASSED","commands":["adb shell input tap 540 500"],"comment":"Login button clicked."},{"action":"Verify that the Home dashboard is visible and user profile photo is shown","status":"FAILED","commands":[],"comment":"Dashboard loaded but profile photo was missing from the UI header."}]}最佳实践
- ✅计算点击中心点:在解析元素边界如
[x1,y1][x2,y2]时,总是要计算中间坐标:
xcenter=x1+x22,ycenter=y1+y22x_{center} = \frac{x_1 + x_2}{2}, \quad y_{center} = \frac{y_1 + y_2}{2}xcenter=2x1+x2,ycenter=2y1+y2 - ✅包含睡眠缓冲:在交互操作(如按钮点击)后总是添加短暂延迟(例如 1-2 秒),以在执行断言之前让布局和过渡渲染完成。
- ✅快速失败:在遇到第一个失败时立即停止测试。失败后继续测试会导致结果无效。
- ✅安全记录精确命令:在 JSON 输出列表中包含非敏感的原始命令(例如
adb shell input tap)以进行诊断。对输入到密码、一次性密码、令牌、支付或个人数据字段的文本进行脱敏;切勿在报告、CI 日志或共享文件中保存真实的秘密信息。
限制
- 解析器只会评估静态屏幕层级(例如
uiautomator dump)。需要滚动的元素除非明确执行滚动操作,否则会被标记为不可见。 - 非标准的用户界面组件(如自定义的 OpenGL 画布视图)不能通过标准的可访问性树读取,可能需要截图分析或硬编码的点击地图。
- 通过 ADB 的按键事件和文本输入在所有模拟器镜像上不会触发标准软键盘事件,这可能导致输入验证问题。
相关技能
@android-cli- 通用 CLI 工具语法、软件包安装和设备查询。@android_ui_verification- 用于常规界面检查的直接 ADB 脚本模板。