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

cy.select() not working properly if index is used #24739

Closed
HeshamMoneer opened this issue Nov 19, 2022 · 4 comments · Fixed by #25016
Closed

cy.select() not working properly if index is used #24739

HeshamMoneer opened this issue Nov 19, 2022 · 4 comments · Fixed by #25016
Assignees
Labels
E2E Issue related to end-to-end testing

Comments

@HeshamMoneer
Copy link

Current behavior

<select data-test-id="scp_dropdown">
    <option value="[object Object]">AHA Bug bash</option>
    <option value="[object Object]">cairo</option>
    <option value="[object Object]">Frankfurt</option>
    <option value="[object Object]">cairo</option>
</select>

I am running this command on the HTML element described above

cy.get('[data-test-id="scp_dropdown"]').select(2);

The selected option should be "Frankfurt," however, "AHA Bug bash" is the selected option.

I believe this generally happens because of the value attribute that is identical for all options (because this problem does not happen when the value options are different). I, unfortunately, cannot change the values because they contain actual objects (that are interpreted in the HTML as the string "[object Object]").

I was hoping to select with an index value that won't be affected by the value attribute.

Desired behavior

The selected option should be "Frankfurt."

Test code to reproduce

cy.get('[data-test-id="scp_dropdown"]').select(2);

Cypress Version

10.7.0

Node version

12.22.9

Operating System

Ubuntu 22.04.1 LTS

Debug Logs

No response

Other

No response

@ryanthemanuel
Copy link
Collaborator

Hi @HeshamMoneer. Thank you for logging this issue. This does seem to be breaking but only when you have exact same values on each option. Are there any unique values on the objects that you are using, that can be used for the value property instead? ids or keys or something?

@HeshamMoneer
Copy link
Author

HeshamMoneer commented Nov 21, 2022

Hi @ryanthemanuel , I am afraid not. The only unique values they have are the objects they contain, and all these objects are converted unfortunately to an identical string in the HTML. I tried to overwrite select method as follows

Cypress.Commands.overwrite('select', (originalFn, subject, value, options) => {
  if(typeof value == 'number'){
    return cy.wrap(subject).children('option').eq(value).then(e => {
      return originalFn(subject, e.text().trim(), options);
    });
  }
  else{
    return originalFn(subject, value, options);
  }
});

and it is working perfectly in other cases. However, my new command fails in this case as well since I have 2 options with the same string value (cairo). So I was wondering maybe if there is another workaround or something to use the option index to actually select it?

@mjhenkes mjhenkes assigned mjhenkes and unassigned ryanthemanuel Nov 23, 2022
@nagash77 nagash77 assigned BlueWinds and unassigned mjhenkes Dec 6, 2022
@cypress-bot cypress-bot bot added the stage: needs review The PR code is done & tested, needs review label Dec 6, 2022
@BlueWinds
Copy link
Contributor

Tracked this down in our driver code - we have logic to force the proper element in some cases, but it wasn't triggering here. Fix in #25016.

@BlueWinds BlueWinds added the E2E Issue related to end-to-end testing label Dec 8, 2022
@ryanthemanuel ryanthemanuel added stage: pending release and removed stage: needs review The PR code is done & tested, needs review labels Dec 12, 2022
@cypress-bot
Copy link
Contributor

cypress-bot bot commented Dec 13, 2022

Released in 12.1.0.

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

@cypress-bot cypress-bot bot locked as resolved and limited conversation to collaborators Dec 13, 2022
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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants