Xcode 9.3 staticText 元素查询限制 128 个字符用于 UI 测试

Sar*_*nya 4 xcode-ui-testing

在 Xcode 9.3 中,当我尝试运行 UI 测试用例时,它开始给我以下异常,其中发现超过 128 个字符的冗长消息 -

由于未捕获的异常“NSInternalInconsistencyException”而终止应用程序,原因:“无效查询 - 字符串标识符“超过 128 个字符的冗长消息......”超过了 128 个字符的最大长度。您可以通过使用自定义 NSPredicate 构建查询来解决此限制,该查询指定要匹配的属性(标签、标题、值、占位符值或标识符)。

给出的解决方法是使用自定义 NSPredicate 如下所示,

let predicate = NSPredicate(format: "label BEGINSWITH 'Empty '")
let label = app.staticTexts.element(matching: predicate)
XCTAssert(label.exists)
Run Code Online (Sandbox Code Playgroud)

但是如果我们像上面那样使用谓词,我们可能无法断言整个文本消息。还有其他可能的方法可以断言整个文本吗?请告诉我。

谢谢,干杯:)

lag*_*man 5

为什么不使用LIKE代替BEGINSWITH. LIKE匹配整个文本。

let predicate = NSPredicate(format: "label LIKE 'Your lengthy text that you want to match...'")
let label = app.staticTexts.element(matching: predicate)
XCTAssert(label.exists)
Run Code Online (Sandbox Code Playgroud)


编辑:阅读 OP 的评论后,我建议采用替代方法:

如果使用 Interface Builder,请将辅助功能标识符添加到您的标签(或 TextView)。您可以通过选择包含冗长文本的标签并打开位于 Xcode 右侧的身份检查器来完成此操作。从那里,找到可访问性区域并添加lengthyTextLabel到标识符部分。

在此处输入图片说明

如果使用 ViewController 来操作视图,只需这样写:

lengthyLabel.accessibilityIdentifier = "lengthyTextLabel"
Run Code Online (Sandbox Code Playgroud)

在您的测试中,您可以通过编写以下内容来获取元素:

let lengthyText = app.staticTexts.element(matching: .any, identifier: "lengthyTextLabel")
Run Code Online (Sandbox Code Playgroud)

这样你就可以找到你的长文本:lengthyText.label