Preface
When performing software testing — for example, on an e-commerce website — have you ever run into the following problems?
- Dynamically loaded product lists: some products are hidden because they are out of stock, a promotion has ended, or the user is not logged in.
- Promotion labels shown on hover: discount information only appears when the user moves the mouse over an element.
- Interference from ad pop-ups: some pop-ups may obscure core functionality, yet during testing you still need to make sure the main flow is unaffected.
What these scenarios have in common is this: a page element may exist in the DOM but still be invisible to the user, for instance when CSS properties (display: none, visibility: hidden) are set. Traditional testing approaches require manually determining element visibility, iterating over elements or writing branch checks against specific objects in advance, which is fairly complicated.
The Latest Playwright Version Offers a Simpler Approach
In the most recent Playwright V1.5.1 update, a visible option was added to the locator.filter() method, which can greatly simplify this kind of operation. (At present this update is available for Node.js; the Python language does not support it yet.)
Its core purpose is:
Filter only the elements that are actually visible to the user on the page, automatically ignoring elements hidden by CSS (such as display: none) or by layout reasons.
Technical principle:
Playwright determines whether an element lies within the viewport and is visible to the user by computing properties such as the element’s layout bounds, opacity, and stacking context. This is fully consistent with a real user’s perspective and avoids test failures caused by operating on hidden elements.
Hands-on Scenario - Filtering Visible Products
Suppose we are testing the “Flash Sale” page of a Chinese e-commerce platform, whose HTML structure is as follows:
| |
Requirement Scenario
When a user visits the “Flash Sale” page, only the products available for purchase are shown (the first two items), while sold-out or not-yet-started products are hidden. We need to verify:
- Whether the number of products visible to the user on the page is correct.
- The “Buy Now” button only takes effect for visible products.
Comparison of Old and New Approaches
Old method - manual iteration + visibility checks
| |
Pain points:
- You have to manually iterate over all elements, which makes the code redundant.
- If the number of products changes dynamically (e.g., loaded via pagination), maintenance costs are high.
New approach - filter visible elements in one line of code
| |
Advantages:
- Concise code: no loops are needed; filtering is done directly through chained calls.
- Real-time matching: automatically responds to dynamic page changes (such as AJAX loading new products).
- Precise assertions: combined with Playwright’s Soft Assertions, multiple conditions can be verified at the same time.
Extended Applications - Common Scenarios in E-commerce Testing
Scenario 1 - Verifying promotion labels shown on hover
Some products’ “limited-time discount” labels only appear on mouse hover. Using visible: true ensures that only actually visible elements are operated on:
| |
Scenario 2 - Filtering out pop-up interference
If the page has ad pop-ups, you can use visible: true to skip hidden pop-up elements:
| |
Conclusion
With this practical Playwright update, we can considerably reduce the complexity of the code we write when building test automation. It also shows that, as a new-generation web testing tool gaining strong momentum, Playwright’s continuous upgrades and optimizations are very much grounded in real-world needs, backed by an active community, and well worth watching closely!
Qiucao has also launched a hands-on course on an automation framework built on Pytest + Playwright. If you are interested, feel free to follow along.
