Skip to content

How do we select dropdown option using data-test-id in cypress #15710

Answered by bahmutov
ShehFahad asked this question in Plugins
Discussion options

You must be logged in to vote

See how to do this in https://glebbahmutov.com/cypress-examples/6.8.0/recipes/select-value-by-id.html

// let's find the option with the given test-id
const optionid = 'pick-oranges'
cy.get('[data-testid=select-example]')
  .find(`option[data-testid=${optionid}]`)
  .invoke('attr', 'value')
  .then((value) => {
    // now that we know the value of the <option>
    cy.get('[data-testid=select-example]').select(value)
  })
// confirm the selected option
cy.get('[data-testid=select-example] option:selected').should(
  'have.text',
  'Oranges',
)

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@ShehFahad
Comment options

Answer selected by bahmutov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants