技术标签: python Python自动化 软件测试 Appium 自动化
在做UI自动化的时候,有很大一部分精力是在进行定位元素操作,元素定位如果不准确,直接影响自动化的成败和效率
使用方法
find_element(AppiumBy.ID, “”)
driver.find_element_by_id() 已过时
元素的resource-id
唯一标识该元素的值(id有时候并不唯一)
示例:一般优先使用此方式定位
driver.find_element(AppiumBy.ID, "com.netease.edu.study:id/actionbar_item_search")
driver.find_element(AppiumBy.CLASS_NAME, "android.widget.LinearLayout")
find_element(AppiumBy.ACCESSIBILITY_ID, "")
根据id属性定位
表达式://*[@resource-id=’id属性’]
根据text属性定位
表达式://*[@text=’text文本属性’]
根据class属性定位
表达式: //*[@class=’class属性’]
通过content-desc属性定位
表达式: //*[@content-desc=’desc的文本’]
示例
driver.find_element(AppiumBy.XPATH, "//*[@resource-id='com.netease.edu.study:id/look_more']").click()
driver.find_element(AppiumBy.XPATH, "//*[@text='查看更多']").click()
driver.find_element(AppiumBy.XPATH, "//*[@class='android.widget.TextView']").click()
driver.find_element(AppiumBy.XPATH, "//*[@content-desc='更多按钮']").click()
contains是模糊匹配的定位方法,对于一些元素的属性是可变的,但有一部分是固定的,这种就可以模糊匹配
//[contains(@text, ‘查看’)]
//[contains(@content-desc, ‘’)]
//[contains(@resource-id, ‘id属性’)]
//[contains(@clsss, ‘class属性’)]
当单个元素定位不唯一时,这时候我们就可以采用多个属性组合定位
如果元素有多个属性,通过xpath也可以同时匹配2个属性,text, resource-id,class ,index,content-desc这些属性都能任意组合定位
id_class = ‘//android.widget.TextView[@resource-id=“com.netease.edu.study:id/look_more”]’
desc_class = ‘//*[@text=“查看更多” and @index=“2”]’
有时候我们遇见元素属性除了class属性,其他属性都为空,且class属性又不唯一,这时候一般方法就没办法进行定位了
Appium就是封装android的uiautomator这个框架来的,所以uiautomator的一些定位方法正常使用即可
官网地址:https://developer.android.google.cn/reference/androidx/test/uiautomator/UiSelector
driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, "new UiSelector().resourceId(\"com.netease.edu.study:id/look_more\")")
可以将:new UiSelector()
省略
driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,"resourceId(\"com.netease.edu.study:id/look_more\")")
1.通过resourceId定位
new UiSelector().resourceId("id")
2.通过resourceIdMatches正则表达式
new UiSelector().resourceIdMatches("正则表达式")
1.通过className定位
new UiSelector().className("className")
2.通过classNameMatches正则表达式
new UiSelector().classNameMatches("正则表达式")
1.通过text文本定位语法
new UiSelector().text("text文本")
2.文本比较长或者可变,可以用textContains模糊匹配,只要文本包含匹配内容。
new UiSelector().textContains("包含text文本")
3.textStartsWith是以某个文本开头的匹配
new UiSelector().textStartsWith("以text文本开头")
4.正则匹配textMatches,这个需要配合正则表达式。
new UiSelector().textMatches("正则表达式")
1.通过content-des文本定位语法
new UiSelector().description("content-des属性")
2.文本比较长或者可变,可以用descriptionContains模糊匹配,只要文本包含匹配内容。
new UiSelector().descriptionContains("包含content-des属性文本")
3.descriptionStartsWith是以某个文本开头的匹配
new UiSelector().descriptionStartsWith("以content-des属性文本开头")
4.正则匹配descriptionMatches,这个需要配合正则表达式。
new UiSelector().descriptionMatches("正则表达式")
1.大部分都是使用用id,class,text这三个属性进行组合,其次description这个属性也可以一起两两组合
new UiSelector().resourceId("id").text("text文本")
2.父子定位childSelector
new UiSelector().resourceId("id").childSelector(text("text文本"))
3.兄弟定位fromParent
new UiSelector().resourceId("id").fromParent(text("text文本"))
以上为内容纯属个人理解,如有不足,欢迎各位大神指正!
如果觉得文章不错,欢迎关注微信公众号,微信公众号定期推送相关测试技术文章
文章浏览阅读187次。在这里插入图片描述。
文章浏览阅读6k次,点赞4次,收藏23次。在这里不再赘述。Windows平台下可用cmake-gui生成vs的.sln工程,Linux平台下可以运行cmake命令。_cmakelist.txt中添加相应的静态库或者动态库 ubuntu
文章浏览阅读6.5k次,点赞38次,收藏133次。用C语言递归算法实现火车调度想法详解_列车调度c语言
文章浏览阅读3.5k次,点赞5次,收藏20次。环境介绍:一共三台机器:hadoop11: 192.168.230.11 namenode 、kerberos clienthadoop12: 192.168.230.12 datanode 、kerberos clienthadoop13: 192.168.230.13 datanode 、kerberos server(KDC)保证安装kerberos 之前能正常开启hadoop集群(已安装集群)一、介绍安装kerberos服务器1、在hadoop13安装kerberos se_hadoop 认证开关
文章浏览阅读3.6k次。1. 建议安装python2, python3可能会有问题。cpplint.py integration cpplint.py integration makes it easy to check that a source file conforms to the style guide. To do this, just go to Tools > External Tools_cpplint vs
文章浏览阅读223次。structure define:the major structure consist of cmdline_param_t and cmdline_instance_t.//parameter of cmdlinetypedef struct{ /_c# cmdline structure
文章浏览阅读215次。PuTTY 中文教程PuTTY 中文教程更新记录版权声明序言一些基本知识简介安装第一印象,开始登录一台远程主机首次登录一台主机时又看到了中文乱码怎么还是乱码?在 PuTTY 里面怎样选中,复制和粘贴?实时保存会话关于注销登录的一些事情窗口保存..._putty return to normal shell environment
文章浏览阅读3.5k次,点赞3次,收藏19次。【代码】Linux离线安装mysql详细教程。_linux离线安装mysql
文章浏览阅读783次。upload.single的参数 file-data 要与前端from表单中input输入框的name属性的值保持一致,否则报错 MulterError: Unexpected field。在router目录下创建 upload.js。在项目根目录下创建 app.js。_fileinput插件
文章浏览阅读2.2k次,点赞2次,收藏2次。代码:void PrintNumbers(int n){ while(n--) { printf("number is :%d\n",n); }}gdb调试信息:(gdb) disassemble PrintNumbersDump of assembler code for function PrintNumbers: 0x0000555555554795 ..._gdb 汇编 可读
文章浏览阅读316次。增添加键值对的数据到Map集合中extends V> V):将一个map集合存放到另一个map集合中删通过键 删除键值对的数据,返回的是值改当键值存在的时候,会将value值覆盖掉的查查看集合中元素的个数判断是否为空,如果不为空返回的是false是否包含这个键是否包含这个值重要的方法通过键 获取值获取map集合中的键 ,然后然或存到set集合中获取map集合中的值,存到了Collection集合中将map集合的键值对,存到了set集合。..._从map中取出multipartfile
文章浏览阅读363次。1.硬盘整体布局-基本布局2.硬盘整体布局-开启flex_bg3.inode与文件数据分布关系3.1.只是用inode,不扩展extent tree时一个inode对应一个file,inode中的i_block字段记录file中的内容与硬盘中的地址对应关系。3.2扩展了extent tree后扩展之后,extent tree有了两个node:inode中的i_block,扩展的一个block此时i_block作为Internal node,扩展的bl..._ext4 extent tree