ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Appium定位方式的总结

2021-07-17 21:35:38  阅读:226  来源: 互联网

标签:总结 定位 Appium UiSelector uiautomator driver new android find


通过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
# 介绍几个简单常用的,text、className、resource-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-id、resourceIdMatches 类似我们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(".*历$")')

#组合定位
self.driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.xueqiu.android:id/tab_name").text("我的")').click()

#父子关系定位
self.driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.xueqiu.android:id/title_container").childSelector(text("股票"))')

#兄弟关系定位
self.driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.xueqiu.android:id/title_container").fromParent(text("股票"))')

#滚动查找
self.driver.find_element_by_android_uiautomator('new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text("查找的元素文本").instance(0));')

复制代码

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

转载至https://www.cnblogs.com/feng0815/p/8481679.html

标签:总结,定位,Appium,UiSelector,uiautomator,driver,new,android,find
来源: https://www.cnblogs.com/R-bear/p/15024917.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有