javascriptjquerycypresse2e-testingcypress-testing-library

how to get value attribute of radio input with cypress?


I have a radio element <input type="radio" name="gender" value="male" />

below is my cypress code

cy.getAllByRole("radio").first().click()

how do I get the value attribute of the radio element? something like this

const radioValue = cy.getAllByRole("radio").first().getValue() //"male"


Solution

  • You can try this:

    cy.getAllByRole('radio')
      .first()
      .invoke('val')
      .then((val) => {
        cy.log(val) //logs male
      })