I'm very new to Javascript and it's my only second week using Cypress, so I need help in getting radio buttons to be clicked. I'm getting errors from Cypress all the time.
The element that I'm trying to check looks like:
<input class="XyzTypeRadio" type="radio" name="zzz_type" value="2">
And what I tried to implement after reading the Cypress documentation (at .html#Syntax )was:
cy.get('[type="radio"]').first('.XyzTypeRadio').check('value=2')
Also tried simply .... .check('2')
and ... .check('Xyz')
I'm very new to Javascript and it's my only second week using Cypress, so I need help in getting radio buttons to be clicked. I'm getting errors from Cypress all the time.
The element that I'm trying to check looks like:
<input class="XyzTypeRadio" type="radio" name="zzz_type" value="2">
And what I tried to implement after reading the Cypress documentation (at https://docs.cypress.io/api/mands/check.html#Syntax )was:
cy.get('[type="radio"]').first('.XyzTypeRadio').check('value=2')
Also tried simply .... .check('2')
and ... .check('Xyz')
- Wele to programming with Javascript :) I am not familiar with Cypress, but I (and other Javascript programmers like me) might be able to help you if you add a link to the documentation for the "check" function! Also, try this: open up the browser's console and see if the browser is reporting any errors (which are usually red) in the console. – sina Commented Feb 27, 2020 at 12:19
- How to open the console – sina Commented Feb 27, 2020 at 12:22
- Thank you for response! It's not the code written by me and it's written over 10 years ago, so I'm sure it's working. But I'm trying to include it in the acceptance test and getting errors from Cypress. – Stacey Commented Feb 27, 2020 at 12:24
- sina, thank you. I got this code from "inspect element", so I know where console is but it didn't help me here. – Stacey Commented Feb 27, 2020 at 12:30
-
My guess is that the "first" function is not used the way it is described in the docs. Try this
cy.get('[type="radio"].XyzTypeRadio').first().check()
instead and see what happens. – sina Commented Feb 27, 2020 at 12:36
2 Answers
Reset to default 7(edited and working answer)
Try this:
cy.get('[type="radio"].XyzTypeRadio').check("2")
Or if you don't care which radio button is checked, you could check the first one:
cy.get('[type="radio"].XyzTypeRadio').first().check()
Takeaways:
- The first() function does not understand selectors, that's why we need to pass our selector ".XyzTypeRadio" to get().
- The check() function expects the value or values as its argument, so instead of "value=2" we simply give it "2".
- The check() function does a bit of selecting, ie the result of everything before calling check("2") is a list of inputs and the check("2") function searches and selects the one whose value is "2".
- We could use first().check() if we want to check the first radio button, or we could remove first() and check a radio button with a specific value using check("2").
cy.get('[value="Other"]').first().check()
this worked for me as there are 3 radio buttons each with a value so it was just a matter of selecting the correct value