Appium定位方式總結


通過appium-desktop定位元素

 

 

 

ClassName

Android

Android的class屬性對應ClassName定位方式,ClassName一般都是會重復的,可以通過index來獲取需要的元素。(從0開始查找dom樹中的同名class屬性)

iOS

iOS的type屬性對應CLassName定位方式,ClassName一般都是會重復的,可以通過index來獲取需要的元素。(從0開始查找dom樹中的同名class屬性)

 

ID

Android

Android的resource-id對應ID定位方式,這個id也可能存在重復情況,可以通過index來獲取需要的元素。(從0開始查找dom樹中的同名resource-id屬性)

使用appium-desktop來獲取元素時,如果提示有id的定位方式,則可以只接獲取,代表唯一。

 

XPATH

Android

Android的Xpath定位與PC的XPATH定位大同小異,可以通過相對路徑的定位方式定位,區別在於,這里相對路徑定位的//后只可以接Android的class屬性或*。(//android.widget.Button[@text="登 錄"])

iOS

iOS10 以上使用XCUITest框架后,原生框架不支持XPATH,Appium進行了轉換,速度很慢不建議使用。

 

AccessibilityId

Android

Android的content-desc屬性對應AccessibilityId定位方式,這個content-desc屬性專門為殘障人士設置,如果這個屬性不為空則推薦使用。

iOS

iOS的label和name屬性都對應AccessibilityId定位方式,如果有則推薦使用。

 

AndroidUIAutomator

Android的源生測試框架的定位方式,定位速度快。推薦使用牢記常用的幾種。

打開方法:

 

# 這個在運行時,調用的是Android自帶的UI框架UiAutomator的Api # 介紹幾個簡單常用的,text、className、resource-id # text # 匹配全部text文字
driver.find_element_by_android_uiautomator('new UiSelector().text("手機號")') # 包含text文字
driver.find_element_by_android_uiautomator('new UiSelector().textContains("機")') # 以text什么開始
driver.find_element_by_android_uiautomator('new UiSelector().textStartsWith("手")') # 正則匹配text
driver.find_element_by_android_uiautomator('new UiSelector().textMatches("^手.*")') # className
driver.find_elements_by_android_uiautomator('new UiSelector().className("android.widget.TextView")') # classNameMatches
driver.find_elements_by_android_uiautomator('new UiSelector().classNameMatches("^android.widget.*")') # resource-id、resourceIdMatches 類似我們html id 這個可能重復,
 driver.find_element_by_android_uiautomator('new UiSelector().resourceId("com.syqy.wecash:id/et_content")') # description driver.find_element_by_android_uiautomator('new UiSelector().description("S 日歷")') # descriptionStartsWith driver.find_element_by_android_uiautomator('new UiSelector().descriptionStartsWith("日歷")') # descriptionMatches driver.find_element_by_android_uiautomator('new UiSelector().descriptionMatches(".*歷$")'

iOSPredicateString

僅支持iOS10以上,可以多個屬性同時定位,推薦。(替代XPATH)

driver.find_elements_by_ios_predicate("label == '登錄'") driver.find_elements_by_ios_predicate("type='XCUIElementTypeOther' and name='聯系人,標簽, 第2個按鈕,共3個'")

iOSUIAutomation

iOS9.3以下使用,現在已經廢棄,iOSPredicateString代替了iOSUIAutomation


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM