Cypress: How to select a dynamic option?

Created on 29 Aug 2018  路  2Comments  路  Source: cypress-io/cypress

Current behavior:

select(value) works fine but what if I cannot get the value ? I've tried using $ to get current option value and then use it as a parameter but it doesn't work.

Desired behavior:

At least I can select the first option without knowing value or any other workaround ? Thanks.

Versions

Cypress: ^3.1.0

question

Most helpful comment

@FrontendSophie I think this code can help you, it selects the first option of the select.

    cy.get('select').then(($select) => {
        const opt = $select.find('option:nth-child(2)')
        $select.val(opt.attr('value'))
        return $select
      }).trigger('change')

All 2 comments

@FrontendSophie I think this code can help you, it selects the first option of the select.

    cy.get('select').then(($select) => {
        const opt = $select.find('option:nth-child(2)')
        $select.val(opt.attr('value'))
        return $select
      }).trigger('change')

@raphaelcunha cheers

Was this page helpful?
0 / 5 - 0 ratings

Related issues

EirikBirkeland picture EirikBirkeland  路  3Comments

carloscheddar picture carloscheddar  路  3Comments

igorpavlov picture igorpavlov  路  3Comments

rbung picture rbung  路  3Comments

szabyg picture szabyg  路  3Comments