UI Testing SwiftUI Tables

I maintain a macOS SwiftUI app whose user interface contains a Table, one with a bound sortOrder. The app's Xcode project contains UI tests that click on the column headers, to exercise the sorting logic.

These tests are really fragile. Odds are good that any new release of macOS, or Xcode, or SwiftUI, will break them.

I'm not sure why they're so fragile. I think the way SwiftUI generates the view hierarchy for a Table may still be in flux.

In any case, here is what I'm currently using to click on all of the column headers of a SwiftUI Table. This is for macOS 13.1, Xcode 14.2, Swift 5.7, SwiftUI 4.2.11.

    private func clickTableColumnHeaders(tableEl: XCUIElement) {
        for column in tableEl.tableColumns.allElementsBoundByIndex {
            let height = column.frame.height
            // Assume the column header lies above the column.
            let y = CGFloat(-10.0)
            let offset = CGVector(dx: 0.5, dy: y / height)
            let coords = column.coordinate(withNormalizedOffset: offset)
            // Click twice to exercise both sort orders.
            coords.click()
            coords.click()
        }
    }