[Feature] extend Locator API with helper methods
See original GitHub issueAs 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:
- Created a year ago
- Comments:6 (3 by maintainers)
Top 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 >
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

@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
Closing since we’ve introduced typed locators like getByRole.