Search code examples
xctestxcode-ui-testing

iOS UI Testing cannot see the UILabel


I have a UI testing issue with some code I've got from another developer (Whose not with the project anymore). The code display a simple alert box at the top of the screen when there's an error and I cannot get my UI test to see the UILabel within it.

The structure of the box looks like this:

UIView
   + UILabel
   + UIButton

And in the custom class for the UIView there is this code:

// Original code
accessibilityIdentifier = "AlertBar" // Required for UI Testing.

// Bits I've added trying to make messageLabel visible to UI Test
isAccessibilityElement = true
accessibilityElements = [messageLabel]
messageLabel.accessibilityIdentifier = "AlertBarMessage"
messageLabel.isAccessibilityElement = true
}

If I breakpoint in the middle of my UI Test and dump the app into the log I can see the UIView:

Attributes: Application, pid: 60317, label: 'The App'
Element subtree:
 →Application, 0x600001df8c40, pid: 60317, label: 'The App'
    Window (Main), 0x600001dfa060, {{0.0, 0.0}, {390.0, 844.0}}

    ... app ui logged here!

      Other, 0x600001dce220, {{4.0, 47.0}, {382.0, 107.7}}, identifier: 'AlertBar'

So it's clear that the alert is visible to XCTest, but nothing inside it. As you can tell from my added code I've been trying all sorts of things but so far I've not been able to make the UILabel (AlertBarMessage) visible.

What am I missing?


Solution

  • As per usual, after typing up a stackOVerFlow question I go back to the code and figure out the problem :-) In this case it was the isAccessibilityElement = true on the view. Making it accessible was effectively hiding the nested UILabel so turning it off fixed my test.

    I know there are ways in Accessibility to setup the view correctly for accessibility so that screen readers see it as a single element and can read the message, but I'll have to work on that later for now.