cashapp / AccessibilitySnapshot

Easy regression testing for iOS accessibility
Apache License 2.0
534 stars 65 forks source link

Support SwiftUI previews #164

Open ZevEisenberg opened 8 months ago

ZevEisenberg commented 8 months ago

I have no idea how feasible it would be to build this, given the requirement for hosted tests, but I'd love if I could wrap my SwiftUI previews in some utility from this library that would let me see my accessibility labels alongside my view while developing, instead of after the fact when snapshot testing.

NickEntin commented 8 months ago

My understanding is previews are rendered in a host app, so I don't think that would be a problem. One other requirement is the container needs to be added to a window so it can convert to the screen coordinate space. I'm guessing previews would need to use a window. The setup code would look something like this:

#Preview {
    let container = AccessibilitySnapshotView(
        containedView: theViewYouWantToSnapshot,
        viewRenderingMode: .drawHierarchyInRect,
        activationPointDisplayMode: .whenOverridden
    )

    // This part should run once the container is added to a window:
    try container.parseAccessibility(useMonochromeSnapshot: true)
    container.sizeToFit()

    return container
}

Maybe we could wrap the AccessibilitySnapshotView inside another container that calls the parse method when it's added to a window? I'm not really sure what the life cycle for a preview is.

DavidBrunow commented 3 months ago

I worked on a sketch of what this could look like: https://github.com/cashapp/AccessibilitySnapshot/pull/213