我想大家在玩自动化的时候最关心的一定是如何定位元素,因为元素定位不到后面的什么方法都实现不了。
那我们就直接看看APPIUMLIBRARY官方给出的信息:
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属性分别对应的为【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 |
就是一层一层的写下去,用到的属性主要是【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. |
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 |
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 |
恩~作者也不吐槽了,毕竟APPIUMLIBRARY出来的时间也不长,还是去社区提交BUG吧,还有就是作者个人感觉,同学们除了用APPIUMLIBRARY之外,也可以尝试自己封装自己的LIBRARY,或者重写APPIUMLIBRARY的部分方法,作者先坦白,作者还没有这个能力,作者正在往这方面努力。作者希望可以通过自己的博客结交更多志同道合的朋友,一起为中国的自动化测试道路添砖加瓦,哈哈哈,不要拍我~不要拍脸~