nalexn / ViewInspector

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

Does find(button: "xyz") find custom button `label:`s? #295

Closed sureshjoshi closed 4 months ago

sureshjoshi commented 4 months ago

I've got a deeply nested button (too far down to manually get to it), that I'm trying to access as follows:

try sut.inspect().find(button: "New XYZ")

However, this fails to match. However, if I do a findAll(ViewType.Button.self), I correctly get the two buttons on the page.

This is the testing API I'm using now, but in order to make my test less brittle to which button comes first in the list, I'd like to access the label, but I can't seem to grab a labelView off of the button...

Does this find(button: API specifically expect a top-level Text inside?

nalexn commented 4 months ago

Hey, your expectation about find(button: "New XYZ") is correct, most likely the text you're looking for is different (do you have localization?)

There is a way to quickly find out the text used as the label for your buttons: after you locate them with findAll, call .labelView().text().string() on each

sureshjoshi commented 4 months ago

Thanks for the quick reply! I was looking into it, and I think I assumed find(button would be a full-depth search for a Text, rather than top-level.

The buttons I'm using are very slightly customized Labels, but that's enough to cause a problem with the API.

What I'm using instead is a utility which emulates the functionality I need.

Thanks!

nalexn commented 4 months ago

If you don't mind, could you share a snippet of the button and the custom label? find(button is supposed to do a deep search of text (you can check the implementation of the function), so I'd want to make sure there is no issue in the lib

sureshjoshi commented 4 months ago
        } label: {
            ZStack {
                self.label()
                    .opacity(phase == .idle ? 1 : 0)
            }.overlay {

where, label is a Text