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

.contains() only yields first match #1009

Closed
fineline opened this issue Dec 5, 2017 · 10 comments
Closed

.contains() only yields first match #1009

fineline opened this issue Dec 5, 2017 · 10 comments
Labels
stage: wontfix Cypress does not regard this as an issue or will not implement this feature

Comments

@fineline
Copy link

fineline commented Dec 5, 2017

  • Operating System: Mac 10.12.6
  • Cypress Version: 1.1.2
  • Browser Version: Chrome 62.0.3202.94

Is this a Feature or Bug?

BUG

How to reproduce:

In a page with multiple spans containing the text "Test", issue the following:

cy.contains("Test").then((results) => {
  debugger
})

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.

@fineline fineline changed the title .contains() only returns first match .contains() only yields first match Dec 5, 2017
@jennifer-shehane
Copy link
Member

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:

.contains() yields the new DOM element it found.

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.

@brian-mann
Copy link
Member

All of the rest of the contains documentation states everywhere that it yields the first element. I guess you pointed out the one place where it conflicts with that. It's because those sections are reusable templates.

@brian-mann
Copy link
Member

Closing in favor of cypress-io/cypress-documentation#306

@fineline
Copy link
Author

fineline commented Dec 5, 2017

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.

@brian-mann
Copy link
Member

You could probably just do... cy.get('div:contains(bar)')

@0xIslamTaha
Copy link

I can still see that the docs say yield firs element
image

@obedparla
Copy link

That's correct @0xIslamTaha, it only yields the first element.

@jennifer-shehane jennifer-shehane added the stage: wontfix Cypress does not regard this as an issue or will not implement this feature label Dec 30, 2020
@sijakret
Copy link

You could probably just do... cy.get('div:contains(bar)')

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?

@theoturner
Copy link

theoturner commented May 26, 2021

cy.get('div:contains(mystr)') does only yield the first result, however you can get around this if you're matching on two different element types, e.g. if one instance is a div containing mystr and one is a p containing mystr, you can use:

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 .eq(i) as with the rest of Cypress should be default behaviour, but it is not the case yet.

@OlsonDev
Copy link

I can't speak to cy.get('div:contains(mystr)') only yielding the first result nowadays, but I suspect that's not true considering this worked for me:

cy.get('[data-test="agreement-status-text"]:contains("Active")').should('have.length', 4)

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stage: wontfix Cypress does not regard this as an issue or will not implement this feature
Projects
None yet
Development

No branches or pull requests

8 participants