UITest: check if text with prefix exists

When performing a user interface test, I can verify that the text exists as follows:

XCTAssertTrue(tablesQuery.staticTexts["Born: May 7, 1944"].exists)

But, how can I check if the text exists if I only know the prefix?

I would like to do something like:

XCTAssertTrue(tablesQuery.staticTextWithPrefix["Born: "].exists)

or even better:

XCTAssertTrue(tablesQuery.staticTextWithRegex["Born: .+"].exists)
+4
source share
1 answer

You can use predicates to search for elements by prefix. For instance:

let app = XCUIApplication()
let predicate = NSPredicate(format: "label BEGINSWITH 'Born: '")
let element = app.staticTexts.elementMatchingPredicate(predicate)
XCTAssert(element.exists)

Note that this may fail if more than one item matches the predicate. More information can be found in the blog post, Cheat Sheet for user interface testing .

+6

All Articles