Skip to content

[Feature]: all locators only select visible elements by default #35294

@miso-belica

Description

@miso-belica

🚀 Feature Request

Please add an option or other possibility to set all locators to ignore invisible elements by default. Currently this is not possible and people have to add .locator('visible=true') or newly introduced .filter({ visible: true }) everywhere which is easy to forget and pollutes the tests.

Consider making it a default behavior in the future versions.

Example

This would introduce a new option in the Playwright config selectInvisibleElements: boolean (or similar with better name) to allow locators select invisible elements by default or not as discussed in #31840.

As stated in #31840 (comment) this may be trimodal option but when using it as option in the config I believe 2 options have more sense.

Motivation

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 .filter({ 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.

I understand this may be seen as 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions