Check checkbox(es) or radio(s).
This element must be an
<input>with typecheckboxorradio.
.check()
.check(value)
.check(values)
.check(options)
.check(value, options)
.check(values, options)
Correct Usage
cy.get('[type="checkbox"]').check() // Check checkbox element
cy.get('[type="radio"]').first().check() // Check first radio element
Incorrect Usage
cy.check('[type="checkbox"]') // Errors, cannot be chained off 'cy'
cy.get('p:first').check() // Errors, '.get()' does not yield checkbox or radio
value (String)
Value of checkbox or radio that should be checked.
values (Array)
Values of checkboxes or radios that should be checked.
options (Object)
Pass in an options object to change the default behavior of .check().
| Option | Default | Description |
|---|---|---|
animationDistanceThreshold |
animationDistanceThreshold |
The distance in pixels an element must exceed over time to be considered animating. |
log |
true |
Displays the command in the Command log |
force |
false |
Forces the action, disables waiting for actionability |
scrollBehavior |
scrollBehavior |
Viewport position to where an element should be scrolled before executing the command |
timeout |
defaultCommandTimeout |
Time to wait for .check() to resolve before timing out
|
waitForAnimations |
waitForAnimations |
Whether to wait for elements to finish animating before executing the command. |
.check() yields the same subject it was given from the previous command. cy.get('[type="checkbox"]').check()
cy.get('[type="radio"]').check()
cy.get('#saveUserName').check()
<form>
<input type="radio" id="ca-country" value="CA" />
<label for="ca-country">Canada</label>
<input type="radio" id="us-country" value="US" />
<label for="us-country">United States</label>
</form>
cy.get('[type="radio"]').check('US')
<form>
<input type="checkbox" id="subscribe" value="subscribe" />
<label for="subscribe">Subscribe to newsletter?</label>
<input type="checkbox" id="acceptTerms" value="accept" />
<label for="acceptTerms">Accept terms and conditions.</label>
</form>
cy.get('form input').check(['subscribe', 'accept'])
You can ignore Cypress' default behavior of checking that the element is visible, clickable and not disabled by setting force to true in the options.
cy.get('.action-checkboxes')
.should('not.be.visible') // Passes
.check({ force: true })
.should('be.checked') // Passes
You can get the currently checked option using the jQuery's
selector.<form id="pick-fruit">
<input type="radio" name="fruit" value="orange" id="orange" />
<input type="radio" name="fruit" value="apple" id="apple" checked="checked" />
<input type="radio" name="fruit" value="banana" id="banana" />
</form>
cy.get('#pick-fruit :checked').should('be.checked').and('have.value', 'apple')
.check() is an "action command" that follows all the rules of Actionability.
.check() requires being chained off a command that yields DOM element(s). .check() requires the element to have type checkbox or radio . .check() will automatically wait for the element to reach an actionable state .check() will automatically retry until all chained assertions have passed .check() can time out waiting for the element to reach an actionable state . .check() can time out waiting for assertions you've added to pass. check the element with name of 'emailUser'
cy.get('form').find('[name="emailUser"]').check()
The commands above will display in the Command Log as:
When clicking on check within the command log, the console outputs the following:
© 2017 Cypress.io
Licensed under the MIT License.
https://docs.cypress.io/api/commands/check