Assert that a View has a specific accessibility traits modifier

I have a project using #ViewInspector , which doesn’t seem to have a means to test #accessibilityTraits on view. Is there any other approach?

What I have:

struct MyView: View {

    Text("Header")

        .accessibilityAddTraits(.isHeader)

}

What I needed:

final class MyViewTests {

    func test_MyView_TextShouldHaveIsHeaderAccessibilityTrait() {

        // Arrange

        let sut = MyView()

        

        // Act

        let text = sut.inspect().text()

        

        // Assert

        XCTAssertTrue(text.accessibilityTraits.contains(.isHeader)) /// ERROR: Value of type 'InspectableView<ViewType.Text>' has no member 'accessibilityTraits'

    }

}

This forum is meant for the Swift Testing library that is part of the Swift project, not for Swift testing questions in general. Since your question is about a specific third-party library (ViewInspector), you may have better luck opening an issue on their GitHub repository.

(EDIT: I've moved this post from Development > Swift Testing to Using Swift.)

1 Like

Thanks for the link, mr Allevato. However, I am open to other frameworks or native solutions. Is there any other approach to assert that a view contains a specific accessibility trait ?