45

I'm trying to record some XCUITesting functions. I get this error.

Timestamped Event Matching Error: Failed to find matching element

Is this an Xcode bug and/or issues with the complexity of my UI?

emoleumassi
  • 4,881
  • 13
  • 67
  • 93

3 Answers3

7

I was having the same problem for a long time on a very complex app as well: The key is all in isAccessibilityElement being set to YES. UI testing and accessibility go hand-in-hand. The way I think about it is that the device needs to be able to interact with the element, since ostensibly, the user wouldn't be able to.

I did this twice:

  1. In cellForRowAtIndexPath - cell.isAccessibilityElement = YES

  2. In awakeFromNib in a UITableViewCell subclass - self.isAccessibilityElement = YES

Hope that helps, and good luck!

ArielSD
  • 829
  • 10
  • 27
  • 1
    This is a great answer! I was adding and removing views from a container programmatically. In my xib file, the accessibility was enabled! But it seems that when you are doing a lot of programmatic manipulation, every time you have added a subview, just do .isAccessibilityElement = true and BOOM! It all works. Thanks for this! – Mike Critchley Jul 17 '19 at 21:57
  • OK, I think I spoke too soon. Making the self.window accessible did, indeed allow the elements on the root view to be accessible...but only in terms of the window hierarchy. And all taps after that just show as window.tap() when recording. I tried, after adding the root view, to make all subviews (iterating through root.view.subviews and making them all accessible, but to no avail. That root view added programmatically is just invisible to UI testing. – Mike Critchley Jul 17 '19 at 22:15
  • Hey Mike! I haven't used XCUITesting in a while - the first question that comes to mind is why you'd ever have to explicitly tap the window, instead if the elements themselves? – ArielSD Jul 22 '19 at 14:56
  • 1
    Hi @ArialSD Yeah, I was just tapping to see what showed up in the recorder. I finally did figure it out. The key was turning OFF isAccessible for some of the containing view controllers. They still have accessibility IDs and can be identified, but basically, the isAccessibilityElement seems to be like a tap recognizer...it masks anything underneath (or in this case, nested views). – Mike Critchley Jul 22 '19 at 15:01
  • 1
    @MikeCritchley Ah, nice, good find, and glad you figured it out! – ArielSD Jul 23 '19 at 17:03
  • @ArielSD I did it as you mentioned, but still face the same problem. I use Xcode 11.2.1. Only tableviewcell is so weird. – Yvan Nov 26 '19 at 02:32
  • Hey @Yvan, I haven't touched this in a while; maybe try making sure that you can access all superviews of the tableViewCell, and make sure they are accessible? – ArielSD Nov 26 '19 at 21:28
1

Try manipulating with items isAccessibilityElement property. As far as I know containers like tableView should have this property set to NO in order to items contained inside them be accessible

Quote from https://developer.apple.com/library/ios/documentation/UIKit/Reference/UIAccessibility_Protocol/#//apple_ref/occ/instp/NSObject/isAccessibilityElement :

Assistive applications can get information only about objects that are represented by accessibility elements. Therefore, if you implement a custom control or view that should be accessible to users with disabilities, set this property to YES. The only exception to this practice is a view that merely serves as a container for other items that should be accessible.

Kamilton
  • 696
  • 6
  • 12
1

You need to add key path value to your UI elements as below. For an example, if you are using a tableview then select it in the Storyboard and then click on Identity Inspector. Now enter the following value isAccessibilityElement in the key and tick the checkbox as shown in the image below.

enter image description here

Also if you can't tap on the UITableViewCell or the subclasses, then enable the Accessibility option as shown below.

enter image description here

hereafter it should work just fine.

Jay Mayu
  • 17,023
  • 32
  • 114
  • 148