Appium 自动化测试详解元素定位方式_appium 根据class定位元素driver.find_element(appiumby.cla-程序员宅基地

技术标签: python  Python自动化  软件测试  Appium  自动化  

简介

在做UI自动化的时候,有很大一部分精力是在进行定位元素操作,元素定位如果不准确,直接影响自动化的成败和效率

环境

  • Appium server :v1.20.2
  • Appium-Python-Client :2.1.2
  • selenium 4.1.0

常用的元素定位方式

  • id定位元素
  • class_name定位元素
  • content-desc定位元素
  • name 定位元素 (appium1.5及之后的版本废弃了name属性)
  • xpath定位元素
  • uiautomator定位元素,Android独有
id 定位
  • 使用方法
    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")
    
class_name定位
  • 使用方法
    find_element(AppiumBy.CLASS_NAME, “”)
  • 此方法一般不使用,由于界面元素的class重复度太高,基本上不存在唯一值
  • 元素的class属性
    在这里插入图片描述
  • 示例:
    driver.find_element(AppiumBy.CLASS_NAME, "android.widget.LinearLayout")
    
content-desc定位
  • 使用方法
    find_element(AppiumBy.ACCESSIBILITY_ID, “”)
  • 元素的content-desc属性
    content-desc属性是用来描述该元素的作用,一般都是为空
    在这里插入图片描述
  • 示例
    find_element(AppiumBy.ACCESSIBILITY_ID, "")
    
name定位
  • 此方法在Appium1.5及之后的版本废弃了name属性
xpath 定位
单个元素定位
  • 根据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属性又不唯一,这时候一般方法就没办法进行定位了

  • 父亲定位儿子
    ‘//[@resoure-id=“com.netease.edu.study:id/composite_item_list_recyclerview”]/android.widget.TextView’
    '//
    [@resoure-id=“com.netease.edu.study:id/composite_item_list_recyclerview”]/com.netease.edu.study:id/item_kingkong_area_image[2]’
  • 儿子定位父亲
    ‘//[@resource-id=“com.netease.edu.study:id/composite_item_list_recyclerview”]/…’
    '//
    [@resource-id=“com.netease.edu.study:id/composite_item_list_recyclerview”]/parent::
    '//
    [@resource-id=“com.netease.edu.study:id/composite_item_list_recyclerview”]/parent::android.widget.LinearLayout’
  • 兄弟元素定位
    ‘//*[@resource-id=“com.netease.edu.study:id/composite_item_list_recyclerview”]/…/android.widget.RelativeLayout’
uiautomator定位
说明

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\")")

resourceId
1.通过resourceId定位
new UiSelector().resourceId("id")

2.通过resourceIdMatches正则表达式
new UiSelector().resourceIdMatches("正则表达式")
className
1.通过className定位
new UiSelector().className("className")

2.通过classNameMatches正则表达式
new UiSelector().classNameMatches("正则表达式")
text
1.通过text文本定位语法
new UiSelector().text("text文本")

2.文本比较长或者可变,可以用textContains模糊匹配,只要文本包含匹配内容。
new UiSelector().textContains("包含text文本")

3.textStartsWith是以某个文本开头的匹配
new UiSelector().textStartsWith("以text文本开头")

4.正则匹配textMatches,这个需要配合正则表达式。
new UiSelector().textMatches("正则表达式")
content-des
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文本"))

以上为内容纯属个人理解,如有不足,欢迎各位大神指正!

如果觉得文章不错,欢迎关注微信公众号,微信公众号定期推送相关测试技术文章
个人微信号

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u010454117/article/details/122673186

智能推荐

一新概念3单词积累11-30-程序员宅基地

文章浏览阅读187次。在这里插入图片描述。

CMake静态库动态库的构建和链接之工程实用篇_cmakelist.txt中添加相应的静态库或者动态库 ubuntu-程序员宅基地

文章浏览阅读6k次,点赞4次,收藏23次。在这里不再赘述。Windows平台下可用cmake-gui生成vs的.sln工程,Linux平台下可以运行cmake命令。_cmakelist.txt中添加相应的静态库或者动态库 ubuntu

用C语言递归实现火车调度算法详解_列车调度c语言-程序员宅基地

文章浏览阅读6.5k次,点赞38次,收藏133次。用C语言递归算法实现火车调度想法详解_列车调度c语言

hadoop2.7.2开启kerberos认证_hadoop 认证开关-程序员宅基地

文章浏览阅读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 认证开关

cpplint 集成到visual studio中_cpplint vs-程序员宅基地

文章浏览阅读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

a simple cmdline implementation method--structure define_c# cmdline structure-程序员宅基地

文章浏览阅读223次。structure define:the major structure consist of cmdline_param_t and cmdline_instance_t.//parameter of cmdlinetypedef struct{ /_c# cmdline structure

随便推点

PuTTY 中文教程 1-程序员宅基地

文章浏览阅读215次。PuTTY 中文教程PuTTY 中文教程更新记录版权声明序言一些基本知识简介安装第一印象,开始登录一台远程主机首次登录一台主机时又看到了中文乱码怎么还是乱码?在 PuTTY 里面怎样选中,复制和粘贴?实时保存会话关于注销登录的一些事情窗口保存..._putty return to normal shell environment

Linux离线安装mysql详细教程-程序员宅基地

文章浏览阅读3.5k次,点赞3次,收藏19次。【代码】Linux离线安装mysql详细教程。_linux离线安装mysql

Bootstrap-fileinput 插件的使用_fileinput插件-程序员宅基地

文章浏览阅读783次。upload.single的参数 file-data 要与前端from表单中input输入框的name属性的值保持一致,否则报错 MulterError: Unexpected field。在router目录下创建 upload.js。在项目根目录下创建 app.js。_fileinput插件

gdb学习18:查看某个函数的汇编代码_gdb 汇编 可读-程序员宅基地

文章浏览阅读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 汇编 可读

JAVA的Map集合 File的文件_从map中取出multipartfile-程序员宅基地

文章浏览阅读316次。增添加键值对的数据到Map集合中extends V> V):将一个map集合存放到另一个map集合中删通过键 删除键值对的数据,返回的是值改当键值存在的时候,会将value值覆盖掉的查查看集合中元素的个数判断是否为空,如果不为空返回的是false是否包含这个键是否包含这个值重要的方法通过键 获取值获取map集合中的键 ,然后然或存到set集合中获取map集合中的值,存到了Collection集合中将map集合的键值对,存到了set集合。..._从map中取出multipartfile

ext4文件系统布局设计_ext4 extent tree-程序员宅基地

文章浏览阅读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

推荐文章

热门文章

相关标签