microsoft / playwright

Playwright is a framework for Web Testing and Automation. It allows testing Chromium, Firefox and WebKit with a single API.
https://playwright.dev
Apache License 2.0
67.11k stars 3.69k forks source link

[Feature]: Provide ability to find parent and sibling locators #33704

Closed Makavelic closed 23 hours ago

Makavelic commented 1 day ago

🚀 Feature Request

Given I have a locator, I want the ability to find sibling or parent locators. Some of our locators are really complex and require work to match them in the first place. For example let’s say I can’t use the standard locator.locator searching because I need to use locator.evaluate to find the one where clientHeight > 10. This isn’t a real example but I just want to demonstrate that there might be cases where you can’t just simply chain locators.

Once the locator is found it’s useful to be able to find its container locator so follow up locators can be scoped to it.

The sibling case doesn’t happen often but I’ve seen cases where there’s some sort of tree structure in the html but child nodes are actually siblings and not children. So in order to find child nodes I need a way to start my search at the initial node and from there find its siblings.

I’ve looked into the filter({has functionality but even if I switched my approach to use it, it wouldn’t work because our page classes often need to work in an iframe and non iframe context and filter does not support frame locators.

Example

An example would be

Page.locator(“someLocator”).parent(“other locator”) where parent takes a string similar to what locator accepts (css selector or xpath).

Replace parent with prevSibling, nextSibling for the sibling cases.

Motivation

This will make migrating from Testcafe a lot easiar but also I think it provides more flexibility in general.

dgozman commented 23 hours ago

Thank you for the issue! Closing as a duplicate of #23718.