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

标签 java ios xpath appium xctest

我正在使用 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/

相关文章:

ios - 添加 subview Controller 后 UIView 容器消失

ios - 即使成功切换到前置摄像头,iPhone 仍然显示后置摄像头的 View

objective-c - 释放没有指针的对象?

xpath - Xpath选择一级子级

python - xPath:将表达式与实际源代码匹配困难

java - HyperLinkListener 取消并保存文件

java - 使用 java.net.HttpURLConnection 时是否可以转储 HTTP header ?

xml - 或者在 XPath 表达式中从多个节点名称中进行选择

java - OGNL 线程安全

java - 无法编译使用通用列表参数调用接口(interface)中方法的类