ViewInspector icon indicating copy to clipboard operation
ViewInspector copied to clipboard

Unable to locate elements in SwiftUI Tree Graph Xcode 16.

Open azamsharpschool opened this issue 1 year ago • 4 comments

I have made a very simple view as shown below:

struct ContentView: View {

@State private var count: Int = 0

var body: some View {
    VStack {
        Text("\(count)")
        Button("Increment") {
            count += 1
        }
    }
}

}

and written test for it.

final class LearnTests: XCTestCase {

func testViewInspectorBaseline() throws {
   
    let view = ContentView()
    let countText = try view.inspect().vStack().text(0)
    let incrementButton = try view.inspect().vStack().button(1)
    
    try incrementButton.tap()
    
    XCTAssertEqual(try countText.string(), "100")
}

}

But it always give the following error: failed: caught error: "vStack() found Learn.ContentView instead of VStack"

Any ideas?

azamsharpschool avatar Feb 15 '25 01:02 azamsharpschool

Hi Mohammad! Haha, are you trying my "counter app" challenge?

Here's a thread of how as of Xcode 16.2, Apple is inserting views where we didn't ask for them. https://github.com/nalexn/ViewInspector/issues/327#issuecomment-2452948216

I could get your test to run and fail on the assertion by inserting anyView() before vStack().

Note that to allow a view to update, that requires more tricks explained in the Inspection Guide: https://github.com/nalexn/ViewInspector/blob/0.10.2/guide.md#views-using-state-environment-or-environmentobject

jonreid avatar Feb 18 '25 00:02 jonreid

Thanks Jon!

azamsharpschool avatar Feb 20 '25 14:02 azamsharpschool

After inserting anyView() the Xcode 16.3 update now broke everything again... 🥲

domfz avatar Apr 01 '25 13:04 domfz

@domfz This is expected. You should be able to remove all of your implicitAnyView()'s, once updating to 16.3.

From the Xcode 16.3 release notes.

Fixed: SWIFT_ENABLE_OPAQUE_TYPE_ERASURE is now off by default for all debug builds. If you were previously turning it off in your project, you can remove that override. Previews now enables this selectively and recompiles the edited file with this setting only when running a preview. (140442875)

JOyo246 avatar Apr 01 '25 18:04 JOyo246