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

Problem with cy.contains(). It find two elements. It should find one #25225

Closed
SkubisMilo opened this issue Dec 20, 2022 · 6 comments · Fixed by #25250
Closed

Problem with cy.contains(). It find two elements. It should find one #25225

SkubisMilo opened this issue Dec 20, 2022 · 6 comments · Fixed by #25250
Assignees
Labels
E2E Issue related to end-to-end testing type: bug type: regression A bug that didn't appear until a specific Cy version release

Comments

@SkubisMilo
Copy link

Current behavior

After update to Cypress 12.1.0 some tests stop working. The problem is with cy.contains().

Before this code: cy.dataCy("OpenAddFormsButton.label").contains("Zadanie").click(); found only one element. Now it finds two elements:
Screenshot_154

Desired behavior

In this situation it should find only element which contains only word "Zadanie" not "Zadanie Cykliczne" also.

Test code to reproduce

Screenshot_154

it("adds new task", () => {
cy.dataCy("OpenAddFormsButton.add").contains("Dodaj").click();
cy.dataCy("OpenAddFormsButton.label").contains("Zadanie").click();
});
Screenshot_155

Cypress Version

12.1.0

Node version

16.14.2

Operating System

Windows 11

Debug Logs

No response

Other

No response

@jmitchell-figure
Copy link

I have had to deal with contains finding multiple before so can't speak on why it was working for you previously, but a simple .first() or using regex will work https://docs.cypress.io/api/commands/contains#Syntax

@emilyrohrbough
Copy link
Member

emilyrohrbough commented Dec 20, 2022

@SkubisMilo This appears to be a regression from 12.0.1 to 12.0.2. I was able to reproduce with the following example:

    it('issue ', () => {
      cy.visit('https://example.cypress.io')
      cy.get('a').contains('next').click()
    })

In 12.0.1 - passes
In 12.0.2 - fails with cy.click() can only be called on a single element. Your subject contained 3 elements. Pass { multiple: true } if you want to serially click each element.[Learn more](https://on.cypress.io/click)

.contains() should be returning the first matched subject: https://docs.cypress.io/api/commands/contains#Only-the-first-matched-element-will-be-returned.

@emilyrohrbough emilyrohrbough added type: bug type: regression A bug that didn't appear until a specific Cy version release labels Dec 20, 2022
@emilyrohrbough emilyrohrbough added routed-to-e2e E2E Issue related to end-to-end testing labels Dec 21, 2022
@cypress-bot cypress-bot bot added the stage: needs review The PR code is done & tested, needs review label Dec 21, 2022
@BlueWinds
Copy link
Contributor

Tracked this down - basically we're running into a problem when .contains() is passed multiple subjects that match the filter (but no child element of any of the subjects does).

Opened a PR to fix it, should go out in the next patch release.

@SkubisMilo
Copy link
Author

@BlueWinds thanks for looking at this problem. I hope that your fix resolve this problem :).

@bondar-artem
Copy link

I have also had problems because of this issue. Hoping for the fix to be released soon.

@cypress-bot
Copy link
Contributor

cypress-bot bot commented Jan 3, 2023

Released in 12.3.0.

This comment thread has been locked. If you are still experiencing this issue after upgrading to
Cypress v12.3.0, please open a new issue.

@cypress-bot cypress-bot bot removed the stage: needs review The PR code is done & tested, needs review label Jan 3, 2023
@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Jan 3, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
E2E Issue related to end-to-end testing type: bug type: regression A bug that didn't appear until a specific Cy version release
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants