0
0
Cypresstesting~15 mins

cy.select() for dropdowns in Cypress - Deep Dive

Choose your learning style9 modes available
Overview - cy.select() for dropdowns
What is it?
cy.select() is a command in Cypress used to interact with dropdown menus on web pages. It allows you to choose an option from a dropdown list by specifying the visible text, value, or index. This helps automate testing of user selections in forms or filters. It works only with HTML tags) work. After mastering cy.select(), you can move on to testing more complex UI interactions like custom dropdowns, chained selects, or verifying dynamic content changes after selection.
Mental Model
Core Idea
cy.select() lets you pick an option from a dropdown by telling Cypress exactly which choice to make, mimicking how a user clicks and selects.
Think of it like...
It's like choosing a flavor from an ice cream menu by pointing to the name or number on the list, and the server scoops that flavor for you.
Dropdown <select> element
┌───────────────┐
│ Select fruit: │
│ ┌───────────┐ │
│ │ Apple     │ │  <-- cy.select('Apple') picks this
│ │ Banana    │ │
│ │ Cherry    │ │
│ └───────────┘ │
└───────────────┘
Build-Up - 7 Steps
1
FoundationUnderstanding HTML dropdown basics
🤔
Concept: Learn what an HTML dropdown ( tag with multiple
Result
You understand the structure and how browsers show dropdowns with options.
Knowing the HTML structure helps you target dropdowns correctly in tests and understand what cy.select() interacts with.
2
FoundationBasic cy.select() usage
🤔
Concept: Learn how to use cy.select() to pick an option by visible text.
To select 'Banana' from the dropdown: cy.get('#fruits').select('Banana') This finds the dropdown by id and selects the option with visible text 'Banana'.
Result
The dropdown changes to show 'Banana' as the selected option.
cy.select() mimics user selection simply by specifying the visible text, making tests readable and straightforward.
3
IntermediateSelecting by value or index
🤔Before reading on: do you think cy.select() can pick options by their value attribute or position number? Commit to your answer.
Concept: cy.select() can also select options by their value attribute or by index number, not just visible text.
Using the same dropdown: - Select by value: cy.get('#fruits').select('banana') // value attribute - Select by index (0-based): cy.get('#fruits').select(2) // selects 'Cherry' (third option) This flexibility helps when visible text changes or is dynamic.
Result
The dropdown updates to the chosen option based on value or index.
Knowing multiple ways to select options makes tests more robust and adaptable to different HTML setups.
4
IntermediateHandling multiple selections
🤔Before reading on: do you think cy.select() can select multiple options at once in a multi-select dropdown? Commit to your answer.
Concept: cy.select() supports selecting multiple options if the dropdown allows multiple selections.
For a multi-select dropdown: You can select multiple options: cy.get('#colors').select(['Red', 'Blue']) This selects both 'Red' and 'Blue'.
Result
Both 'Red' and 'Blue' options become selected in the dropdown.
Understanding multi-select behavior lets you test complex forms where users pick several choices.
5
IntermediateVerifying selection with assertions
🤔
Concept: Learn how to check that the dropdown selection worked using Cypress assertions.
After selecting an option, verify it: cy.get('#fruits').select('Cherry') cy.get('#fruits').should('have.value', 'cherry') This confirms the dropdown's value matches the expected option's value attribute.
Result
Test passes if the dropdown's selected value is 'cherry', fails otherwise.
Assertions ensure your test not only performs actions but also confirms the app behaves as expected.
6
AdvancedDealing with dynamic dropdowns
🤔Before reading on: do you think cy.select() works on custom dropdowns built without elements; custom dropdowns require different approaches.
Some apps use styled dropdowns made of divs and buttons, not and its options exist and are not disabled. However, if options load asynchronously after page load, you may need explicit waits or assertions before selecting: cy.get('#fruits').should('contain', 'Banana') cy.get('#fruits').select('Banana') This ensures options are present before selection.
Result
Tests become more stable and avoid flaky failures due to timing.
Understanding Cypress's automatic waiting helps you write reliable tests and debug selection issues caused by slow-loading dropdown options.
Under the Hood
cy.select() works by finding the ? Commit yes or no.
Common Belief:cy.select() can select options in any dropdown UI, including custom styled div-based dropdowns.
Tap to reveal reality
Reality:cy.select() only works on native HTML element but may not wait for options loaded asynchronously unless you add explicit waits or assertions.
Why it matters:Tests can fail intermittently if options are not present yet, causing flaky test results.
Quick: Can cy.select() select multiple options in a dropdown that does not support multiple selections? Commit yes or no.
Common Belief:cy.select() can select multiple options regardless of dropdown settings.
Tap to reveal reality
Reality:cy.select() can only select multiple options if the elements, not on custom HTML structures.
#2Selecting an option before it is loaded dynamically.
Wrong approach:cy.get('#fruits').select('Banana') // immediately after page load
Correct approach:cy.get('#fruits').should('contain', 'Banana') cy.get('#fruits').select('Banana')
Root cause:Not waiting for asynchronous content to appear before interacting causes flaky test failures.
#3Selecting multiple options on a single-select dropdown.
Wrong approach:cy.get('#fruits').select(['Apple', 'Banana'])
Correct approach:cy.get('#fruits').select('Apple') // single selection only
Root cause:Ignoring the dropdown's 'multiple' attribute and trying to select multiple options where not supported.