UI自动化元素定位

大纲

1
2
3
APPIUM元素定位原理
常见的元素定位方式
元素定位技巧/辅助工具

APPIUM元素定位原理

目前的UI自动化测试,使用Appium进行页面元素的定位和操作。如下图所示,AppiumServer和UiAutomator2的手机端进行通信后完成元素的操作。

alt text

底层实现过程
alt text

  • 首先,Appium通过调用findElement的方式进行元素定位。
  • 然后,调用Android提供UIDevice对象的findObject方法。
  • 最终,通过PartialMatch.accept完成元素的查找。

接下来我们看一下,这个PartialMatch.accept到底是如何完成元素定位的。通过对于源码的研究,我们发现元素的信息都是存储在一个叫做AccessibilityNodeInfo的对象里面。源码中使用大量node.getXXX方法中的信息,大家是否眼熟呢?这些信息其实就是我们日常自动化测试中可以获取UI元素的属性。从这我们知道了appium元素定位的工作过程;
alt_text

常见元素定位方式

1
2
3
4
5
6
id定位
xpath定位
classname定位
text定位
坐标定位
accessibility_id定位

1、id定位

id定位是最常用的定位方式,在选择ID时需要验证ID唯一,此方法仅可用于Android,ios不可用

id定位示例:driver.find_element_by_id("id属性值")

  • 查找ID方式:①为使用uiautomatorviewer②为使用Appium Inspector

alt_text
alt_text

  • ID查找性能

由上方的appium定位原理可知,查找页面元素位置,是在AccessibilityNodeInfo对象里查找的,这个对象维护的各属性值是字典,因此通过ID查询的性能是较快的

  • boomplay UI自动化框架的使用
    alt_text

2、xpath定位

xpath定位也是最常用的定位方式之一,xpath是根据页面层级来定位元素位置,此方法仅可用于Android和iOS

xpath定位示例:driver.find_element_by_xpath("xpath表达式")

  • 查找xpath方式:①为使用uiautomatorviewer②为使用Appium Inspector

alt_text
alt_text

  • xpath查找性能

由于xpath是根据元素位置来查找的,从头开始查找,因此xpath查找的性能一般较差,且不同写法查找范围不同,性能差异很大,一般通过id/name属性值来定位;参考xpath语法

  • boomplay UI自动化框架的使用
    alt_text

3、classname定位

classname是基于classname值来定位元素,由于classname一般不唯一,一般不使用此值定位,使用的话也是结合xpath来使用,ios和Android均支持

class_name定位示例:driver.find_element_by_class_name("class_name属性值")

  • 查找classname方式:①为使用uiautomatorviewer②为使用Appium Inspector

alt_text
alt_text

  • classname查找性能

classname是页面的classname属性值,用它来定位元素时,由于classname值一般不唯一,因此一般不直接用其定位,一般和xpath一起使用,将classname作为xpath的属性值,性能由于xpath和ID定位性能接近

  • boomplay UI自动化框架的使用
    alt_text

4、name定位

name定位是使用页面text值来进行定位的,Android从5.5开始不支持name定位,iOS支持name定位

name定位示例:driver.find_element_by_name("text值")

  • 查找name方式:ios使用Appium Inspector

alt_text

  • name查找性能

name查找性能较快,和id查找接近

  • boomplay UI自动化框架的使用
    不支持

5、accessibility_id定位

accessibility_id定位是使用页面content-desc属性值来进行定位的,android和ios均支持

accessibility_id定位示例:driver.find_element_by_accessibility_id("content-desc值")

  • 查找content-desc方式:①为使用uiautomatorviewer②为使用Appium Inspector

alt_text
alt_text

  • accessibility_id查找性能

accessibility_id查找性能较快,和id查找接近

  • boomplay UI自动化框架的使用
    alt_text

6、指定uiautomator工具定位

uiautomator定位方式是直接指定uiautomator工具来进行定位,可利用工具对象的属性来进行定位(支持ID/xpath/class_name等属性值来进行定位),仅支持Android,在Android环境下和driver.find_element_by_是完全一样的

accessibility_id定位示例:driver.find_element_by_android_uiautomator("UiSelector工具属性值来进行定位")

  • 查找属性值方式:①为使用uiautomatorviewer②为使用Appium Inspector

alt_text
alt_text

  • uiautomator工具查找性能

Android环境下根据定位的属性值来区分,性能和driver.find_element_by_是完全一样的

  • boomplay UI自动化框架的使用
    不支持,可扩展

7、css_selector定位

css属性定位,支持通过页面的各类css属性来进行定位元素,支持单个属性和多个属性值组合定位,仅支持web

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 1.用 标签名 定位查找
driver.find_element_by_css_selector("input")

# 2.用 id 属性定位查找
driver.find_element_by_css_selector("kw")

# 3.用 class 属性定位查找
driver.find_element_by_css_selector("s_ipt")

# 4.其他属性定位
driver.find_element_by_css_selector("[name="wd"]")

# 5.标签名及id属性值组合定位
driver.find_element_by_css_selector("input#kw")
  • 查找属性值方式:①为使用uiautomatorviewer②为使用Appium Inspector

alt_text
alt_text

  • uiautomator工具查找性能

性能根据定位的属性值来区分,性能和driver.find_element_by_是完全一样的

  • boomplay UI自动化框架的使用
    alt_text

8、单数定位和复数定位

单数定位示例:driver.find_element_by_("值")

复数定位示例:driver.find_elements_by_("值").get(6)

单数定位时定位到第一个元素即返回,返回单个对象;复数定位会定位到页面的所有对象,返回一个数组;现在支持id和xpath的单复数定位;复数定位主要用于某些位置无唯一元素的场景,可通过第几个来进行定位
alt_text

9、坐标定位

APPIUM提供tap方法支持通过元素坐标进行点击,提供了一个adb_tap方法给大家获取元素坐标

  • boomplay UI自动化框架的使用
    alt_text

10、图像识别

APPIUM从1.9.0版本开始支持识别图像来进行查找对象,暂未调研。后续补充

  • boomplay UI自动化框架的使用
    alt_text

元素定位工具和技巧

  • 辅助工具
    • Android:uiautomatorviewer/Appium Inspector
    • iOS:Appium Inspector

欢迎关注我的其它发布渠道