Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: Add support for querying shadow DOM #21233

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

martinpitt
Copy link
Member

@martinpitt martinpitt commented Nov 8, 2024

Add a querySelectorAll() variant which can transparently pierce and traverse shadow DOMs without having to know their structure (i.e. doing selectors piece-wise and iterating on their .shadowRoot attribute).

As this is more expensive than standard querySelector()`, only use it if the page actually contains any shadow root.

This makes it a lot more convenient to write tests for pages which use web components, like https://patternflyelements.org.


cockpit-project/starter-kit#1028 makes use of that.

TODO:

  • This makes queries much slower. Provide an opt-in.

@martinpitt martinpitt added the no-test For doc/workflow changes, or experiments which don't need a full CI run, label Nov 8, 2024
martinpitt added a commit to martinpitt/starter-kit that referenced this pull request Nov 8, 2024
PFE uses lit, so it makes sense to just use that for the main
application as well. Web components are pretty much React built into the
web platform, and lit adds some convenience around that.

This is mostly a demo -- for real Cockpit pages, PF Elements is still
missing too many components.

Install query-selector-shadow-dom to enable testlib's shiny new support
for traversing selectors through shadow DOMs from
cockpit-project/cockpit#21233
@martinpitt martinpitt requested review from jelly and removed request for jelly November 8, 2024 09:28
@martinpitt martinpitt added the blocked Don't land until something else happens first (see task list) label Nov 8, 2024
@martinpitt
Copy link
Member Author

martinpitt commented Nov 8, 2024

Apparently this does not work in Firefox

Nope, it's fine. It was just a packaging bug (forgot to include the new npm module into the tarball)

@martinpitt martinpitt marked this pull request as draft November 8, 2024 11:54
martinpitt added a commit to martinpitt/starter-kit that referenced this pull request Nov 8, 2024
PFE uses lit, so it makes sense to just use that for the main
application as well. Web components are pretty much React built into the
web platform, and lit adds some convenience around that.

This is mostly a demo -- for real Cockpit pages, PF Elements is still
missing too many components.

Install query-selector-shadow-dom to enable testlib's shiny new support
for traversing selectors through shadow DOMs from
cockpit-project/cockpit#21233
@martinpitt martinpitt removed the blocked Don't land until something else happens first (see task list) label Nov 8, 2024
@martinpitt martinpitt requested a review from jelly November 8, 2024 13:51
@martinpitt martinpitt marked this pull request as ready for review November 8, 2024 13:51
martinpitt added a commit to martinpitt/starter-kit that referenced this pull request Nov 11, 2024
PFE uses lit, so it makes sense to just use that for the main
application as well. Web components are pretty much React built into the
web platform, and lit adds some convenience around that.

This is mostly a demo -- for real Cockpit pages, PF Elements is still
missing too many components.

Install query-selector-shadow-dom to enable testlib's shiny new support
for traversing selectors through shadow DOMs from
cockpit-project/cockpit#21233
martinpitt added a commit to martinpitt/starter-kit that referenced this pull request Nov 11, 2024
PFE uses lit, so it makes sense to just use that for the main
application as well. Web components are pretty much React built into the
web platform, and lit adds some convenience around that.

This is mostly a demo -- for real Cockpit pages, PF Elements is still
missing too many components.

Install query-selector-shadow-dom to enable testlib's shiny new support
for traversing selectors through shadow DOMs from
cockpit-project/cockpit#21233
martinpitt added a commit to martinpitt/starter-kit that referenced this pull request Nov 12, 2024
PFE uses lit, so it makes sense to just use that for the main
application as well. Web components are pretty much React built into the
web platform, and lit adds some convenience around that.

This is mostly a demo -- for real Cockpit pages, PF Elements is still
missing too many components.

Install query-selector-shadow-dom to enable testlib's shiny new support
for traversing selectors through shadow DOMs from
cockpit-project/cockpit#21233
@martinpitt martinpitt removed the request for review from jelly November 12, 2024 07:06
@martinpitt martinpitt marked this pull request as draft November 12, 2024 07:26
@martinpitt martinpitt changed the title test: Add support for query-selector-shadow-dom test: Add support for querying shadow DOM Nov 12, 2024
Add a `querySelectorAll()` variant which can transparently pierce and
traverse shadow DOMs without having to know their structure (i.e. doing
selectors piece-wise and iterating on their `.shadowRoot` attribute).

As this is more expensive than standard querySelector()`, only  use it
if the page actually contains any shadow root.

This makes it a lot more convenient to write tests for pages which use
web components, like https://patternflyelements.org.
@martinpitt martinpitt marked this pull request as ready for review November 16, 2024 09:13
@martinpitt
Copy link
Member Author

The previous commit f11e9e8 always used this "deep" querySelector, and it worked. But I figure it'll be slower, so I reworked this to detect the presence of shadow DOMs, and only use the deep one if there are any.

I adjusted cockpit-project/starter-kit#1028 to pull in this new version.

/* Detect if we have any shadow DOM */
window.__haveShadowDom = function() {
if (window.__haveShadowDomResult === undefined)
window.__haveShadowDomResult = Array.from(document.querySelectorAll('*')).filter(el => el.shadowRoot).length > 0;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems pretty expensive? Likely it is more performance to write:

for (int x = 0 ; i < querySelector('*').length; i++ )
    if (el.shadowRoot)
         return true;

Alternatively:

let has_shadow_dom = false
document.querySelectorAll('*').forEach(element => {if (element.shadowRoot) { has_shadow_dom = true ;break; }})

Alternatively document.body.contains() would I suppose "work" but requires you to run it every time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-test For doc/workflow changes, or experiments which don't need a full CI run,
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants