目录

  • 一.认识定位控件(元素定位工具)
  • 二.功能及按钮介绍
  • 三.Appuim支持的定位方式
    • 3.01 id定位
    • 3.02 name定位
    • 3.03 className定位
    • 3.04 classNameMatches定位
    • 3.05 textMatches 正则匹配查找
    • 3.06 textStartsWith定位
    • 3.07 class_name定位
    • 3.08父子关系、兄弟关系定位
    • 3.09 滚动查找
    • 3.10 Accessibility_id定位
    • 3.11 XPath定位
    • 3.12 contains模糊定位
    • 3.13 AndroidUiAutomator定位元素
    • 3.14 resourceID定位
    • 3.15 resourceIDMatches 定位
    • 3.16组合定位
    • 3.17 Android UIAutomator定位
    • 3.18 支持层级定位
    • 3.19 支持部分定位
    • 3.20 Android原生支持元素定位方式
    • 3.21Appium不支持的元素定位方式
    • 3.22 appium与selenium元素定位之比较
一.认识定位控件(元素定位工具)
  • 该工具位于android-sdk-windows\tools\uiautomatorviewer.bat双击即可启动,启动过程中组件所打开的CMD窗口不用关闭;手机开启USB调试,或打开模拟器即可使用
  • 在使用Appium的时候,需要填写Desired Capabilities,这里使用Android SDK自带的uiautomatorviewer.bat控件可以便捷的获取所需信息
二.功能及按钮介绍
  • 通过截屏并分析XML布局文件的方式,为用户提供控件信息查看服务

  • 获取节点元素相关属性

  • 获取点位坐标

  • The first button:Open:Screenshot;UI XML Dump打开本地截图和打开.uix格式文件(uix文件需安装Binary Data打开)

  • Second button:Device Screenshot(uiautomator dump)设备屏幕截图(uiautomator转储)

  • Third button:Device Screenshot with Compressed Hierarchy(uiautomator dump --compressed)具有压缩层次结构的设备屏幕截图

  • Save:保存至本地指定位置

  • 在Android Studio中可以通过Android Device Monitor 调用UI Automator Viewer

  • First button on the right:+Expand All-全部展开

  • Second button on the right:Toggle NAF Nodes-切换NAF节点,点击后展示不可被识别的控件

  • Third button on the right:Clear search results-清除搜索结果

三.Appuim支持的定位方式
3.01 id定位
  • 通过uiautomatorviewer.bat 工具可以查看对象的id属性;
  • 如果目标设备的API Level低于18则UIAutomatorViewer不能获得对应的Resource ID,只有等于大于18的时候才能使用;
  • resource-id 就是的id属性;
  • 使用方法(未验证):driver.find_element_by_id(‘android:id/text1’).click();driver.findElement(By.id(“com.android.calculator2:id/formula”))
3.02 name定位

一般text属性认为是name

3.03 className定位

通过调用android uiautomator使用className进行定位

ele = self.driver.find_element_by_android_uiautomator('new UiSelector().className("android.widget.EditText")')
ele.send_keys('132')
3.04 classNameMatches定位

通过className正则匹配进行定位

ele = self.driver.find_element_by_android_uiautomator('new UiSelector().classNameMatches (".*EditText")')
ele.send_keys('132')
3.05 textMatches 正则匹配查找

textMatches故名思义就是通过正则的来进行查找定位,他也是通过text的属性来进行正则匹配

ele = self.driver.find_element_by_android_uiautomator('new UiSelector().textMatches("^请输入手.*")')
ele.send_keys("123")
3.06 textStartsWith定位
driver.find_element_by_android_uiautomator('new UiSelector().textStartsWith("请输入")')
3.07 class_name定位
  • 定位一个控件class就是class_name ,一般获得的view都不止一个,所以应该需要遍历一遍得到的views,然后缩小搜索条件来获得目标控件;如果多个只操作第一个driver.find_element_by_class_name("com.jingdong.app.mall:id/ic").click()
  • 定位组控件,例如:com.jingdong.app.mall:id/ic;
    driver.find_elements_by_class_name("com.jingdong.app.mall:id/ic")[1].click(),或WebElement button = driver.findElement(By.className(“com.jingdong.app.mall:id/ic”));
3.08父子关系、兄弟关系定位
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("密码"))')
3.09 滚动查找
self.driver.find_element_by_android_uiautomator('new UiScrollable(new UiSelector().scrollable(true).instance(0)).scrollIntoView(new UiSelector().text("查找的元素文本").instance(0));')
3.10 Accessibility_id定位

参数取content-desc的值:driver.find_element_by_accessibility_id('Accessibility').click()findElement(By.AccessibilityId("sharebutton"))

3.11 XPath定位
  • Appium对于xpath定位执行效率是比较低的,也就是说遇到xpath的定位代码的时候,执行比较慢;迫不得已的情况下尽量不用这个定位方式
    (参数取text的值)driver.find_element_by_xpath('//*[@text="Accessibility"]').click()
    (参数取resource-id的值)driver.find_element_by_xpath('//*[@resource-id="android:id/text1"]').click()
    参数格式:
    1)//class
    2)//class[n]
    3)//class[@属性=“属性值”]
    4)//class[@属性=“属性值” and @属性=“属性值”]
    5)//*[@属性=“属性值”]
    例如:com.jingdong.app.mall:id/ic
    driver.findElement(By.xpath("//com.jingdong.app.mall:id/ic"))
    driver.find_element_by_xpath('//*[@resource-id="android:id/text1"]').click()
agree_continue_xpath = "//*[@text='退出登录']"WebDriverWait(driver, 10, 1).until(EC.visibility_of_element_located((MobileBy.XPATH, agree_continue_xpath)))
driver.find_element_by_xpath(agree_continue_xpath).click()
3.12 contains模糊定位

用于获取toast时,toast文本内容较长,可采用contains包含部分文本的匹配方式;以用来模糊匹配上面的文本属性“退出登录”

agree_continue_xpath = "//android.widget.Button[contains(@text, '退出')]"WebDriverWait(driver, 10, 1).until(EC.visibility_of_element_located((MobileBy.XPATH, agree_continue_xpath)))
driver.find_element_by_xpath(agree_continue_xpath).click()
3.13 AndroidUiAutomator定位元素

AndroidUIAutomator是一个强有力的元素定位方式,它是通过Android UIAutomator类库去找元素,定位方式:findElement(By.AndroidUIAutomator(String UIAuto));
可以选择id,nameclassName,description作为传入的字符串

WebElement el =driver.findElementByAndroidUIAutomator("new UiSelector().resourceId(\"com.tencent.mm:id/do\")");
3.14 resourceID定位

resourceld定位和appium封装好的id定位一样,差别在写法变成了uiautomator的写法

ele = self.driver.find_element_by_android_uiautomator('new UiSelector().resourceId("cn.com.open.mooc:id/et_phone_edit")')
ele.send_keys('234')
3.15 resourceIDMatches 定位

通过id进行正则匹配定位

ele = self.driver.find_element_by_android_uiautomator('new UiSelector().resourceIdMatches(".+et_phone_edit")')
ele.send_keys('123')
3.16组合定位

xpath中同时包含class和text两个属性

agree_continue_xpath = "//*[@class='android.widget.TextView' and @text='退出登录']"WebDriverWait(driver, 10, 1).until(EC.visibility_of_element_located((MobileBy.XPATH, agree_continue_xpath)))
driver.find_element_by_xpath(agree_continue_xpath).click()
3.17 Android UIAutomator定位

android uiautomator原理是通过android 自带的android uiautomator的类库去查找元素,其实和appium的定位一样,或者说他比appium的定位方式更佳多以及更佳适用,它也支持id、className、text、模糊匹配等进行定位

3.18 支持层级定位

1.driver.find_element_by_xpath("//android.widget.ListView/android.widget.TextView").click()
2.driver.find_element_by_xpath("//android.widget.ListView/*[1]").click()
3.driver.find_element_by_xpath('//*[@resource-id="android:id/list"]/*[1]').click()在父元素API Demos下,Accessibility是第1个子元素;xpath的索引从1开始
4.driver.find_element_by_xpath('//*[@resource-id="android:id/list"]/*[@resource-id="android:id/text1"]').click()
5.driver.find_element_by_xpath('//android.widget.TextView[contains(@text,"Acce")]').click()

  • 参数格式://class[@属性=“属性值”]/class[]/class
3.19 支持部分定位

driver.find_element_by_xpath('//android.widget.TextView[contains(@text,"Acce")]').click()

3.20 Android原生支持元素定位方式

①(参数取resource-id的值)driver.find_element_by_android_uiautomator('new UiSelector().resourceId("android:id/text1")').click()
简化(默认是UiSelector对象):driver.find_element_by_android_uiautomator('.resourceId("android:id/text1")').click()
例如:WebElement element = driver.findElement(By.id("com.tencent.mm:id/do"));
driver.findElementById("com.tencent.mm:id/do")
②(参数取class的值)driver.find_element_by_android_uiautomator('.className("android.widget.TextView")').click()
③(参数取text的值)driver.find_element_by_android_uiautomator('.text("Accessibility")').click()

3.21Appium不支持的元素定位方式

3.22 appium与selenium元素定位之比较

appium是app端,用appium需导出selenium包
selenium是web端

frame Element positioning method remarks
appium id, className, AccessibilityId, xpath, AndroidUIAutomator for H5,Same support selenium name,link,text,css
selenium id, className, name, tag name, link text, paratial link text, xpath ,css

自动化测试-uiautomatorviewer.bat相关推荐

  1. Appinum:在使用安卓模拟器定位页面元素时启动uiautomatorviewer.bat ,但是报错: Error while obtaining UI hierarchy XML file: c

    在使用安卓模拟器定位页面元素时启动uiautomatorviewer.bat ,但是报错: Error while obtaining UI hierarchy XML file: com.andro ...

  2. android uiautomator 截屏,安卓系统手机用uiautomatorviewer.bat截图失败或者用adb截的图片打不开...

    安卓系统手机用uiautomatorviewer.bat截图失败或者用adb截的图片打不开 2020年07月23日 | 萬仟网IT编程 | 我要评论 很多没有root过的手机经常在使用uiautoma ...

  3. uiautomatorviewer.bat闪退无法使用问题解决

    androidSDK下uiautomatorviewer.bat闪退无法使用问题解决 1.jdk版本是否过高,建议jdk1.8 2.java环境变量配置,删除JAVA_HOME 3.uiautomat ...

  4. 如何使用安卓无障碍服务之uiautomatorviewer.bat(UI Automator Viewer)层级查看器

    首先安装Android studio,我使用的android版本是 android-studio-2022.1.1.21-windows.zip No .exe installer 下载地址为 Dow ...

  5. Android自动化测试应用:uiautomatorviewer工具的安装与使用

    准备工作一.jdk 1.8 (请勿使用JDK 1.9 将造成兼容性错误,导致)链接:https://pan.baidu.com/s/1dUspTs 密码:o6c9 二.Android-SDK 3.0. ...

  6. 5 个 APP 自动化测试辅助定位工具,你用过几个?

    UI 自动化测试时,先要定位到需要操作的元素,然后才能执行指令. 在网页端可以非常方便的通过 devtools 工具(也就是经常说的 F12) 获取和编写元素定位表达式. 但是在移动端自动化时,这种辅 ...

  7. python+appium判断元素存在_python自动化测试应用--Appium元素篇

    1.1概要 本篇将对和界面元素相关的内容做讲解,比如,查找元素的几种常见方法,还将介绍如何查看app界面上的元素去做定位,最后还将新手经常遇到的问题做个简单的回答. 1.2 查找app界面元素的工具 ...

  8. 腾讯Android自动化测试实战3.1.4 Robotium的控件获取、操作及断言

    3.1.4 Robotium的控件获取.操作及断言 Robotium是一款在Android客户端中的自动化测试框架,它需要模拟用户操作手机屏幕.要完成对手机的模拟操作,应该包含以下几个基本操作: (1 ...

  9. 安卓自动化测试(一)

    安卓自动化测试(一) 环境配置按照网上教程一步步来,版本等最好不要错,因为软件版本之间有时差别很大.. 查看包名和appActivity的方法: 1,包名可以用91等安装时看到,appactivety ...

  10. pythonandroid自动化测试仪器_使用Python进行Android自动化测试

    下面我们开始第一个简单的Android UI自动化测试 1.使用adb命令连接真机或模拟器 2.打开uiautomatorviewer工具 3.使用uiautomatorviewer工具获取应用的元素 ...

最新文章

  1. iOS开发 关于启动页和停留时间的设置
  2. [设计模式]设计模式之禅关于迪米特法则
  3. 清华博士教你如何用推荐算法技术「找到女朋友」
  4. 离散数学关系的基本运算和关系的性质闭包
  5. Supervisor使用教程
  6. linux命令-- pstack命令(跟踪进程栈)
  7. MyBatis缓存分为一级缓存和二级缓存
  8. springboot util 测试类怎么写_SpringBoot入门建站全系列(九)文件上传功能与下载方式...
  9. python中序列类型和数组之间的区别_「Python」序列构成的数组
  10. java整蛊小游戏源码_Java 开发打飞机小游戏(附完整源码)
  11. Pandas 基本文本数据处理
  12. php 写一个大富翁游戏,抽奖系列:如何用纯js做一个大富翁游戏
  13. 大学物理2-2笔记(5)麦克斯韦电磁场理论
  14. 米思齐(Mixly)图形化系列教程(一)-Mixly软件安装及界面功能介绍
  15. 企业不可忽略的问题——员工移动设备管理
  16. Java SSL实现使用详解
  17. 优秀程序员的博客有哪些?
  18. 计算机网络实验一、验证性实验
  19. 火灾隐患是查不完的,消防监管要着力于提升单位消防能力
  20. The following packages have unmet dependencies: libx11-dev : Depends: libx11-6 (= 2:1.6.7-1+deb10u2

热门文章

  1. 金仓数据库KingbaseES blob类型数据导入导出
  2. 学英语尽量不要从背词汇表开始
  3. uniapp点击打开外部应用跳转链接,指定App打开应用市场
  4. ORA-20011: Approximate NDV failed: ORA-29913: error in executing ODCIEXTTABLEOPEN callout
  5. Web功能测试(邮箱,手机号,验证码,身份证号测试用例)
  6. 高中英语语法(001)-虚拟语气
  7. 解决git 提交报fatal: unable to access ' ': The requested URL returned error: 403 错(亲测可行)
  8. 小区人脸识别门禁系统解决方案
  9. 小龙 Dev-C++ 5.16 发布了
  10. 计算机维护费入什么会计科目,​系统维护费记入什么会计科目