Select an option from a dropdown and verify selection
Preconditions (1)
✅ Expected Result: The dropdown menu shows 'Canada' as the selected option after selection
describe('Dropdown selection test', () => { it('Selects Canada from country dropdown and verifies selection', () => { cy.visit('https://example.com/dropdown-page'); cy.get('#country-select').select('Canada'); cy.get('#country-select').should('have.value', 'Canada'); }); });
The test starts by visiting the page with the dropdown.
We locate the dropdown using cy.get('#country-select') which uses the id selector, a best practice for stable selectors.
Then we use .select('Canada') to pick the option with visible text 'Canada'. This is simple and clear.
Finally, we assert the dropdown's value is 'Canada' using should('have.value', 'Canada') to confirm the selection worked.
This approach avoids brittle selectors and uses Cypress commands consistently.
Now add data-driven testing to select and verify three different countries: 'Canada', 'USA', and 'Mexico'.
cy.select() command do in Cypress testing?cy.select()cy.select() command is designed specifically to choose options from dropdown menus in Cypress tests.cy.type(), clicking uses cy.click(), and waiting uses cy.wait(). So, cy.select() is unique for dropdowns.cy.select()?cy.select() syntaxcy.get(), then call .select() with the visible text as argument.selectByText() or choose() method in Cypress. Also, cy.select() is not called directly with selector and text.cy.get('#colors').select('blue').should('have.value', 'blue')cy.get('select#country').select(2).should('have.value', 'US')cy.select() accepts visible text, value, or an array of these. Selecting by index number directly (like 2) is not supported.select#country is valid CSS. The assertion should('have.value', 'US') is correct to check the selected value.cy.select()?cy.select().select() calls does not accumulate selections. Passing a comma-separated string selects one option matching that exact string, which is invalid.Pass the test and this pattern moves unknown