In Xcode UI testing, how do I test that staticTexts contains a string?
In the debugger, I can run something like this to print out all the content of staticTexts:
You can create an extensions to simply use it over XCUIElement
.
extension XCUIElement {
func assertContains(text: String) {
let predicate = NSPredicate(format: "label CONTAINS[c] %@", text)
let elementQuery = staticTexts.containing(predicate)
XCTAssertTrue(elementQuery.count > 0)
}
}
Usage:
// Find the label
let yourLabel = app.staticTexts["AccessibilityIdentifierOfYourLabel"].firstMatch
// assert that contains value
yourLabel.assertContains(text: "a part of content of the staticText")