-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
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
[Select] Targeting dropdown items with Cypress using data-testid
when closed
#44939
Comments
@NhutMinhIT Have you tried this? https://stackoverflow.com/a/70958592 |
data-testid
when closed
@mj12albert Components like I think, this problem need improve. I dont't confirm it's bug. Thanks you very much ! |
Yes - this is expected, because the select menu is not rendered to the DOM at all before it's opened
Does the SO suggestion of |
The |
I think you need to add an extra step to explicitly open the select menu before selecting an item |
@mj12albert THANK YOU ! |
@NhutMinhIT Are you able to share a minimal repro (e.g. a repo, or CodeSandbox, though separate repo is probably easier)? It's hard to go by just screenshots In your screenshot, I think this line is wrong, cy.get('[data-testid="role"]', { withinSubject: null }).should('exist'); I think what needs to be done is something like:
|
Steps to reproduce
Steps to reproduce the issue
Open a live example:
Go to the Material-UI docs page with a live example of the Select component:
Material-UI Select Component
Inspect the DOM structure:
Open the browser's Developer Tools (
F12
orCtrl+Shift+I
).Locate the
<Select />
dropdown in the DOM. You'll notice that the dropdown items (<MenuItem />
) are not displayed inside the same parent as the<Select />
component when the dropdown is closed.Click the dropdown to open it.
Observe that the dropdown is displayed in a different part of the DOM, usually a sibling element to the root element (
<body>
), due to the use of portal.data-testid
attribute:Use Cypress or another testing tool to target the dropdown items with
data-testid
.If the dropdown item is not open, the items will be inaccessible, meaning that cypess has not yet recognized the data-testid for the Select element.
Current behavior
Dropdown
<Select />
Behavior in TestingThe
<Select />
dropdown component includes both anid
and adata-testid
, allowing you to use either for testing. However, there are cases wheredata-testid
may not work as expected. Below are some common scenarios to consider:Issues with
data-testid
Dropdown Not Rendered Yet
If the dropdown has not been rendered or if there are changes in the DOM structure, the
data-testid
attribute might not be accessible.UI Libraries and DOM Structure
Some UI libraries (e.g., Material-UI) render the dropdown menu in a different location in the DOM when the dropdown is opened. As a result:
data-testid="role"
, the dropdown items (<MenuItem />
) will not be accessible unless the dropdown is opened.Expected behavior
Recommendations for Testing
To handle these scenarios effectively when using Cypress or other testing tools:
Ensure Dropdown Is Open
Before interacting with dropdown items, make sure the dropdown is opened using a method like
.click()
or.focus()
on the trigger element.Use Stable Selectors (using
id
)Prefer
id
attributes if they remain static across renders. Whiledata-testid
is convenient, it might not always work as intended in dynamic UI libraries.Inspect DOM Behavior
Check the DOM structure when the dropdown is open to ensure you are targeting the correct elements.
Context
Component with MUI
<Select>
IntegrationThis component demonstrates the usage of Material UI's
<Select>
component for selecting a role in a form. It is designed to handle form validation and updates based on user interactions.Component Code Example
The text was updated successfully, but these errors were encountered: