http://blog.csdn.net/deadgrape/article/details/50628113

我想大家在玩自动化的时候最关心的一定是如何定位元素,因为元素定位不到后面的什么方法都实现不了。

那我们就直接看看APPIUMLIBRARY官方给出的信息:

Introduction

AppiumLibrary is a App testing library for Robot Framework.

Locating elements

All keywords in AppiumLibrary that need to find an element on the app take an argument, locator. By default, when a locator value is provided, it is matched against the key attributes of the particular element type. For example, id and name are key attributes to all elements, and locating elements is easy using just the id as a locator. For example:

Click Element my_element

Appium additionally supports some of the Mobile JSON Wire Protocol locator strategies It is also possible to specify the approach AppiumLibrary should take to find an element by specifying a lookup strategy with a locator prefix. Supported strategies are:

Strategy Example Description
identifier Click Element | identifier=my_element Matches by @id or @name attribute
id Click Element | id=my_element Matches by @id attribute
name Click Element | name=my_element Matches by @name attribute
xpath Click Element | xpath=//UIATableView/UIATableCell/UIAButton Matches with arbitrary XPath
class Click Element | class=UIAPickerWheel Matches by class
accessibility_id Click Element | accessibility_id=t Accessibility options utilize.
android Click Element | android=new UiSelector().description('Apps') Matches by Android UI Automator
ios Click Element | ios=.buttons().withName('Apps') Matches by iOS UI Automation
css Click Element | css=.green_button Matches by css in webview

这里的方法有很多,接下来作者来给大家说说每个方法分别对应Android界面控件的什么属性,大致怎么用的。

1. identifier

identifier Click Element | identifier=my_element Matches by @id or @name attribute

通过描述,我们可见对应的属性为ID 或者NAME,如果有过ANDROID开发经验的同学们,一定会奇怪ANDROID控件中有ID属性,但是没有name属性,在这里作者告诉大家一个SDK的工具,在TOOLS目录中【uiautomatorviewer.bat】,双击可以打开,这是一个类似QTP中SPY的工具,可以用来抓控件,如图所示:

更具这个工具,我来告诉大家,文档中的ID和NAME属性分别对应的为【resource-id】和【text】

2.id&name

id Click Element | id=my_element Matches by @id attribute
name Click Element | name=my_element Matches by @name attribute

就不多说了对应1就可以了

3.XPATH

xpath Click Element | xpath=//UIATableView/UIATableCell/UIAButton Matches with arbitrary XPath

这是一个较为新出寻址方式,用法就像文件路径,一般使用方法为:“//android.widget.LinearLayout/android.widget.LinearLayout/android.widget.TextView”

就是一层一层的写下去,用到的属性主要是【class】,当然XPATH不会这么呆板,它还有其他的用法(通过特殊属性、序列等等定位的),在这里作者也就不过多啰嗦了,有兴趣的同学可以去W3C上看看,在这里作者无私奉献给出网址

http://www.w3school.com.cn/xpath/index.asp

4.class,对应属性就是【class】

class Click Element | class=UIAPickerWheel Matches by class

5.accessibility_id,对应属性为【content-desc】

accessibility_id Click Element | accessibility_id=t Accessibility options utilize.

6.android,这是调用UIAUTOMATOR的方法来实现元素定位,new UiSelector().之后可以接很多方法,不光光是description,还有TEXT,NAME,DESCRIPTIONCONTAINS等等,具体的内容可以用【UiSelector】在百度中搜索,很多这方面的知识。

android Click Element | android=new UiSelector().description('Apps') Matches by Android UI Automator

7.IOS不用说大家都知道这个是用在苹果测试上的,好吧作者还未涉足苹果,就不卖乖了

ios Click Element | ios=.buttons().withName('Apps') Matches by iOS UI Automation

8.CSS这个方法,作者坦白没自己用过,见过开发玩过,用起来很方便,但是据说很危险很多人都不建议使用CSS,都建议使用XPATH,作者大部分用的也是XPATH,至于是不是也被蒙在鼓里,作者也不好说,同学们自己把握,说不定这个是个原子弹,大杀器。

css Click Element | css=.green_button Matches by css in webview

好啦~关于APPIUM里的定位也就都讲完了,下面趁作者过年期间有空给大家唠唠嗑

1.首先就说说定位的方法吧,作者已经把方法都试了遍了,发现identifier,accessibility_id,android这三个不怎么好用,主要还是用了ID,NAME,XPATH,

2.然后XPATH中如果用到【CONTENT-DESC】属性来定位话,有时会定位不到,如果这个属性的字符串里有【空格】,基本就残废了,这点是作者血淋淋的教训,

3.坐标点击函数,对图形化的控件效果较好,但是对于VIEW控件却支持不佳

Click A Point x=0, y=0

Click on a point

4.个人感觉目前这套自动化测试方案对安卓纯APP较为适宜,但是遇到混合测试则有点力不从心,就拿作者来说目前有个微信公众号的自动化测试项目,结果发现各种找不到元素,各种没有反应,各种方法无效。

恩~作者也不吐槽了,毕竟APPIUMLIBRARY出来的时间也不长,还是去社区提交BUG吧,还有就是作者个人感觉,同学们除了用APPIUMLIBRARY之外,也可以尝试自己封装自己的LIBRARY,或者重写APPIUMLIBRARY的部分方法,作者先坦白,作者还没有这个能力,作者正在往这方面努力。作者希望可以通过自己的博客结交更多志同道合的朋友,一起为中国的自动化测试道路添砖加瓦,哈哈哈,不要拍我~不要拍脸~

RobotFrameWork+APPIUM实现对安卓APK的自动化测试----第七篇【元素定位介绍】的更多相关文章

  1. RobotFrameWork+APPIUM实现对安卓APK的自动化测试----第五篇【AppiumLibrary校验函数介绍】

    http://blog.csdn.net/deadgrape/article/details/50619050 以上连作者先跪一下方便面,在上一篇中,作者遗漏了两个常用的函数: 1.长按 Long P ...

  2. RobotFrameWork+APPIUM实现对安卓APK的自动化测试----第三篇【实例】

    http://blog.csdn.net/deadgrape/article/details/50579565 在这一篇里我先让大家看一下RF+APPIUM这个框架的实际运行时什么样子的,给大家一个直 ...

  3. RobotFrameWork+APPIUM实现对安卓APK的自动化测试----第四篇【AppiumLibrary实用函数介绍】

    http://blog.csdn.net/deadgrape/article/details/50585677 通过前几篇的讲述,我相信大家已经对RF+Appium的框架已经有所了解了. 接下来我告诉 ...

  4. RobotFrameWork+APPIUM实现对安卓APK的自动化测试----第六篇【AppiumLibrary等待函数介绍】

    http://blog.csdn.net/deadgrape/article/details/50622441 废话不多说,少年们请看下面. Wait Until Page Contains text ...

  5. [转]RobotFrameWork+APPIUM实现对安卓APK的自动化测试----第一篇【安装】

    前言:关于RobotFrameWork+APPIUM实现对安卓APK的自动化测试的文章都是取自于乐于分享知识于网络的好心人们,所以我也希望我的知识可以分享给大家. 首先我们先罗列一下我们要安装的软件 ...

  6. RobotFrameWork+APPIUM实现对安卓APK的自动化测试----第一篇【安装】

    文章来源http://blog.csdn.net/deadgrape/article/details/50563119 前言:关于RobotFrameWork+APPIUM实现对安卓APK的自动化测试 ...

  7. RobotFrameWork+APPIUM实现对安卓APK的自动化测试----第二篇【原理】

    http://blog.csdn.net/deadgrape/article/details/50574459 接着上一篇,我们开始聊聊APPIUM的框架和运行模式.废话不多说直接上图. 1.首先自动 ...

  8. 【转】RobotFrameWork+APPIUM实现对安卓APK的自动化测试----第二篇【原理】

    接着上一篇,我们开始聊聊APPIUM的框架和运行模式.废话不多说直接上图. 1.首先自动化脚本通过RobotFrameWork将命令传递给Appium的客户端: 2.然后[Appium的客户端]将接受 ...

  9. 技术分享 | app自动化测试(Android)--元素定位方式与隐式等待

    原文链接 元素定位是 UI 自动化测试中最关键的一步,假如没有定位到元素,也就无法完成对页面的操作.那么在页面中如何定位到想要的元素,本小节讨论 Appium 元素定位方式. Appium的元素定位方 ...

随机推荐

  1. C语言播放声音最简单的两种方法

    1. 假设仅须要播放波形文件wav格式的声音,非常easy.仅仅需一句话: PlaySound(TEXT("Data\\1.wav"), NULL, SND_FILENAME | ...

  2. The Unique MST--hdoj

    The Unique MST Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other) Tota ...

  3. Windows Server 2012 / 2016 安装 .Net Framework 3.5(PowerShell)

    原文链接:https://www.muhanxue.com/essays/2017/04/3736598.html 问题描述 使用 Windows Server 2012 R2 或 Windows S ...

  4. docker(部署常见应用):docker部署redis

    上节回顾:docker(部署常见应用):docker部署mysql docker部署redis:4.0 # 下载镜像 docker pull redis:4.0 # 查看下载镜像 docker ima ...

  5. fastJson解析复杂对象时碰到的问题

    碰到map对象无法解析出来,发现问题是缺少有get/set方法,否则无法解析. 对象转json字符串 JSON.toJSONString(module) json字符串转对象(必须是pojo) mod ...

  6. cookie,session,viewstate

    viewstate的原理是隐藏域. protected void Page_Load(object sender, EventArgs e) { ViewState["v1"] = ...

  7. 文档控件NTKO OFFICE 详细使用说明之预览PDF文件(禁止打印、下载、另存为、防抓包下载)

    1.在线预览PDF文件(禁止打印.下载.复制.另存为) (1) 运行环境 ① 浏览器:支持IE7-IE11(平台版本还支持Chrome和Firefox) ② IE工具栏-Internet 选项:将ww ...

  8. Java基础6一面向对象

    面向对象的编程思想:是以事物的整体的为基本单位,从事物的属性和行为两个方面进行描述. 特点: Java来源于生活服务于生活 用面向对象的思想能够接近正常的思维方式. 面向对象语言中有设计模式一说. 在 ...

  9. javascript中的构造函数和原型及原型链

    纯属个人理解,有错误的地方希望大牛指出,以免误人子弟 1.构造函数: 构造函数的作用 : 初始化由new创建出来的对象    new 的作用: 创建对象(空对象) new 后面跟的是函数调用,使用ne ...

  10. Axis2 1.7.4构建项目

    1.下载axis2项目文件 http://axis.apache.org/axis2/java/core/download.html 2.Maven文件的pom.xml文件 3.将下载的axis2-1 ...