51Testing软件测试论坛

标题: Appium-同一个元素不同定位方式的区别 [打印本页]

作者: 小爸爸    时间: 2017-6-28 13:37
标题: Appium-同一个元素不同定位方式的区别
前言
使用过 Appium 的都知道,元素的定位方式有很多种,具体使用哪一种,主要看业务的需要和自己的使用爱好。
下面总结一下,Appium 到底有哪些定位方式,定位的元素以下面截图指定的元素为例子:

这里给 Macaca 的 inspector 查看器打个广告,的确很好用,有需要可到社区的 Macaca 版块自己查找啊!
Appium 的定位方式种类
我说的定位方式都是基于我自己亲测过,没使用或比较少用的就在这里不列举了,如有错误的地方,请多多包涵!常用的定位方式(仅限 Android 和 iOS 两种系统)有 className、id、xpath、AccessibilityId、AndroidUIAutomator、iOSNsPredicateString、iOSClassChain、IosUIAutomation等
className
使用元素的className属性定位,支持:Android 和 iOS,推荐使用。
MobileBy.className("XCUIElementTypeButton")
id
使用元素的Resource Id属性定位,支持:Android,仅支持 Android 4.3或以上,推荐使用。反正我没有在 iOS 用过,大家有正确使用过的例子,可以分享一下。
MobileBy.id("package.name:id/android")
xpath
支持:Android 和 iOS。但由于 iOS 10开始使用的 XCUITest 框架原声不支持,定位速度很慢,所以官方现在不推荐大家使用,也有其他替代的定位方式可使用。
1.使用绝对路径定位,如截图所显示的 xpath 路径
两种属性:MobileBy.xpath("//className[@label='更多信息'][@isVisible='1']")
部分属性(最强大):MobileBy.xpath("//className[contains(@label,'更多')]")
AccessibilityId
替代以前的name定位方式,推荐使用。
在 Android 上,主要使用元素的content-desc属性,如该属性为空,不能使用此定位方式。
在 iOS 上,主要使用元素的label或name(两个属性的值都一样)属性进行定位,如该属性为空,如该属性为空,也是不能使用该属性。
MobileBy.AccessibilityId("更多信息")
AndroidUIAutomator
仅支持 Android 4.2或以上,可支持元素的单个属性和多个属性定位,推荐使用。
一种属性:MobileBy.AndroidUIAutomator("new UiSelector().text(\"发送\")")
两种属性:MobileBy.AndroidUIAutomator("new UiSelector().text(\"发送\").clickable(true)")
元素的所有属性都可用做定位,功能非常强大,且速度很快。
iOSNsPredicateString
仅支持 iOS 10或以上,可支持元素的单个属性和多个属性定位,推荐使用。
一种属性:MobileBy.iOSNsPredicateString("type == 'XCUIElementTypeButton'")
两种属性:MobileBy.iOSNsPredicateString("type == 'XCUIElementTypeButton' AND label == '更多信息'")
具体 iOSNsPredicate语法结构可查看官方文档,或期待我下一个帖子。
iOSClassChain
仅支持 iOS 10或以上,这是 github 的 Mykola Mokhnach 大神开发,仅限在 WebDriverAgent 框架使用,用于替代 xpath 的,但使用一阵子后,感觉灵活性没有 xpath 和 iOSNsPredicate 好,应该还不完善吧。
MobileBy.iOSClassChain('XCUIElementTypeWindow[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeNavigationBar[1]/XCUIElementTypeOther[1]/XCUIElementTypeButton[2]')
IosUIAutomation
仅支持 iOS 19.3或以下,是 iOS 旧框架 UIAutomation 的定位方式,现在基本上很少使用
总结:
以上这个多定位方式,很少说全部用完。根据我的经验,推荐使用:Android:AndroidUIAutomator > className = id = AccessibilityId > xpath。iOS:iOSNsPredicateString > className = AccessibilityId
搜索

作者: 草帽路飞UU    时间: 2017-6-28 17:34
iOS目前主要用xpath和name,官方推荐用AccessibilityId,感觉跟name一样。目前主要问题的desktop在没有name或AccessibilityId情况下,不能提供xpath,app-inspector又各种任性不稳定,焦虑
作者: 八戒你干嘛    时间: 2017-6-28 17:35
整理的挺详细的,最后一段有笔误,ios 19.3。Mykola Mokhnach 对selendroid inspector做的二开,支持定位ios也提供xpath属性,目前用下来挺好的
作者: 悠悠小仙仙    时间: 2017-6-28 17:35
iOSNsPredicateString用法详解,期待楼主新的分享




欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2