原文地址http://blog.csdn.net/wuyepiaoxue789/article/details/77885136
元素屬性的介紹
type
:元素類型,與className
作用一致,如:XCUIElementTypeStaticText
value
: 一般不用name
:元素的文本內容,可用作 AccessibilityId
定位方式,如:測試420班級群
label
:絕大多數情況下,與 name 作用一致enabled
:元素是否可點擊,一般值為true
或者false
visible
:元素是夠可見,一般值為true
或者false
1.findElementByIosNsPredicate
這個定位方式需要用java-client -5.0.版本,4.x的版本沒有這個定位方式
.findElementByIosNsPredicate
.findElementByIosNsPredicate
.findElementByIosNsPredicate
.findElementByIosNsPredicate
.findElement
僅支持 iOS 10或以上,可支持元素的單個屬性和多個屬性定位,推薦使用。
一種屬性:MobileBy.iOSNsPredicateString("type == 'XCUIElementTypeButton'")
兩種屬性:MobileBy.iOSNsPredicateString("type == 'XCUIElementTypeButton' AND label == '更多信息'")
具體 iOSNsPredicate語法結構可查看官方文檔,或查看的這個帖子:iOS 定位方式 iOSNsPredicateString 詳解。
Appium 的定位方式種類
我說的定位方式都是基於我自己親測過,沒使用或比較少用的就在這里不列舉了,如有錯誤的地方,請多多包涵!常用的定位方式(僅限 Android 和 iOS 兩種系統)有 className、id、xpath、AccessibilityId、AndroidUIAutomator、iOSNsPredicateString、iOSClassChain、IosUIAutomation等
className
使用元素的className
屬性定位,支持:Android 和 iOS,推薦使用。MobileBy.className("XCUIElementTypeButton")
id
使用元素的Resource Id
屬性定位,支持:Android,僅支持 Android 4.3或以上,推薦使用。反正我沒有在 iOS 用過,大家有正確使用過的例子,可以分享一下。MobileBy.id("package.name:id/android")
xpath
支持:Android 和 iOS。但由於 iOS 10開始使用的 XCUITest 框架原聲不支持,定位速度很慢,所以官方現在不推薦大家使用,也有其他替代的定位方式可使用。
1.使用絕對路徑定位,如截圖所顯示的 xpath 路徑MobileBy.xpath("className/className/className/className")
2.使用相對路徑定位MobileBy.xpath("//className")
3.通過元素的索引定位MobileBy.xpath("//className[index]")
4.通過元素的屬性定位
一種屬性:MobileBy.xpath("//className[@label='更多信息']")
兩種屬性:MobileBy.xpath("//className[@label='更多信息'][@isVisible='1']")
部分屬性(最強大):MobileBy.xpath("//className[contains(@label,'更多')]")
AccessibilityId
替代以前的name
定位方式,推薦使用。
在 Android 上,主要使用元素的content-desc屬性
,如該屬性為空,不能使用此定位方式。
在 iOS 上,主要使用元素的label
或name
(兩個屬性的值都一樣)屬性進行定位,如該屬性為空,如該屬性為空,也是不能使用該屬性。MobileBy.AccessibilityId("更多信息")
AndroidUIAutomator
僅支持 Android 4.2或以上,可支持元素的單個屬性和多個屬性定位,推薦使用。
一種屬性:MobileBy.AndroidUIAutomator("new UiSelector().text(\"發送\")")
兩種屬性:MobileBy.AndroidUIAutomator("new UiSelector().text(\"發送\").clickable(true)")
元素的所有屬性都可用做定位,功能非常強大,且速度很快。
iOSNsPredicateString
僅支持 iOS 10或以上,可支持元素的單個屬性和多個屬性定位,推薦使用。
一種屬性:MobileBy.iOSNsPredicateString("type == 'XCUIElementTypeButton'")
兩種屬性:MobileBy.iOSNsPredicateString("type == 'XCUIElementTypeButton' AND label == '更多信息'")
具體 iOSNsPredicate語法結構可查看官方文檔,或查看的這個帖子:iOS 定位方式 iOSNsPredicateString 詳解。
iOSClassChain
僅支持 iOS 10或以上,這是 github 的 Mykola Mokhnach 大神開發,僅限在 WebDriverAgent 框架使用,用於替代 xpath 的,但使用一陣子后,感覺靈活性沒有 xpath 和 iOSNsPredicate 好,應該還不完善吧。具體使用方法,請見:https://github.com/appium/appium-xcuitest-driver/pull/391 。MobileBy.iOSClassChain('XCUIElementTypeWindow[1]/XCUIElementTypeOther[1]/XCUIElementTypeOther[1]/XCUIElementTypeNavigationBar[1]/XCUIElementTypeOther[1]/XCUIElementTypeButton[2]')
IosUIAutomation
僅支持 iOS 9.3或以下,是 iOS 舊框架 UIAutomation 的定位方式,現在基本上很少使用,這個定位類型同樣可使用 iOS 謂詞進行定位,詳細可參考:iOSNsPredicate
總結:
以上這個多定位方式,很少說全部用完。根據我的經驗,推薦使用:Android:AndroidUIAutomator > className = id = AccessibilityId > xpath。iOS:iOSNsPredicateString > className = AccessibilityId