我正在使用 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 被 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] ?
最佳答案
首先在 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'"))
这将返回 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()
希望这对您有所帮助并让我知道。
关于java - Appium:如何为 XCUIElement 使用 ios 谓词策略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39661647/