How To Find Android UI Element Location with Appium Locator
想要順利找到App上每個元件的位置嗎?
除了要有一顆堅毅的心、健康的肝
你還需要知道如何用有效的方法(Locator)找到Android App上的每個UI 元件



Appium 1.5以後的版本不支援 ,
原因是Name 很容易因為語言而改變

使用ClassName的方式通常 會找到不只一個元件
我們會用find_elements來存成List做處理











靈活的運用以上這些方法, 並掌握原則
你就能快速地找出每個UI元件的路徑, 開發自動化腳本
除了要有一顆堅毅的心、健康的肝
你還需要知道如何用有效的方法(Locator)找到Android App上的每個UI 元件
選擇Locator的原則
掌握這些要領, 你的Locator就會很強壯(importal)!!- 在當前操作頁面上是唯一的(unique), 不可重複
- 少用容易變動的
- 減少搜尋整個頁面的動作
輔助工具
UI Automator viewer能在茫茫大海中為我們指引出方向/Users/username_Name/Library/Android/sdk/tools/bin/uiautomatorviewer

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")

By Accessibility ID
driver.find_element_by_accessibility_id("plus").click()

By Name
Appium 1.5以後的版本不支援 ,原因是Name 很容易因為語言而改變
driver.find_element_by_name("9")

By ClassName
使用ClassName的方式通常 會找到不只一個元件我們會用find_elements來存成List做處理
driver.find_elements_by_class_name("android.widget.Button")

等一下, 你是不是忘了還有一個...
我當然記得Xpath, 因為它很重要我單獨拉出來講
Xpath的各種組合方式
By XPath using class and text
driver.find_element_by_xpath("//android.widget.Button[@text='del']")

By XPath using class and resource-id
driver.find_element_by_xpath("//android.widget.Button[contains(@resource-id,'digit_5')]")

By XPath using class, text and resource-id
driver.find_element_by_xpath("//android.widget.Button[contains(@resource-id,'digit_5') and @text='5']")

By XPath using class, text and resource-id
driver.find_element_by_xpath("//android.widget.Button[contains(@resource-id,'digit_5') and @text='5']")

By XPath using class, text and index
driver.find_element_by_xpath("//android.widget.Button[@text='5' and @index='4']")

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']")

By XPath using content-desc
driver.find_element_by_xpath("//android.widget.Button[@content-desc='delete']")

By XPath using class name
driver.find_elements_by_xpath("//android.widget.EditText")

By XPath using class and text
driver.find_element_by_xpath("//android.widget.Button[@text='÷']")

By XPath using class name by level
driver.find_element_by_xpath("//android.widget.LinearLayout/android.view.ViewGroup/android.widget.Button[5]")

靈活的運用以上這些方法, 並掌握原則
你就能快速地找出每個UI元件的路徑, 開發自動化腳本
Comments
Post a Comment