Xcode 9.3 staticText element query limits 128 characters for UI testing
Asked Answered
M

1

5

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:)

Mikiso answered 11/5, 2018 at 11:42 Comment(0)
P
6

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.

Phonoscope answered 14/5, 2018 at 8:57 Comment(4)
Thanks for the response @lagoman. LIKE is not working as the lengthy text has line breaks, is there any other alternative ?Mikiso
@Mikiso I edited my answer adding alternative approach for your problem.Phonoscope
Thank you @lagoman, I tried with 'LIKE' and replaced line break '\n' with '?' and this worked for me thanks :)Mikiso
@Mikiso You're welcome. Don't forget to accept answer.Phonoscope

© 2022 - 2024 — McMap. All rights reserved.