UIAutomatorViewer 教程:Android 测试检查器

什么是 UiAutomatorViewer?

“UIautomatorviewer”是一款图形用户界面工具,用于扫描和分析 Android 应用程序的 UI 组件。要使用 Appium 自动化任何 Android 应用程序,用户需要识别 AUT(待测应用程序)中的对象。通过“UIautomatorviewer”,您可以检查 Android 应用程序的 UI,以找出元素的层次结构并查看其不同的属性(id、text 等)。

在执行自动化脚本时,Appium 使用“Uiautomatorviewer”来识别对象的不同属性,并使用这些属性来识别所需的对象。

UiAutomatorViewer

如何下载和安装 UIAutomator

“Uiautomatorviewer”是 Android SDK 管理器的一部分,一旦您安装了 SDK 管理器即可访问。从此处下载并安装 Android SDK 管理器

Android SDK 安装后,导航到链接

c:\users\<username>\AppData\Local\Android\sdk\tools

您会看到一个名为的批处理文件

uiautomatorviewer.bat

双击它以启动“Uiautomatorviewer”图形用户界面

Download & Install UIAutomator

如何使用 Uiautomatorviewer 查找应用程序中的对象

  1. 在您的设备上启用“开发者”选项。点击此处了解如何在 Android 设备上启用开发者选项
  2. 通过 USB 线将您的 Android 设备连接到电脑
  3. 从应用程序中选择“Guru99”应用程序

Use Uiautomatorviewer to Find Objects in My Application

  1. 点击“设备截图”按钮以刷新“Uiautomatorviewer”并加载 Guru99 应用程序的 GUI 到“Uiautomatorviewer”中

Use Uiautomatorviewer to Find Objects in My Application

  1. 刷新完成后,会打开 Guru99 应用程序的截图

Uiautomatorviewer to Find Objects in My Application

  1. 如上图所示,窗口的右侧有两个面板。

上方面板包含 UI 组件的节点层次结构,点击每个节点可在下方面板中查看 UI 元素的属性

  1. 在上图中选择“测验”按钮以查看不同的属性(文本、资源 ID…)

Use Uiautomatorviewer to Find Objects in My Application

如何使用这些属性来识别自动化元素

好吧,您不能直接使用这些属性,每个属性都有其他的名称。让我们看看如何使用这些属性值来工作。以下属性可用于识别 Guru99 应用程序中的“测验”按钮。

  • text 属性可用作“name”

Use Name Properties to Identify Elements For Automation

  • resource-id 属性可用作“id”

Use Id Properties to Identify Elements For Automation

  • class 属性可用作“className”

Use className Properties to Identify Elements For Automation

  • content-desc 属性可用作“AccessibilityId”

    Use AccessibilityId Properties to Identify Elements For Automation

    除了上述属性,我们还可以编写 xpaths 进行对象识别

使用 Uiautomatorviewer 时可能遇到的错误

  • 我看到错误 - “adb 未检测到任何 Android 设备”,如下图所示,我该如何解决这个问题?

Error One Might Encounter While Using Uiautomatorviewer

解决方案:请确保您的设备已连接到电脑