Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SwiftUI Accessibility for UI Testing #568

Open
toddgower-hiatus opened this issue Dec 11, 2024 · 0 comments
Open

SwiftUI Accessibility for UI Testing #568

toddgower-hiatus opened this issue Dec 11, 2024 · 0 comments

Comments

@toddgower-hiatus
Copy link

I'm attempting to set accessibility for a SwiftUI view that is presented within a hosting view controller that SwiftMessages presents. I attempted conforming to AccessibleMessage and setting the UIHostingController to the accessibleElement property. This did not seem to work as the Appium inspector that we're using still does not see the buttons within the SwiftUI View. Any tips on getting accessibillity to work with SwiftUI?

SwiftUI container UIView

class PopUpContainerView<T: View>: UIView, AccessibleMessage {
    // Accessibility for E2E testing
    var accessibilityMessage: String?
    var accessibilityElement: NSObject?
    var additonalAccessibilityElements: [NSObject]?

    let uiContainerView: UIHostingController<T>

    init(content: T, cornerRadius: CGFloat = 15, padding: CGFloat = 24) {
        uiContainerView = UIHostingController(rootView: content)
        super.init(frame: .zero)
        addSubview(uiContainerView.view)
        uiContainerView.view.layer.cornerRadius = cornerRadius
        uiContainerView.view.pinSuperview(horizontalPadding: padding)

        accessibilityElement = uiContainerView
    }

    @available(*, unavailable)
    required init(coder _: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

which is presented like this:

let view = PopUpContainerView(
    content: MySwiftUIView(onExit: {
        SwiftMessages.hide()
    }, onProceed: {
        SwiftMessages.hide()
        // do something
    }),
    cornerRadius: 12
)
var config = SwiftMessages.Config()
config.duration = .forever
config.presentationStyle = .center
config.dimMode = .gray(interactive: true)
SwiftMessages.show(config: config, view: view)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants