207: Xcode UI Testing | Ray Wenderlich


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3619-207-xcode-ui-testing

How to tap on specific word of a label in Xcode10 Ui Testing?

is this possible to tap on particular word in a String(Label)? If this is possible please explain me.

Ex:- ā€œI agree to the Terms and Conditions and Privacy Policyā€.

The ā€œTerms and Conditionsā€ string in the above line is not a link, it exists as part of whole ā€˜Labelā€™ and I want to tap on specific ā€œTerms and Conditionsā€.

@designatednerd Can you please help with this when you get a chance? Thank you - much appreciated! :]

Ooh, thatā€™s a tough one! (Sorry I missed this until now!)

With XCUI itā€™s really going to be difficult because you donā€™t really have access directly to the label itself, so you canā€™t really calculate how the label would be laid out, and it would be really difficult to calculate precisely where the tester needs to tap.

Remember that a UI tester is a bit like a blind user, since it canā€™t really ā€œseeā€ whatā€™s on the screen and uses all the accessibility APIs that a blind user would in practice. If thereā€™s no way for a blind user to find something, then thereā€™s no way for the UI tester to find it.

Iā€™d recommend starting from there: How would a blind user find it with accessibility APIs? Once you figure that out, you can use those APIs to access it with XCUI.

thanks for your reply.
is it possible to implement tap() on particular word in a String(Label) or not ?

I guess Iā€™ll be a bit more clear than I was previously: Almost certainly not, because thereā€™s no way for the computer to ā€œseeā€ which words are where on the screen.

Youā€™d need to create some kind of subclass which allowed accessibility to examine labels word by word (which may or may not be what your blind users actually want to do).

Honestly, on iOS, requiring a user to click on a specific part of a label is a bit of an anti-pattern, particularly since the tap targets for single words are going to be really small, and therefore very annoying to try to hit with an actual finger (plus the aforementioned ways it screws up accessibility).

You probably want to consider a different way to do whatever it is youā€™re doing that doesnā€™t require such pinpoint accuracy.