How To Find Android UI Element Location with Appium Locator
想要順利找到App上每個元件的位置嗎?
除了要有一顆堅毅的心、健康的肝
你還需要知道如何用有效的方法(Locator)找到Android App上的每個UI 元件
![image](https://user-images.githubusercontent.com/29251855/38122803-f60b32e0-3409-11e8-9254-eff36b423094.png)
![image](https://user-images.githubusercontent.com/29251855/38122864-4cf89cd2-340a-11e8-8d81-a1694984d321.png)
![image](https://user-images.githubusercontent.com/29251855/38122914-ac0fd7bc-340a-11e8-8bda-450107931b99.png)
Appium 1.5以後的版本不支援 ,
原因是Name 很容易因為語言而改變
![image](https://user-images.githubusercontent.com/29251855/38122949-f8106ef6-340a-11e8-8173-73379eb2303a.png)
使用ClassName的方式通常 會找到不只一個元件
我們會用find_elements來存成List做處理
![image](https://user-images.githubusercontent.com/29251855/38122995-438be752-340b-11e8-9d28-0b39fa48ff60.png)
![image](https://user-images.githubusercontent.com/29251855/38123075-b29bab96-340b-11e8-8c6e-f9013c2c9aff.png)
![image](https://user-images.githubusercontent.com/29251855/38123249-a7919cc8-340c-11e8-9a46-e5a14cc627e9.png)
![image](https://user-images.githubusercontent.com/29251855/38123300-f15552aa-340c-11e8-9525-24e68be9fc75.png)
![image](https://user-images.githubusercontent.com/29251855/38123332-1edcd036-340d-11e8-8c41-dccb24410aa4.png)
![image](https://user-images.githubusercontent.com/29251855/38123396-62a58768-340d-11e8-9d8f-83ae9844887f.png)
![image](https://user-images.githubusercontent.com/29251855/38123476-cb171bc2-340d-11e8-8537-b1402c2bd084.png)
![image](https://user-images.githubusercontent.com/29251855/38123654-d8ab9636-340e-11e8-967d-f4e50a1856ec.png)
![image](https://user-images.githubusercontent.com/29251855/38123759-6cd441e6-340f-11e8-9d40-8cb8dfbc3b8f.png)
![image](https://user-images.githubusercontent.com/29251855/38123807-b5d01456-340f-11e8-9bc5-5d69725e044b.png)
![image](https://user-images.githubusercontent.com/29251855/38123876-556e3a10-3410-11e8-9008-af803a11024c.png)
靈活的運用以上這些方法, 並掌握原則
你就能快速地找出每個UI元件的路徑, 開發自動化腳本
除了要有一顆堅毅的心、健康的肝
你還需要知道如何用有效的方法(Locator)找到Android App上的每個UI 元件
選擇Locator的原則
掌握這些要領, 你的Locator就會很強壯(importal)!!- 在當前操作頁面上是唯一的(unique), 不可重複
- 少用容易變動的
- 減少搜尋整個頁面的動作
輔助工具
UI Automator viewer能在茫茫大海中為我們指引出方向/Users/username_Name/Library/Android/sdk/tools/bin/uiautomatorviewer
![image](https://user-images.githubusercontent.com/29251855/38122803-f60b32e0-3409-11e8-9254-eff36b423094.png)
Appium 提供的Locator方法
- By ID
- By Accessibility ID
- By Name (Appium 1.5 以後不支援)
- By ClassName
- By Xpath
By ID
driver.find_element_by_id("com.android.calculator2:id/digit_8")
![image](https://user-images.githubusercontent.com/29251855/38122864-4cf89cd2-340a-11e8-8d81-a1694984d321.png)
By Accessibility ID
driver.find_element_by_accessibility_id("plus").click()
![image](https://user-images.githubusercontent.com/29251855/38122914-ac0fd7bc-340a-11e8-8bda-450107931b99.png)
By Name
Appium 1.5以後的版本不支援 ,原因是Name 很容易因為語言而改變
driver.find_element_by_name("9")
![image](https://user-images.githubusercontent.com/29251855/38122949-f8106ef6-340a-11e8-8173-73379eb2303a.png)
By ClassName
使用ClassName的方式通常 會找到不只一個元件我們會用find_elements來存成List做處理
driver.find_elements_by_class_name("android.widget.Button")
![image](https://user-images.githubusercontent.com/29251855/38122995-438be752-340b-11e8-9d28-0b39fa48ff60.png)
等一下, 你是不是忘了還有一個...
我當然記得Xpath, 因為它很重要我單獨拉出來講
Xpath的各種組合方式
By XPath using class and text
driver.find_element_by_xpath("//android.widget.Button[@text='del']")
![image](https://user-images.githubusercontent.com/29251855/38123075-b29bab96-340b-11e8-8c6e-f9013c2c9aff.png)
By XPath using class and resource-id
driver.find_element_by_xpath("//android.widget.Button[contains(@resource-id,'digit_5')]")
![image](https://user-images.githubusercontent.com/29251855/38123249-a7919cc8-340c-11e8-9a46-e5a14cc627e9.png)
By XPath using class, text and resource-id
driver.find_element_by_xpath("//android.widget.Button[contains(@resource-id,'digit_5') and @text='5']")
![image](https://user-images.githubusercontent.com/29251855/38123300-f15552aa-340c-11e8-9525-24e68be9fc75.png)
By XPath using class, text and resource-id
driver.find_element_by_xpath("//android.widget.Button[contains(@resource-id,'digit_5') and @text='5']")
![image](https://user-images.githubusercontent.com/29251855/38123332-1edcd036-340d-11e8-8c41-dccb24410aa4.png)
By XPath using class, text and index
driver.find_element_by_xpath("//android.widget.Button[@text='5' and @index='4']")
![image](https://user-images.githubusercontent.com/29251855/38123396-62a58768-340d-11e8-9d8f-83ae9844887f.png)
By XPath using parent and child class hierarchy
driver.find_element_by_xpath("//android.widget.LinearLayout[@index='0']/android.view.ViewGroup[@index='0']/android.widget.Button[@index='6']")
![image](https://user-images.githubusercontent.com/29251855/38123476-cb171bc2-340d-11e8-8537-b1402c2bd084.png)
By XPath using content-desc
driver.find_element_by_xpath("//android.widget.Button[@content-desc='delete']")
![image](https://user-images.githubusercontent.com/29251855/38123654-d8ab9636-340e-11e8-967d-f4e50a1856ec.png)
By XPath using class name
driver.find_elements_by_xpath("//android.widget.EditText")
![image](https://user-images.githubusercontent.com/29251855/38123759-6cd441e6-340f-11e8-9d40-8cb8dfbc3b8f.png)
By XPath using class and text
driver.find_element_by_xpath("//android.widget.Button[@text='÷']")
![image](https://user-images.githubusercontent.com/29251855/38123807-b5d01456-340f-11e8-9bc5-5d69725e044b.png)
By XPath using class name by level
driver.find_element_by_xpath("//android.widget.LinearLayout/android.view.ViewGroup/android.widget.Button[5]")
![image](https://user-images.githubusercontent.com/29251855/38123876-556e3a10-3410-11e8-9008-af803a11024c.png)
靈活的運用以上這些方法, 並掌握原則
你就能快速地找出每個UI元件的路徑, 開發自動化腳本
Comments
Post a Comment