Queries match elements inside the <head /> part of the document
See original GitHub issuecypress-testing-library
version: 5.3.0
Relevant code or config
<html>
<head>
<title>Hello world</title>
</head>
<body>
<h1>Hello world</h1>
</body>
</html>
cy.findByText('Hello world')
What you did
Find an element by its text content.
What happened
Since the document title element in the head area also matches, the query tells me that it found more than one element with the expected text content.
Problem description
The queries are using the entire document as the container.
Suggested solution
Maybe the queries should use the body as the container. Elements in the head area cannot be interacted with and are generally not visible to the user. Except maybe for the title, but there should be more declarative ways to check for it than findByText
.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Document.querySelector() - Web APIs | MDN
Note: The matching is done using depth-first pre-order traversal of the document's nodes starting with the first element in the document's ...
Read more >The Head Element and Related Elements
Information in the HEAD element corresponds to the top part of a memo or mail message. It describes properties of the document such...
Read more >How To Access Elements in the DOM - DigitalOcean
With querySelector() , comma-separated values function as an OR operator. For example, querySelector('div, article') will match div or article , ...
Read more >Matching the first/nth element of a certain type in the entire ...
This means that :first-of-type is applied to the first element of its type relative to its parent and not the document's root (or...
Read more >Selecting content on a web page with XPath - Library Carpentry
$x(", This function tells the browser we want it to execute an XPath query. ; //, Look anywhere in the document… ; h2...
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
Thanks @pranjaljately!
This would be best:
That would work regardless of the level of heading. It’s also more semantically correct 🤘
Well… Maybe we should do this then: https://github.com/testing-library/dom-testing-library/issues/593