Appium:如何对 XCUIElement 使用 ios 谓词策略

Appium: How to use ios predicate strategy for XCUIElement

我正在使用 appium 1.6.0-beta1、Xcode 8、iPhone iOS 10 模拟器和 java.

编写移动自动测试

我想查找不使用 xpath 的元素。 在 appium 1.5.3 中可以使用 Ios Automation(在 appium java-client 中有 MobileBy.ByIosUIAutomation),但现在 UI Automation 被 XCUI测试。

我发现,ios 谓词可用于查找 XCUI 元素(MobileBy.ByIosNsPredicate in appium java-client 5.0.0)。

我的问题是如何使用 ios 谓词查找元素?

我在这里找到了UI自动化的规则:http://appium.readthedocs.io/en/stable/en/writing-running-appium/ios_predicate/#appium-predicate-helpers。 例如,这里是 ios 等价于 xpath 的谓词:

ios 谓词: tableViews()[1].cells().firstWithPredicate("label == 'Olivia' ")

xpath: /UIATableView[2]/UIATableCell[@label = 'Olivia'][ 1]

对于这样的 xpath 我应该使用什么 ios 谓词: //XCUIElementTypeCell/XCUIElementTypeStaticText[3] ?

首先在 xcode ui 测试中,你可以像下面这样使用 NSPredicate :

NSPredicate(format: "label CONTAINS 'your label string'")

NSPredicate(format: "label BEGINSWITH 'your label string'")

NSPredicate(format: "label ENDSWITH 'your label string'")

NSPredicate(format: "label == 'your label string'")

所以,在你的情况下,你的解决方案是

app.cells.matchingPredicate(NSPredicate(format: "label CONTAINS 'Olivia'"))

this returns xcelementquery 这意味着您将获得一个或多个元素。 从那里,您可以选择您想要的元素,例如:

let elem = app.cells.matchingPredicate(NSPredicate(format: "label CONTAINS 'Olivia'")).elementBoundByIndex(0) //it may be 0 or 1 or 2
//you can know this using p print in debug mode.

现在使用这个元素点击或从这个元素中获取任何东西。

elem.tap()

希望这对您有所帮助并告诉我。