我想大家在玩自动化的时候最关心的一定是如何定位元素,因为元素定位不到后面的什么方法都实现不了。
那我们就直接看看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
【RF|RobotFrameWork+APPIUM实现对安卓APK的自动化测试----第七篇【元素定位介绍】】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属性分别对应的为【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 |
3.XPATH
xpath | Click Element | xpath=//UIATableView/UIATableCell/UIAButton | Matches with arbitrary XPath |
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 |