Saranya
Saranya

Reputation: 51

Xcode 9.3 staticText element query limits 128 characters for UI testing

In Xcode 9.3, when I try to run UI test cases it started giving me below exception where ever it found lengthy messages exceeding 128 characters -

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Invalid query - string identifier "lengthy message more than 128 characters..." exceeds maximum length of 128 characters. You can work around this limitation by constructing a query with a custom NSPredicate that specifies the property (label, title, value, placeholderValue, or identifier) to match against.'

Workaround given is to use custom NSPredicate something like below,

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

But If we use predicate like above, we might not be able to assert the entire text message.Is there any other possible way where we can assert the entire text? Please let me know.

Thanks, Cheers:)

Upvotes: 5

Views: 2435

Answers (1)

Mladen
Mladen

Reputation: 2210

Why don't you use LIKE instead of BEGINSWITH. LIKE matches entire text.

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


EDIT: After reading OP's comment, I'm suggesting alternative approach:

If using Interface Builder, add accessibility identifier to your Label (or TextView). You can do it by selecting label that holds lengthy text and opening Identity inspector that is on the right side of Xcode. From there, find Accessibility area and add lengthyTextLabel to Identifier section.

enter image description here

If using ViewController to manipulate views, just write this:

lengthyLabel.accessibilityIdentifier = "lengthyTextLabel"

And in your tests, you can get your element by writing this:

let lengthyText = app.staticTexts.element(matching: .any, identifier: "lengthyTextLabel")

This way you can find your long text with: lengthyText.label.

Upvotes: 6

Related Questions