nalexn / ViewInspector

Runtime introspection and unit testing of SwiftUI views
MIT License
2.09k stars 145 forks source link

[Suggestion]How about screenshot/snapshot testing? #306

Open frankois944 opened 4 weeks ago

frankois944 commented 4 weeks ago

is It (technically) possible to make a screenshot of an inspected view to make snapshot testing? Or at least, a screenshot, it should be a good start.

It's currently possible with some tool like https://www.browserstack.com/guide/snapshot-testing-ios

On Android, it's now easily possible to screenshot/snapshot testing an app and make some testing faster.

Like https://github.com/cashapp/paparazzi or even google propose an official tool https://developer.android.com/studio/preview/compose-screenshot-testing.

frankois944 commented 4 weeks ago

Ah found something that could work Using this for taking/checking a screenshot : https://github.com/pointfreeco/swift-snapshot-testing

We could use the Framework like this :

 let sut = ContentView()

 try await ViewHosting.host(sut) { hostedView in
    hostedView.inspect { @MainActor view in
        let actual = try view.actualView()
        assertSnapshot(of: actual, as: .image(layout: .device(config: .iPhone12)))
    }
}