Yesterday, I wrote about how to test custom events in Playwright. In building out the tests for Kelp's web components, I ran into an interesting challenge on the Today, I wanted to explain the problem, and how I fixed it. Let's dig in! The challengeKelp uses an HTML Web Component approach for most of it's components, but there are a few exceptions where that doesn't make sense. For example, the
Typically in Playwright, you…
That works great for HTML Web Components, but fails on step 2 for JS-rendered ones like Timing issuesStep 2—wait for it to be found/loaded—doesn't run until the element has a visible bounding rectangle. Because custom elements are treated like a By the time Playwright recognizes it—after the content has been rendered—the event has already fired, and the event listener/promise never resolves. The test fails. There's a simple fix!One of the things I love about Playwright is that it tests real HTML files. I'm using it for component testing rather than end-to-end, so each test spec has a corresponding HTML file with the markup hard-coded into it. I like this a lot better than having to dynamically inject HTML with JS for testing! In the test HTML, I add an event listener for the When the event fires, I log
Over in my test file, I…
Is it silly? Yes! Does it work? Also yes! This wouldn't be a great solution in an end-to-end test, where you'd be logging this stuff in the actual app. But for unit/component testing, it works great! Like this? A Go Make Things membership is the best way to support my work and help me create more free content. Cheers, Want to share this with others or read it later? View it in a browser. |
0 Komentar untuk "[Go Make Things] Catching early DOM events in Playwright"