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] extend Locator API with helper methods

See original GitHub issue

As an example, I would like to introduce findByRole method that is equivalent to:

page.locator(
  `role=${role}[name="${name}"]`
)

I have figured out how to add this to Page object,

https://github.com/microsoft/playwright/issues/1604#issuecomment-1224950604

This allows me to write code like:

await page.findByRole('link', 'Services').click();

However, this breaks when chaining Locator instances, e.g.

await page
  .locator('role=dialog')
-  .locator('role=link[name="Create A Service"]')
+  .findByRole('link', 'Create A Service')
  .click();

The above is throwing an error since .locator('role=dialog') is returning an instance of a Locator that is not aware of findByRole.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
yury-scommented, Aug 23, 2022

@gajus do you have a particular API in mind that Playwright could expose to support such extensibility?

You can already wrap a locator in another object with the extended interface similar to how you did with the page (you’d need to override Page.locator to return a wrapper instead of the original locator). You’d also need to proxy all locator methods and also ensure that the methods that return a new instance of Locator also return wrapped objects. It will not work with the playwright API methods that accept Locator as argument (e…g. has option) so they’d have to be unwrapped before being passed into the method.

Related issue https://github.com/microsoft/playwright/issues/13517

0reactions
pavelfeldmancommented, Nov 5, 2022

Closing since we’ve introduced typed locators like getByRole.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Shared classes - using the Java Helper API - IBM
The Helper API provides a set of flexible Java interfaces so that Java class loaders to use the shared classes features in the...
Read more >
What's the correct way to use helpers to extend functionality in ...
What i want is to group the methods and properties into this helpers, and what i want is something like: class Product(models.
Read more >
Useful JavaScript helper functions - Optimizely for Web
This function sets a cookie and accepts the cookie's name, value, domain, and duration in days as arguments. JavaScript. /* * Usage *...
Read more >
Helper - 4.8 - Ember API Documentation
Ember Helpers are functions that can compute values, and are used in templates. ... Helpers defined using a class must provide a compute...
Read more >
VS Code API | Visual Studio Code Extension API
The document link provider defines the contract between extensions and feature of showing links in the editor. Methods. provideDocumentLinks(document: ...
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