-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
.contains() only yields first match #1009
Comments
This is in fact an error with the documentation. .contains() always yields the first element containing the text (also with regard taken to find relevant elements over deepest). The yields statement should be updated to:
If you have a particular use case for having a command in Cypress that yields multiple elements with the containing text, please specify an example and your need so that we can consider adding it as a new feature. |
All of the rest of the |
Closing in favor of cypress-io/cypress-documentation#306 |
Thanks for correcting the doc. Didn't see this mentioned elsewhere - when trying to figure out what the promise yields, the Yields section is where I looked. My use case is to click through items on a list deleting any previous test entries, which are identifiable by a match on a text pattern. I have now implemented this by getting all items with a cy.get() and then iterating through them with .each() and testing their text with if ($li.text().match(/MyPattern/)) doStuff() Not quite as concise as a .contains() but does the trick. |
You could probably just do... |
That's correct @0xIslamTaha, it only yields the first element. |
not for me! <div>a</div>
<div>a</div> cy.get('div') // -> both
cy.get('div:contains(a)') // -> only first wont fix => there is no supported path to do this, right? |
cy.get('div:contains(mystr)') // First result in a div
cy.get('p:contains(mystr)') // First result in a p Alternatively you can match on two different classes: cy.get('.class1').contains('mystr') // First result in class1
cy.get('.class2').contains('mystr') // First result in class2 Multiple results with access via |
Is this a Feature or Bug?
BUG
How to reproduce:
In a page with multiple spans containing the text "Test", issue the following:
When the dev tools stops at the debugger line, inspect results.
Current behavior:
It contains a single element.
Desired behavior:
Documentation here:
https://docs.cypress.io/api/commands/contains.html#Content
states:
.contains() yields the new DOM element(s) it found.
It seems it only yields a single element, but should yield them all.
The text was updated successfully, but these errors were encountered: