1

I am starting to include some SwiftUI code in my UIKit project. The UIKit project had UI Tests using XCUITest which, as you may know, can query elements down the UI tree and can even have an identifier to narrow do your elements found.

I have moved over all the accessibility identifiers, hints, labels, and values have been moved over to the SwiftUI components.

In my case, I had a UITableView which is now a SwiftUI List. In my UI tests, I would query the UITableView like this:

let app = XCUIApplication()

let tableCells = app.table.cells["MyCellIdentifier"]

However, while this worked in SwiftUI, it no longer works in SwiftUI's List. I understand SwiftUI pretty much hides more of the implementation details of how it builds its UI and it may not even be a Table View under the covers but it certainly looks and acts like the UITableView I was using before.

Does anyone know how to properly test a table view (List) in SwiftUI using XCUITest or why it doesn't work with the existing test code?

As you can see below, the sample code shows me adding a accessibility identifier to a RowView but for Accessibility, it gets passed down to the Text views (StaticText in the Accessibility tree).

enter image description here

Mario A Guzman
  • 3,483
  • 4
  • 27
  • 36
  • This doesn’t sound right. There should be no difference in querying a table or cells built with SwiftUI. What does your `debugDescription` show? – Mike Collins Sep 10 '21 at 12:57
  • @MikeCollins, good idea! I didn't think to try that so I just did. It's weird though. It seems to generate something different that what I'd expect based on how I combine elements. For example, the two Text labels I have in my "cell" have individual accessibility identifiers however they get erased because its containing VStack uses `.accessibilityElement(children: .combine)`. Also, there doesn't seem to be a way to add an identifier to the container cell, it always goes to its immediate child of type "other." – Mario A Guzman Sep 10 '21 at 19:22
  • Could you ID your cells by getting the parent `otherElement` of those `staticText`s? – Mike Collins Sep 11 '21 at 20:10

1 Answers1

0

There is a modifier in SwiftUI for that:

func accessibilityIdentifier(_ identifier: String) -> ModifiedContent<Self, AccessibilityAttachmentModifier>

if you add it to your View element like this:

.accessibility(identifier: ["MyCellIdentifier")

It will get exposed to XCUITests

LuLuGaGa
  • 13,089
  • 6
  • 49
  • 57
  • 1
    I forgot to mention that the same flags were moved over to the SwiftUI code. My issue is that even without using the flags in SwiftUI, generic queries like this one `let tableCells = app.table.cells` doesn't seem to work. – Mario A Guzman Sep 09 '21 at 17:50