You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hey qk5! We've discussed this in our team sync yesterday. The fact that by default, getByText selects not just visible elements but also invisible ones is a design regret of ours. All the RTL-Style selectors (think getByText, getByAltText, getByRole) are modeled after human perception, and that means that it's surprising for it to include hidden elements. So if we could go back in time, we would change the defaults so that getByText only selects visible elements.
We can't though, and we'd prefer not to introduce breaking changes in such a fundamental part of our API. Even if we made this behaviour a config toggle, we'd be forking our userbase into two fundamentally different behaviours, which would lead to even more confusion. Really bad, especially when you consider that libraries have no way of detecting it.
Now, the visible option you propose is an interesting way out of this! Before we think about implementing it though, we'd like to understand how big this problem really is. I'll label it "P3: Collecting Feedback" and then we can collect some upvotes on the issue. Hopefully that gives us some perspective on the gravitas of this, and sleep a couple of nights about other solutions to the problem.
Also, an interesting observation in API design: in getByText("...", { visible: true }), the visible option is trimodal! With the defaults we have, { visible: false } clearly means "only select invisible", { visible: true } means "only select visible", and { visible: undefined } means "do the default", which is selecting both visible and invisible elements.
I want to chime in that a workaround for this is to use getByRole instead of getByText, as we can exploit the fact that invisible elements do not have a role:
when visible
when hidden
Maybe the documentation can address getByText’s quirk (of also selecting visible elements) to nudge people into using getByRole more.
We can't though, and we'd prefer not to introduce breaking changes in such a fundamental part of our API. Even if we made this behaviour a config toggle, we'd be forking our userbase into two fundamentally different behaviours, which would lead to even more confusion. Really bad, especially when you consider that libraries have no way of detecting it.
Simon Knott (@Skn0tt) For testing a react-native-web applications this is really important because the routing works by hiding the pages and stack them one on another. Then selecting base things like navigation can lead to multiple elements found in all tests. Adding a global option to ignore invisible/hidden elements would be really a great help here so we don't have to add .locator('visible=true') to every selector.
The package react-native-testing-library changed the default behavior to exclude elements hidden from accessibility by default so maybe you can inspire or talk to them about this. It was a big help for our tests.
Reacted by Niklas Machacek, qk5, thalmann-christof, Edgar, Jack Tomaszewski and Vivek Raj Sethi
I was surprised when getByText locators returned invisible elements. I would recommend adding a warning to the docsite to mention that scenario as it's not obvious, and I found this thread only by chance. { visible: true } is an option, but I would give my vote to have this as the default in a major release.
Reacted by Stefan Judis, Joshua J. and Jack Tomaszewski
We can't though, and we'd prefer not to introduce breaking changes in such a fundamental part of our API. Even if we made this behaviour a config toggle, we'd be forking our userbase into two fundamentally different behaviours, which would lead to even more confusion. Really bad, especially when you consider that libraries have no way of detecting it.
I was thinking about this more as it is still very annoying for our tests. I understand this is a breaking change but I believe this is a good one. Considering people are currently probably testing with selectors catching on hidden elements they would actually catch bugs in their code after a break change. I can imagine someone not practicing TDD and not seeing red test in the first place writing a green test assuming everything is working when the actual user would not be able to do an action because of some hidden element. So it's more like fixing an important bug in the testing framework :) Also, for people whose tests are properly written nothing would break because they are already selecting visible elements.
In conclusion, this is only a breaking change for people with false-green tests and by updating they catch bugs in their app. Also, if that helps, it is possible without hard breaking change. With config flag and a warning, everyone can migrate smoothly, fix this issue with config and then migrate on next version with turned this by default.
Reacted by Stefan Judis, Jack Tomaszewski and Beto Frega
I believe this is not closable by the mentioned change. It is still required to pollute all tests with await locator.filter({ visible: true }) but this issue is about having global option applicable to all tests automatically. Is that somehow possible now?
Activity
qk5 commentedon Jul 24, 2024
Update: Upon reading more on the playwright documentation, I found out the following also works:
page.getByText('Waiting for response...', { exact: true }).locator('visible=true');But I still think if we can expose visible as an option, it will look cleaner. Please feel free to correct me if I am wrong.
Thank you.
Skn0tt commentedon Jul 31, 2024
Hey qk5! We've discussed this in our team sync yesterday. The fact that by default,
getByTextselects not just visible elements but also invisible ones is a design regret of ours. All the RTL-Style selectors (thinkgetByText,getByAltText,getByRole) are modeled after human perception, and that means that it's surprising for it to include hidden elements. So if we could go back in time, we would change the defaults so thatgetByTextonly selects visible elements.We can't though, and we'd prefer not to introduce breaking changes in such a fundamental part of our API. Even if we made this behaviour a config toggle, we'd be forking our userbase into two fundamentally different behaviours, which would lead to even more confusion. Really bad, especially when you consider that libraries have no way of detecting it.
Now, the
visibleoption you propose is an interesting way out of this! Before we think about implementing it though, we'd like to understand how big this problem really is. I'll label it "P3: Collecting Feedback" and then we can collect some upvotes on the issue. Hopefully that gives us some perspective on the gravitas of this, and sleep a couple of nights about other solutions to the problem.Skn0tt commentedon Jul 31, 2024
Also, an interesting observation in API design: in
getByText("...", { visible: true }), thevisibleoption is trimodal! With the defaults we have,{ visible: false }clearly means "only select invisible",{ visible: true }means "only select visible", and{ visible: undefined }means "do the default", which is selecting both visible and invisible elements.dtinth commentedon Aug 22, 2024
I want to chime in that a workaround for this is to use
getByRoleinstead ofgetByText, as we can exploit the fact that invisible elements do not have a role:Maybe the documentation can address
getByText’s quirk (of also selecting visible elements) to nudge people into usinggetByRolemore.miso-belica commentedon Oct 4, 2024
Simon Knott (@Skn0tt) For testing a react-native-web applications this is really important because the routing works by hiding the pages and stack them one on another. Then selecting base things like navigation can lead to multiple elements found in all tests. Adding a global option to ignore invisible/hidden elements would be really a great help here so we don't have to add
.locator('visible=true')to every selector.The package react-native-testing-library changed the default behavior to exclude elements hidden from accessibility by default so maybe you can inspire or talk to them about this. It was a big help for our tests.
grundmanise commentedon Jan 6, 2025
I was surprised when
getByTextlocators returned invisible elements. I would recommend adding a warning to the docsite to mention that scenario as it's not obvious, and I found this thread only by chance.{ visible: true }is an option, but I would give my vote to have this as the default in a major release.miso-belica commentedon Jan 7, 2025
I was thinking about this more as it is still very annoying for our tests. I understand this is a breaking change but I believe this is a good one. Considering people are currently probably testing with selectors catching on hidden elements they would actually catch bugs in their code after a break change. I can imagine someone not practicing TDD and not seeing red test in the first place writing a green test assuming everything is working when the actual user would not be able to do an action because of some hidden element. So it's more like fixing an important bug in the testing framework :) Also, for people whose tests are properly written nothing would break because they are already selecting visible elements.
In conclusion, this is only a breaking change for people with false-green tests and by updating they catch bugs in their app. Also, if that helps, it is possible without hard breaking change. With config flag and a warning, everyone can migrate smoothly, fix this issue with config and then migrate on next version with turned this by default.
dtinth commentedon Mar 20, 2025
Playwright since v1.51.0 now allows filtering an existing locator for visible ones.
miso-belica commentedon Mar 20, 2025
#31840 (comment)
I believe this is not closable by the mentioned change. It is still required to pollute all tests with
await locator.filter({ visible: true })but this issue is about having global option applicable to all tests automatically. Is that somehow possible now?dtinth commentedon Mar 20, 2025
Mišo Belica (@miso-belica) The issue description asks for the
visibleoption for individual locators, and did not mention a global option.miso-belica commentedon Mar 20, 2025
I know, even though the discussion shifted a bit. But fair enough. I have created a new issue #35294
dgozman commentedon Mar 20, 2025
As Thai Pangsakulyanont (@dtinth) points out, this one was implemented in v1.50. See docs here.