[Question] Custom Element Implementation
See original GitHub issueWhat is the best practice to implement a custom element. For example, we have a combobox that isn’t a normal html select option. There are certain workarounds for how to select an option.
I was thinking about implementing the ElementHandle
interface and passing in the page and the locator for the root element. Something like this:
public class CustomComboBox implements ElementHandle {
private ElementHandle elementHandle;
public CustomComboBox(Page page, String selector) {
this.elementHandle = page.querySelector(selector);
}
}
For all of the methods i would just pass on the method call to the elementHandle
. And for the selectOption
methods I would implement the custom logic needed to select an option in this custom combobox.
Is there a better way to do this?
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (3 by maintainers)
Top Results From Across the Web
Using custom elements - Web Components | MDN
To register a custom element on the page, you use the CustomElementRegistry.define() method. This takes as its arguments:.
Read more >Custom Elements v1 - Reusable Web Components
Custom elements allow web developers to define new HTML tags, extend existing ones, and create reusable web components.
Read more >html - What is the minimal implementation for custom elements ...
Always test your Web Component with code that defines the Custom Element BEFORE it is used in the DOM. A Real World <flag-icon>...
Read more >Custom Elements
The custom elements implementation is split between core/dom and bindings/core/v8. Design. Some Important Classes. CustomElementDefinition.
Read more >Your First Custom Element | DigitalOcean
We register (define) the custom element with customElement.define and passing-in the tag name as the first argument and then the class that ...
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
First of all, all interfaces in the API except for callbacks are expected to be implemented only by Playwright. You should consider them as
final
classes (e.g. one shouldn’t try to implementPage
interface). We use interfaces for the API definition primarily for documentation purposes and to hide implementation noise from the clients eyes. Furthermore, Playwright code makes certain assumptions about the actual implementation of the interfaces and if you provide your own class implementingElementHandle
and then try to pass it e.g. as an argument intoPage.evaluate
it will be serialized as a JSON object rather than as an element handle which is not what you want.Can you elaborate on why do you need to implement
ElementHandle
rather than just wrap it into your ownCustomComboBox
class and delegating some of the calls to the corresponding handle?If you want to have generic code that works on collections of element handles and custom elements it might be easier to have your own hierarchy that either delegates directly to the wrapped handle or does some tricky logic and calls into playwright when needed. I’d like to better understand you constraints.
You need to know the Frame in which to lookup the other element, then you can get access to that frame e.g. via
Page.frame(name)
and callframe.waitForSelector
on it.