1. 程式人生 > >Appium UIAutomator定位簡介

Appium UIAutomator定位簡介

UIAutomator定位簡介 UIAutomator元素定位是 Android 系統原生支援的定位方式,雖然與 xpath 類似,但比它更加好用,且支援元素全部屬性定位.定位原理是通過android 自帶的android uiautomator的類庫去查詢元素。 Appium元素定位方法其實也是基於Uiautomator來進行封裝的。 使用方法 find_element_by_android_uiautomator() 可以運用UiAutomator元素定位。 定位方法 id定位 text定位 class name定位 id定位 id定位是根據元素的resource-id屬性來進行定位,使用 UiSelector().resourceId()方法即可。 by_Uiautomator_id.py

from find_element.capability import driver

driver.find_element_by_android_uiautomator\
    ('new UiSelector().resourceId("com.tal.kaoyan:id/login_email_edittext")').send_keys('coco123123')#使用者名稱
driver.find_element_by_android_uiautomator\
    ('new UiSelector().resourceId("com.tal.kaoyan:id/login_password_edittext")').send_keys('coco123123')#密碼
driver.find_element_by_android_uiautomator\
    ('new UiSelector().resourceId("com.tal.kaoyan:id/login_login_btn")').click()#登入

text定位 text定位就是根據元素的text屬性值來進行定位,new UiSelector()

driver.find_element_by_android_uiautomator\
    ('new UiSelector().text("請輸入使用者名稱")').send_keys('coco123123')

by_Uiautomator_text.py

from find_element.capability import driver

driver.find_element_by_android_uiautomator\
    ('new UiSelector().text("請輸入使用者名稱")').send_keys('coco123123')
driver.find_element_by_android_uiautomator\
    ('new UiSelector().resourceId("com.tal.kaoyan:id/login_password_edittext")').send_keys('coco123123')#密碼
driver.find_element_by_android_uiautomator\
    ('new UiSelector().resourceId("com.tal.kaoyan:id/login_login_btn")').click()#登入

class name定位 與Appium class定位方式一樣,也是根據元素的class屬性來進行定位。

driver.find_element_by_android_uiautomator\
    ('new UiSelector().className("android.widget.EditText")').send_keys('coco123123')

by_Uiautomator_classname.py

from find_element.capability import driver

driver.find_element_by_android_uiautomator\
    ('new UiSelector().className("android.widget.EditText")').send_keys('coco123123')
driver.find_element_by_android_uiautomator\
    ('new UiSelector().resourceId("com.tal.kaoyan:id/login_password_edittext")').send_keys('coco123123')#密碼
driver.find_element_by_android_uiautomator\
    ('new UiSelector().resourceId("com.tal.kaoyan:id/login_login_btn")').click()#登入

在這裡插入圖片描述