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.

[Question] Custom Element Implementation

See original GitHub issue

What 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:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
yury-scommented, Mar 22, 2021

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 implement Page 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 implementing ElementHandle and then try to pass it e.g. as an argument into Page.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 own CustomComboBox class and delegating some of the calls to the corresponding handle?

public class CustomComboBox {
  private ElementHandle elementHandle;

  public CustomComboBox(Page page, String selector) {
    this.elementHandle = page.querySelector(selector);
  }

  public select(String value) {
    // some logic actually selecting the value
  }
}

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.

0reactions
yury-scommented, Mar 26, 2021

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 call frame.waitForSelector on it.

Read more comments on GitHub >

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

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