UITest: Check if text with prefix exists
Asked Answered
B

1

7

While doing a UI test, I can test that a text exists like this:

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

But, how do I test if a text exist, 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)
Bond answered 25/5, 2016 at 15:56 Comment(0)
Y
11

You can use predicates to find elements by prefixes. For example:

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

Note that this could fail if more than one element matches the predicate. More information can be found in a blog post, a Cheat Sheet for UI Testing.

Yocum answered 25/5, 2016 at 16:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.