一、介紹
AppiumLibrary 是 Robot Framework 的App測試庫。
它使用Appium 與Android 和 iOS應用程序進行通信,類似於Selenium WebDriver與Web瀏覽器的對話。
AppiumLibrary是繼承和引用appiumandroidlibrary,但重新實現使用appium 1.X技術,它完全支持Python 2.7,但對python 3.3+的支持仍然是實驗性的。
二、定位控件
AppiumLibrary需要在應用程序中查找元素的所有關鍵字都使用參數locator。
當提供locator值時,它將與特定元素類型的鍵屬性進行匹配。支持的定位器(locator)有:
定位器 |
格式 |
描述 |
identifier |
Click Element | identifier=my_element |
匹配 @id 或 @name 屬性 |
id |
Click Element | id=my_element |
匹配 @id 屬性 |
name |
Click Element | name=my_element |
匹配 @name 屬性 |
xpath |
Click Element | xpath=//UIATableView/UIATableCell/UIAButton |
匹配 XPath |
class |
Click Element | class=UIAPickerWheel |
匹配 class name |
accessibility_id |
Click Element | accessibility_id=t |
匹配 輔助選項 |
android |
Click Element | android=new UiSelector().description('Apps') |
匹配 Android UI Automator |
ios |
Click Element | ios=.buttons().withName('Apps') |
匹配 iOS UI Automation |
css |
Click Element | css=.green_button |
匹配 css in webview |
三、定位工具



四、定位元素
1. identifier 定位
說明:匹配 @id 或 @name 屬性,分別對應 resource-id 和 text 屬性。
案例:點擊計算器數字“ 9 ”,可以使用id定位,也可以使用name定位。
Click Element | identifier=com.android.calculator2:id/digit_9 |
Click Element | identifier=9 |
2. id 定位
說明:匹配 @id 屬性,對應 resource-id 屬性。
案例:點擊計算器輸入框,id可以指定也可以不指定,如果不指定策略,默認就是id定位。
Click Element | id=com.android.calculator2:id/formula |
Click Element | com.android.calculator2:id/formula |
3. name 定位
說明:匹配 @name 屬性,對應 text 屬性。
案例:點擊計算器數字“ 9 ”。
Click Element | name=9 |
4. xpath 定位
說明:匹配 Xpath ,這也是功能最強大的一種定位方式,不過通常用於web自動化。
案例:點擊計算器數字“ 7 ”。
Click Element | xpath=//android.widget.Button[contains(@text,'7')] |
附加Xpath定位表達式說明:
5. class 定位
說明:匹配 class name ,對應 class 屬性,但通常class屬性都不唯一。
案例:點擊計算器數字“ 6 ”。
Click Element | class=android.widget.Button |
6. accessibility_id 定位
說明:匹配 輔助選項,這個方法屬於Appium擴展的定位方法,主要就是content-desc屬性。
案例:點擊計算器加號“ + ”。
Click Element | accessibility_id=plus |
7. android 定位
說明:匹配 Android UI Automator。
案例:點擊計算器加號“ + ”。
Click Element | android=new UiSelector().description(\"plus\") |
8. css 定位
說明:匹配 css in webview,只適用於webview的html頁面,繼承自webdriver。
9. ios 定位
說明:匹配 iOS UI Automation,很顯然只適用於ios系統。
以上就是筆者對RF框架Appium引用控件定位總結,個人感受比較好用的定位器是id、name、xpath、accessibility_id,
其他幾個相對比較局限,用的不多,其中css、ios定位筆者還未使用過,讀者有興趣可以自己去發掘一下。