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.

Queries match elements inside the <head /> part of the document

See original GitHub issue
  • cypress-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:open
  • Created 3 years ago
  • Reactions:2
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
kentcdoddscommented, May 29, 2020

Thanks @pranjaljately!

This would be best:

cy.findByRole('heading', {name: /hello world/i }).should('exist')

That would work regardless of the level of heading. It’s also more semantically correct 🤘

0reactions
kentcdoddscommented, May 29, 2020
Read more comments on GitHub >

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

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