Rayrun
← Back to Discord Forum

`instanceof` Fails in Playwright Because Class Info Is Lost

xxx_mlgnoob_xxxposted in #help-playwright
Open in Discord
xxx_mlgnoob_xxx
xxx_mlgnoob_xxx

I have an application where I'm using Web Components. In one specific area, I'm running a test to make sure that the element which I'm working with is the correct type. I do this by running

if (element instanceof MyCustomElement) {
  // ...
}

This works perfectly fine with vite. But when I run the Playwright tests, this logic fails because the instanceof test fails. Apparently, Playwright (or something else) is transforming class MyCustomElement extends HTMLElement { } into function HTMLElement { (native code) }, and this seems to be the reason for the failure.

Any ideas on what's causing this? And ideas on how to just work with the class? I don't want my JS getting transpiled.

This thread is trying to answer question "Why does the `instanceof` test fail in Playwright when used with Web Components and how can it be resolved?"

2 replies
xxx_mlgnoob_xxx
xxx_mlgnoob_xxx

To my surprise, this seems to be user error. Will investigate.

xxx_mlgnoob_xxx
xxx_mlgnoob_xxx

The issue was that the custom element was not upgraded by the time I did the check. So I had to manually call customElements.upgrade to get the job done. This is resolved now, though it's unrelated to playwright.

See: https://developer.mozilla.org/en-US/docs/Web/API/CustomElementRegistry/upgrade

Related Discord Threads

TwitterGitHubLinkedIn
AboutQuestionsDiscord ForumBrowser ExtensionTagsQA Jobs

Rayrun is a community for QA engineers. I am constantly looking for new ways to add value to people learning Playwright and other browser automation frameworks. If you have feedback, email [email protected].