kif-framework / KIF

Keep It Functional - An iOS Functional Testing Framework
Other
6.2k stars 909 forks source link

Does KIF work with SwiftUI? #1199

Open rauls4 opened 3 years ago

rauls4 commented 3 years ago

Hello, we are looking for UI testing options and KIF has come up. I have not been able to find out about SwiftUI compatibility.

Any information would be appreciated.

Thanks

kenji21 commented 3 years ago

Can confirm it works, and I've also tried some interesting thing, using SwiftUI previews in tests:

    func testContentViewPreviews() {
        // Given
        let contentViewPreviews = ContentView_Previews.previews

        // When
        solo.showViewControllerInCleanWindow(UIHostingController(rootView: contentViewPreviews))
        let shouldFindVersion = solo.waitFor(textWithPrefix: "Version")

        // Expect
        XCTAssertTrue(shouldFindVersion)
        attachScreenshot(name: "contentViewPreviews")
        solo.window?.printHierarchy()
    }

Here is the log:

UIWindow
|   UITransitionView
|   |   UIDropShadowView
|   |   |   _TtGC7SwiftUI14_UIHostingViewGVS_15ModifiedContentV7myapp11ContentViewGVS_30_EnvironmentKeyWritingModifierGSqCS2_8AppState____
|   |   |   SwiftUI.AccessibilityNode, label: (null), traits: image
|   |   |   SwiftUI.AccessibilityNode, label: Version BETA 0.0.1, traits: static text
|   |   |   SwiftUI.AccessibilityNode, label: Copyright © myapp, traits: static text
|   |   |   |   UIView
|   |   |   |   _TtCOCV7SwiftUI11DisplayList11ViewUpdater8Platform13CGDrawingView
|   |   |   |   _TtCOCV7SwiftUI11DisplayList11ViewUpdater8Platform13CGDrawingView
|   |   |   |   _TtC7SwiftUIP33_A34643117F00277B93DEBAB70EC0697122_UIShapeHitTestingView
|   |   |   |   _TtC7SwiftUIP33_A34643117F00277B93DEBAB70EC0697122_UIShapeHitTestingView
|   |   |   |   UIView
Test Case '-[MyAppTests.ContentViewTests testContentViewPreviews]' 
rauls4 commented 3 years ago

Wow. That’s great to learn. You might want to indicate this on the read me for others wondering the same.

Thank you very much!

Raul Silva

On Dec 12, 2020, at 10:25 AM, kenji21 notifications@github.com wrote:

Can confirm it works, and I've also tried some interesting thing, using SwiftUI previews in tests:

func testContentViewPreviews() {
    // Given
    let contentViewPreviews = ContentView_Previews.previews

    // When
    solo.showViewControllerInCleanWindow(UIHostingController(rootView: contentViewPreviews))
    let shouldFindVersion = solo.waitFor(textWithPrefix: "Version")

    // Expect
    XCTAssertTrue(shouldFindVersion)
    attachScreenshot(name: "contentViewPreviews")
    solo.window?.printHierarchy()
}

Here is the log:

UIWindow | UITransitionView | | UIDropShadowView | | | _TtGC7SwiftUI14_UIHostingViewGVS_15ModifiedContentV7myteepi11ContentViewGVS_30_EnvironmentKeyWritingModifierGSqCS2_8AppState____ | | | SwiftUI.AccessibilityNode, label: (null), traits: image | | | SwiftUI.AccessibilityNode, label: Version BETA 0.0.1, traits: static text | | | SwiftUI.AccessibilityNode, label: Copyright © myapp, traits: static text | | | | UIView | | | | _TtCOCV7SwiftUI11DisplayList11ViewUpdater8Platform13CGDrawingView | | | | _TtCOCV7SwiftUI11DisplayList11ViewUpdater8Platform13CGDrawingView | | | | _TtC7SwiftUIP33_A34643117F00277B93DEBAB70EC0697122_UIShapeHitTestingView | | | | _TtC7SwiftUIP33_A34643117F00277B93DEBAB70EC0697122_UIShapeHitTestingView | | | | UIView Test Case '-[MyAppTests.ContentViewTests testContentViewPreviews]' — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/kif-framework/KIF/issues/1199#issuecomment-743779774, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAQ5VLJVR6QBQLKCCWANZULSUOKQXANCNFSM4UXF3HIA.

dheerajn commented 3 years ago

Yes, this works for me. All I had to do is to use UIHostingController(:) in place of ViewController and load the SwiftUI View