question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Feature] Make it easier to reuse locators for chaining

See original GitHub issue

Let 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:

  1. Allow the first arg of page.locator() to be either a selector: string OR a locator: Locator --> this.parent.locator(this.child)
  2. Be able to get the selector from a locator --> this.parent.locator(this.child.selector)

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:5
  • Comments:7

github_iconTop GitHub Comments

2reactions
kenny-fcommented, Apr 21, 2022

Does it make sense ? Sorry if I am confused.

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.

1reaction
imartinflorescommented, Apr 21, 2022

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:

class PageObject {
  readonly parent: Locator;
  readonly childSelector: String;
  readonly child: Locator;

  constructor(page: Page) {
    this.childSelector = "yourLocator";
    this.parent = page.locator('[data-id=parent]');
    this.child = page.locator(`${this.childSelector}`);
  }
}

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found