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

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的部分方法,作者先坦白,作者还没有这个能力,作者正在往这方面努力。作者希望可以通过自己的博客结交更多志同道合的朋友,一起为中国的自动化测试道路添砖加瓦,哈哈哈,不要拍我~不要拍脸~

原文地址:https://www.cnblogs.com/yunman/p/6656373.html