[Feature] Make it easier to reuse locators for chaining
See original GitHub issueLet us know what functionality you’d like to see in Playwright and what your use case is.
Let’s say I have a page object like the one below
class PageObject {
readonly parent: Locator;
readonly child: Locator;
constructor(page: Page) {
this.parent = page.locator('[data-id=parent]');
this.child = page.locator('[data-id=child]');
}
}
It makes sense in my use case to have these as separate locators.
But in some instances I would essentially want to chain the parent locator to the child locator.
I could add another locator this.parentChild = page.locator('[data-id=parent] [data-id=child]')
but it seems like duplication of selectors that are already defined.
Or I could extract out the selectors but that again is more code.
Some ideas are:
- Allow the first arg of page.locator() to be either a
selector: string
OR alocator: Locator
-->this.parent.locator(this.child)
- Be able to get the selector from a
locator
-->this.parent.locator(this.child.selector)
Issue Analytics
- State:
- Created a year ago
- Reactions:5
- Comments:7
Top Results From Across the Web
Chaining Custom Locators
Here I'll look at how to chain custom locators. ... The lack of reusable content makes updating, maintaining and extending that much more ......
Read more >Effective Locator Strategies in Appium
Learn about different locator strategies supported by the Appium framework for locating web elements in automated app testing.
Read more >Robust locator strategy: custom attributes for test automation
Add custom attributes for test automation directly into your front-end code. This way reduces test brittleness and simplifies test ...
Read more >Stop using Page Objects and Start using App Actions
Page objects 1, 2 are intended to make end-to-end tests readable and easy to maintain. Instead of ad-hoc interactions with a page, ...
Read more >What's the Difference Between ">", ">>" and " " Separators in ...
The double >> allows you to chain different Playwright selectors. Chaining Selectors. This is specific to Playwright. p > span >> text=foo ,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yes, both your proposed solutions make sense and I have considered them.
Maybe I wasn’t clear enough as to why I’m requesting this feature. It’s not that there is currently anything blocking me from achieving my use case.
It’s more of a convenience thing. It would be good if you could get away with only having locators in the page object instead of having a mix of locators and string selectors. Keeps the class smaller and cleaner IMO.
Feel free to close if you don’t think this is something that is worth pursuing.
It’s alright. In that case, could you not use a string instead of a Locator object ? So you can create your selector, and then chain it or not depending if it’s under the parent, i.e:
And then if it’s under, you can use:
this.parent.locator(this.childSelector);
The childSelector could also be a const instead, I assume you won’t need to update it.
Does it make sense ? Sorry if I am confused.