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

ale*_*lex 0 java xpath ios appium xctest

我正在使用 appium 1.6.0-beta1、Xcode 8、iPhone iOS 10 模拟器和 java 编写移动自动测试。

我想找到不使用 xpath 的元素。在 appium 1.5.3 中可以使用 Ios 自动化(在 appium java-client 中有 MobileBy.ByIosUIAutomation),但现在 UI 自动化被 XCUITest 取代。

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

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

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

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

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

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

noo*_*oor 5

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

NSPredicate(format: "label CONTAINS 'your label string'")
Run Code Online (Sandbox Code Playgroud)

或者

NSPredicate(format: "label BEGINSWITH 'your label string'")
Run Code Online (Sandbox Code Playgroud)

或者

NSPredicate(format: "label ENDSWITH 'your label string'")
Run Code Online (Sandbox Code Playgroud)

或者

NSPredicate(format: "label == 'your label string'")
Run Code Online (Sandbox Code Playgroud)

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

app.cells.matchingPredicate(NSPredicate(format: "label CONTAINS 'Olivia'"))
Run Code Online (Sandbox Code Playgroud)

这将返回 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.
Run Code Online (Sandbox Code Playgroud)

现在使用这个元素来挖掘或从这个元素中获取任何东西。

elem.tap()
Run Code Online (Sandbox Code Playgroud)

希望这会有所帮助并让我知道。