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的部分方法,作者先坦白,作者還沒有這個能力,作者正在往這方面努力。作者希望可以通過自己的博客結交更多志同道合的朋友,一起為中國的自動化測試道路添磚加瓦,哈哈哈,不要拍我~不要拍臉~
