如何在 Xcode UI 测试中查询标签

How to query labels in Xcode UI testing

我知道我可以查询文本字段、表格、按钮,但不能查询标签,为什么?

app.textFields["Username"].typeText("bcyops")
app.secureTextFields["Password"].typeText("ops15")
app.buttons["Submit"].tap()
app.tabBars.buttons["Main"].tap()
let tablesQuery = app.tables

您可以使用NSPredicates查询具有指定标签的元素:

app.textViews.element(matching: NSPredicate(format: "label LIKE theLabelYoureLookingFor"))

如果你的意思是标签为静态文本元素,你可以查询标签:

let label = app.staticTexts["yourLabelIdentifier"]

如果您需要标签中的文字

let textInLabel = app.staticTexts["yourLabelIdentifier"].label

包含所有元素类型的文档在此处:https://developer.apple.com/documentation/xctest/xcuielementtypequeryprovider

标签在 XCTest UI 测试中被称为 staticTexts

app.staticTexts["myLabel"].exists