通过Appium和Xcode Accessibility Inspector获取iOS应用元素定位的方法

在 iOS 移动应用程序上使用选择器查找元素定位是我们在移动端 UI 自动化测试的先决条件。
但是,由于应用程序内容在原生 iOS 应用程序中的呈现方式,我们可以用来定位应用程序元素的选择器与 Web 浏览器元素有很大不同。
在本文中,我们将了解 3 种常用方法来确定元素选择器,以便在 iOS 应用程序中查找元素定位,进而用于进行自动化移动 UI 测试。

XCTest 框架和 Appium XCUITest 驱动程序
对于原生 iOS 应用,应用内容通过 WKWebView(iOS 8.0 或更高版本)呈现。Apple 开发的官方 XCTest 框架提供了一种方法,可以根据每个元素的渲染内容,根据其元素类型和 WKWebView 上下文中的元素属性来定位元素。但是,XCTest 框架仅支持 Swift 和 Objective-C,因此当 iOS 应用程序是用 React Native 而不是 Swift 编写时,它就不适用于移动端 UI 自动化测试了。 
如果已经对于移动端 UI 自动化测试有所了解或应用的话,可能会对这样一种替代方案有所耳闻: Appium XCUITest Driver(以前称为 UIAutomation Driver for iOS)。 Appium XCUITest 驱动程序利用 Apple 的 XCTest 框架及其底层库来提升 iOS 应用程序的自动化,并支持多种语言,包括 Java、Python、javascript、Ruby、C# 和 PHP。接下来将以 Appium XCUITest 驱动程序为例来看看我们如何检查 iOS 应用程序并找到元素定位的。

查找 iOS 移动应用的元素选择器
XCTest 框架或 Appium XCUITest 驱动程序提供了一种通过编程方式与 app 元素交互的方法,但我们仍然需要手动识别元素选择器并将选择器提供给我们的元素进行 UI 自动化测试。由于我们必须使用 iOS 模拟器或真实的 iOS 设备来打开应用程序,而设备上并没有内置的检查器来为我们提供这些信息,反观在各种浏览器中,就有各种开发者工具可以用来提供这些信息。
 
以下是 3 种经常使用来检查原生 iOS 应用程序内容并确定选择器以定位元素的方法。 
顺便说一下,我们将用于演示的示例 iOS 应用程序如下。它使用 React Native 编写并在 iOS 模拟器上运行。

(在 iOS 模拟器上运行的示例 react-native 应用程序的屏幕截图)

1. driver.getPageSource()

Appium XCUITest 驱动程序提供了一种方法: getPageSource(),它可以在终端控制台中直接打印出当前呈现的应用页面的页面源代码。这是一种可以快速查看应用页面中所有元素的全貌,并确定可用于获取元素定位的元素类型和属性的快速方法。
只需要在 UI 自动化测试中插入以下代码(如下Java代码):
System.out.println(driver.getPageSource());
下面是示例应用程序的页面源代码片段。

<XCUIElementTypeOther type="XCUIElementTypeOther" name="Welcome to Example App" label="Welcome to Example App" enabled="true" visible="true" x="0" y="76" width="375" height="77">
  <XCUIElementTypeStaticText type="XCUIElementTypeStaticText" value="Welcome to Example App" name="Welcome to Example App" label="Welcome to Example App" enabled="true" visible="true" x="24" y="100" width="327" height="29"/>
</XCUIElementTypeOther>
<XCUIElementTypeOther type="XCUIElementTypeOther" name="Section One This is the description for Section One." label="Section One This is the description for Section One." enabled="true" visible="true" x="0" y="184" width="375" height="55">
  <XCUIElementTypeStaticText type="XCUIElementTypeStaticText" value="Section One" name="Section One" label="Section One" enabled="true" visible="true" x="24" y="184" width="327" height="25"/>
  <XCUIElementTypeStaticText type="XCUIElementTypeStaticText" value="This is the description for Section One." name="This is the description for Section One." label="This is the description for Section One." enabled="true" visible="true" x="24" y="216" width="327" height="23"/>
</XCUIElementTypeOther>
<XCUIElementTypeOther type="XCUIElementTypeOther" name="Section Two This is the description for Section Two." label="Section Two This is the description for Section Two." enabled="true" visible="true" x="0" y="270" width="375" height="54">
  <XCUIElementTypeStaticText type="XCUIElementTypeStaticText" value="Section Two" name="Section Two" label="Section Two" enabled="true" visible="true" x="24" y="270" width="327" height="25"/>
  <XCUIElementTypeStaticText type="XCUIElementTypeStaticText" value="This is the description for Section Two." name="This is the description for Section Two." label="This is the description for Section Two." enabled="true" visible="true" x="24" y="302" width="327" height="23"/>
</XCUIElementTypeOther>
<XCUIElementTypeOther type="XCUIElementTypeOther" name="Section Three This is the description for Section Three. Example Button" label="Section Three This is the description for Section Three. Example Button" enabled="true" visible="true" x="0" y="356" width="375" height="113">
  <XCUIElementTypeStaticText type="XCUIElementTypeStaticText" value="Section Three" name="Section Three" label="Section Three" enabled="true" visible="true" x="24" y="356" width="327" height="25"/>
  <XCUIElementTypeStaticText type="XCUIElementTypeStaticText" value="This is the description for Section Three." name="This is the description for Section Three." label="This is the description for Section Three." enabled="true" visible="true" x="24" y="388" width="327" height="44"/>
  <XCUIElementTypeButton type="XCUIElementTypeButton" name="Example Button" label="Example Button" enabled="true" visible="true" x="24" y="431" width="327" height="38"/>
</XCUIElementTypeOther>

可以看到,打印出来的页面源代码提供了每个元素的丰富信息,包括元素的类型和属性等,我们可以使用这些信息来确定每个元素定位。
例如,对于 Example Button  按钮元素,其页面源代码为:
<XCUIElementTypeButton type="XCUIElementTypeButton" name="Example Button" label="Example Button" enabled="true" visible="true" x="24" y="431" width="327" height="38"/> </XCUIElementTypeOther>
我们可以很容易地确定它的元素定位为:
driver.findElement(By.xpath("//XCUIElementTypeButton[@name='Example Button']"))

2. Accessibility Inspector

Accessibility Inspector是 Apple 开发的一个鲜为人知但非常有用的工具,可帮助开发人员提高 iOS 应用程序的可访问性。该工具是与 Xcode 捆绑在一起的,因此需要在 Mac 电脑上安装Xcode。Accessibility Inspector 通过解析和识别 iOS 应用程序上的每个元素并检查元素的类型和可访问性标签来明确 iOS 应用程序的可访问性功能及问题。由于它提供了有关应用程序上每个元素的详细信息,因此它也可以很容易地用于在自动化 UI 测试中识别每个元素的选择器。
下面是 Accessibility Inspector 为 Example Button 按钮元素提供的信息:

可以看到,Accessibility Inspector 显示 Example Button  按钮元素具有 Button 类型和 Example Button标签,因此我们就可以使用它们来确定该元素定位:
driver.findElement(By.xpath("//XCUIElementTypeButton[@name='Example Button']"))
在确定确切的元素选择器方面,Accessibility Inspector 一开始并不总是很容易使用,但是一旦熟悉了 Accessibility Inspector 如何呈现有关应用程序元素的信息,它将是一种快速且轻量级的工具,可以轻松完成工作。
 
实操示例:
1)Accessibility Inspector 在Xcode的入口:Xcode —— Open Developer Tool —— Accessibility Inspector


2)连接好设备后点击开始按钮即可开始使用。

3. Appium Desktop

Appium Desktop 是 Appium 团队为 Mac、Windows 和 Linux 开发的桌面应用程序。它是许多 Appium 开发人员在移动端UI自动化测试中检查 iOS 和 Android 应用程序元素的流行工具。 
下面是 Appium Desktop 显示的关于 Example Button 按钮元素的内容:

 (示例应用和 Appium 桌面的屏幕截图)

可以看到,中间部分的 App Source 显示了当前应用页面的完整层次结构,右侧部分的 Selected Element 显示了详细的信息和属性列表 。更有用的是它还提供了用于定位当前元素的最佳选择器,即 accessibility id 或者 xpath,这二者非常方便使用。
Appium Desktop 还提供了其他不错的功能,例如直接单击/点击所选元素,以及在应用页面更改后刷新页面源。可以参考官方的Appium Desktop代码仓库了解更多信息。
 
最后的话
总之,Appium Desktop 是一种用于检查 iOS 应用程序上的元素获取元素定位的流行且推荐的工具。如果你是刚开始编写 UI 自动化测试,那么利用它来全面了解应用程序和页面元素也很有用。一旦你相当熟悉了应用程序和页面结构,直接在代码中使用driver.getPageSource() 或使用 Accessibility Inspector 是快速找到元素定位的好选择。
 

Reference

1、3 Ways to Find Element Selector for iOS App with Appium:https://stephenweixu.com/blog/3-ways-to-find-element-selector-for-ios-app-with-appium
2、iOS 测试 WebDriverAgent 简介:https://testerhome.com/topics/4904 (WebDriverAgent Inspector 的使用)
3、Appium1.6 定位iOS元素和操作元素 :https://www.cnblogs.com/meitian/p/7373460.html
4、Appium+Python3+iOS定位元素:https://wangxiaoxi.cn/posts/appium-ios-2/ (元素定位慢的问题)
5、uiautomator2 使用Python测试 Android应用:https://www.cnblogs.com/fnng/p/8486863.html (也有提到 这种方法可以获取元素定位 )
 

<think>好的,用户之前问了关于UI Automator的报错问题,我给出了详细的解决方案,其中提到如果问题无法解决,可以尝试使用Appium Desktop的Inspector工具作为备选方案。现在用户接着询问这个工具的具体使用方法,说明他们可能遇到了UI Automator的问题,或者对Appium Inspector有实际需求。 首先,我需要确定用户的使用场景。他们可能是在进行移动应用自动化测试,特别是在处理UI元素定位时遇到了困难。使用Inspector工具可以帮助他们查看应用的UI层次结构,定位元素,生成代码,这对于调试编写测试脚本非常有用。 接下来,用户可能是测试工程师、开发人员或者刚接触Appium的新手,需要一步步的指导。考虑到用户之前遇到的是Android相关问题,可能需要重点讲解Android的设置,但也要覆盖iOS部分,以备不时之需。 用户的需求是学习如何使用Appium Desktop的Inspector工具。深层需求可能包括如何配置会话、连接设备、定位元素以及解决常见的连接问题。需要确保步骤清晰,避免技术术语过多,同时提供常见问题的解决方法,比如设备连接失败、无法获取UI结构等。 我需要组织回答的结构,从准备工作开始,然后分步讲解配置启动Inspector,接着是主要功能使用技巧,最后是常见问题处理。这样用户能按顺序操作,遇到问题也能及时找到解决办法。同时,加入截图示例代码能帮助用户更好地理解,但由于文本回复,需要用文字描述代替截图的位置。 还需要注意用户可能遇到的权限问题,比如Android的USB调试权限,iOS的开发者模式信任问题,这些在准备工作中要强调。此外,提醒用户保持设备唤醒状态,避免因屏幕锁定导致连接失败。 最后,总结使用场景,帮助用户理解Inspector工具在测试流程中的作用,并鼓励他们在遇到问题时参考提供的解决方案。确保回答全面,覆盖各个步骤可能的问题,让用户能够顺利使用Inspector工具进行元素定位测试脚本开发。</think>以下是 **Appium Desktop Inspector工具** 的详细使用指南: --- ### **一、核心功能定位** Appium Inspector 是专为移动应用自动化测试设计的可视化调试工具,主要用于: 1. **实时查看UI层级结构** 2. **精准定位元素属性**(ID/XPath/Class) 3. **录制自动化操作脚本** 4. **生成多语言测试代码** 5. **验证元素定位策略** --- ### **二、准备工作** #### **Android设备配置** 1. 开启开发者选项:连续点击 **设置 > 关于手机 > 版本号** 7次 2. 启用 **USB调试** **USB安装** 权限 3. 连接电脑后执行: ```bash adb devices # 确认设备序列号出现 adb tcpip 5555 # 可选:切换为WiFi调试 ``` #### **iOS设备配置** 1. 启用 **开发者模式**(iOS 16+需在 **设置 > 隐私与安全** 中开启) 2. 安装最新版 **Xcode** 并同意许可协议 3. 使用USB连接后信任设备: ```bash idevicepair pair # 验证连接 ``` --- ### **三、操作流程详解** #### **步骤1:创建会话配置** 1. 启动Appium Desktop,点击 **Start Server** 2. 点击 **New Session Window** 按钮 3. 填写必要参数(JSON格式): ```json { "platformName": "Android", "platformVersion": "13", "deviceName": "Pixel_6_Pro", "app": "/path/to/app.apk", "automationName": "UiAutomator2", "udid": "设备序列号" // 通过`adb devices`获取 } ``` #### **步骤2:启动Inspector** 1. 点击 **Start Session** 按钮 2. 等待设备自动安装 **Appium Settings** **Appium UIAutomator2 Server** 3. 成功连接后界面分为三部分: - 左侧:实时UI树状结构 - 右侧:元素属性详情 - 底部:操作记录面板 --- ### **四、核心功能演示** #### **1. 元素定位模式** - **点选定位**:点击界面中的 **Tap to Select Element** 按钮,直接点击屏幕元素 - **XPath验证**:在搜索框输入 `//android.widget.Button[@text='登录']` - **属性查看**: ```text resource-id: com.example:id/login_btn bounds: [42,1836][1038,1980] checkable: false ``` #### **2. 脚本录制** 1. 点击 **Start Recording** 开始录制 2. 执行以下操作自动生成代码: ```python # Python示例 el1 = driver.find_element(by=AppiumBy.ID, value="com.example:id/username") el1.send_keys("testuser") el2 = driver.find_element(by=AppiumBy.ANDROID_UIAUTOMATOR, value='new UiSelector().text("登录")') el2.click() ``` #### **3. 高级功能** - **屏幕截图分析**:点击 **Take Screenshot** 保存当前界面快照 - **手势模拟**:通过 **Actions** 标签模拟滑动、长按等操作 - **性能监控**:在 **Advanced** 标签中开启 **Show System Apps** 查看后台进程 --- ### **五、调试技巧** #### **常见问题处理** | 问题现象 | 解决方案 | |---------------------------------|----------------------------------| | 无法连接设备 | 检查`adb devices`是否显示设备,重启Appium服务 | | 界面空白无元素 | 确认应用已启动到前台,关闭系统弹窗 | | 元素属性缺失 | 在Desired Capabilities添加:`"appWaitActivity": "*"` | | iOS设备元素无法识别 | 使用 **XCUITest** 替代 **UIAutomation** | #### **Android特殊配置** ```json // 解决WebView检测问题 { "chromedriverExecutable": "/path/to/chromedriver", "showChromedriverLog": true } ``` #### **iOS特殊配置** ```json // 处理弹窗权限 { "autoAcceptAlerts": true, "safariInitialUrl": "http://localhost", "wdaStartupRetries": 4 } ``` --- ### **六、最佳实践建议** 1. **元素定位优先级**: ```text ID定位 > Accessibility ID > XPath > 坐标定位 ``` 2. **使用相对选择器**(Android示例): ```java driver.findElement(AppiumBy.androidUIAutomator( "new UiScrollable(new UiSelector().scrollable(true))" + ".scrollIntoView(new UiSelector().text(\"目标文本\"))" )); ``` 3. **定期清理缓存**: ```bash appium server --clear-cache # 重置会话缓存 ``` --- ### **七、扩展应用场景** 1. **混合应用调试**:通过 **Contexts** 标签切换WEBVIEW/NATIVE上下文 2. **跨平台对比**:同时连接Android/iOS设备对比UI布局 3. **性能测试辅助**:结合 **Appium Desktop Console** 监控内存/CPU使用率 通过掌握这些方法,您可以高效利用Appium Inspector完成元素定位、脚本调试自动化测试开发。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值