cashapp / AccessibilitySnapshot

Easy regression testing for iOS accessibility
Apache License 2.0
550 stars 69 forks source link

Getting Started #215

Closed LostStudent closed 5 months ago

LostStudent commented 6 months ago

Hey Id love to give this a try but im having trouble using the accessibilityImage .

when I use SnapshotTesting's assert:

 func testExample() throws {
        assertSnapshot(of: ContentView(), as: .image)
}

that takes a snapshot.

But when I try to use the accessibilityImage strategy :

func testExample() throws {
        assertSnapshot(of: ContentView(), as: .accessibilityImage)

    }

I get compile error: "Type of expression is ambiguous without a type annotation" is this trying to use SnapshotTesting's assert rather than AccessibilitySnapshot's?

NickEntin commented 6 months ago

Hey @LostStudent, that error means the compiler can't resolve the method signature. The assertSnapshot method is defined by SnapshotTesting, while the accessibilityImage strategy is defined by AccessibilitySnapshot.

  1. How are you installing the frameworks? (What package manager, and what configuration options within that package manager)
  2. Are you importing both frameworks in this test class file? What is the import statement for AccessibilitySnapshot?
LostStudent commented 6 months ago

ah adding a hosting controller works.

func testExample() throws {

        let host = UIHostingController(rootView: ContentView())
        host.view.frame = .init(x: 0, y: 0, width: 100, height: 100)
        assertSnapshot(
            matching: host,
            as: .accessibilityImage(showActivationPoints: .always,
                                    drawHierarchyInKeyWindow: true),
            named: "shot"
        )

    }

should the previous usage have worked? My test project imports AccessibilitySnapshot using swift package manager in xcode. AccessibilitySnapshot is added as a dependency of the test target the imports in my test file are:

import XCTest
import SnapshotTesting
@testable import accessSnaps
import SwiftUI
import AccessibilitySnapshot
NickEntin commented 5 months ago

Ahh, that's expected. Try updating to version 0.7. Prior versions didn't support snapshotting SwiftUI views directly, requiring a hosting controller instead.