通过 appium-desktop 定位元素
- ClassName
- Android
Android 的 class 属性对应 ClassName 定位方式, ClassName 一般都是会重复的, 可以通过 index 来获取需要的元素 (从 0 开始查找 dom 树中的同名 class 属性)
iOS
iOS 的 type 属性对应 CLassName 定位方式, ClassName 一般都是会重复的, 可以通过 index 来获取需要的元素 (从 0 开始查找 dom 树中的同名 class 属性)
ID
Android
Android 的 resource-id 对应 ID 定位方式, 这个 id 也可能存在重复情况, 可以通过 index 来获取需要的元素 (从 0 开始查找 dom 树中的同名 resource-id 属性)
使用 appium-desktop 来获取元素时, 如果提示有 id 的定位方式, 则可以只接获取, 代表唯一
XPATH
Android
Android 的 Xpath 定位与 PC 的 XPATH 定位大同小异, 可以通过相对路径的定位方式定位, 区别在于, 这里相对路径定位的 // 后只可以接 Android 的 class 属性或 *(//android.widget.Button[@text="登 录"])
iOS
iOS10 以上使用 XCUITest 框架后, 原生框架不支持 XPATH,Appium 进行了转换, 速度很慢不建议使用
- AccessibilityId
- Android
Android 的 content-desc 属性对应 AccessibilityId 定位方式, 这个 content-desc 属性专门为残障人士设置, 如果这个属性不为空则推荐使用
iOS
iOS 的 label 和 name 属性都对应 AccessibilityId 定位方式, 如果有则推荐使用
AndroidUIAutomator
Android 的源生测试框架的定位方式, 定位速度快推荐使用牢记常用的几种
打开方法:
- # 这个在运行时, 调用的是 Android 自带的 UI 框架 UiAutomator 的 Api
- # 介绍几个简单常用的, textclassNameresource-id
- # text
- # 匹配全部 text 文字
- driver.find_element_by_android_uiautomator(new UiSelector().text("手机号"))
- # 包含 text 文字
- driver.find_element_by_android_uiautomator(new UiSelector().textContains("机"))
- # 以 text 什么开始
- driver.find_element_by_android_uiautomator(new UiSelector().textStartsWith("手"))
- # 正则匹配 text
- driver.find_element_by_android_uiautomator(new UiSelector().textMatches("^ 手.*"))
- # className
- driver.find_elements_by_android_uiautomator(new UiSelector().className("android.widget.TextView"))
- # classNameMatches
- driver.find_elements_by_android_uiautomator(new UiSelector().classNameMatches("^android.widget.*"))
- # resource-idresourceIdMatches 类似我们 html id 这个可能重复,
- driver.find_element_by_android_uiautomator(new UiSelector().resourceId("com.syqy.wecash:id/et_content")) # description driver.find_element_by_android_uiautomator(new UiSelector().description("S 日历")) # descriptionStartsWith driver.find_element_by_android_uiautomator(new UiSelector().descriptionStartsWith("日历")) # descriptionMatches driver.find_element_by_android_uiautomator(new UiSelector().descriptionMatches(".* 历 $"))
- iOSPredicateString
仅支持 iOS10 以上, 可以多个属性同时定位, 推荐 (替代 XPATH)
- driver.find_elements_by_ios_predicate("label == 登录")
- driver.find_elements_by_ios_predicate("type=XCUIElementTypeOther and name = 联系人, 标签, 第 2 个按钮, 共 3 个")
- iOSUIAutomation
iOS9.3 以下使用, 现在已经废弃, iOSPredicateString 代替了 iOSUIAutomation
来源: http://www.bubuko.com/infodetail-2513831.html