Learn how to remove code duplication from your Cypress tests by calling the .check() command only once when you want to check all the checkboxes in a section of the application ☑️
I don't know about you, but I don't really like code duplication. So today, I'm going to show you a technique to eliminate some duplication when dealing with checkboxes using the Cypress test automation framework.
To exemplify, I will use a sample application created using only static files (HTML, CSS, and a background image).
Note: the app text is in Portuguese
In this application, as shown below, there is a section with the label Select the technologies you use:
Since all checkboxes are contained in a div with id check, I can check them all with a single command like this:
// cypress/e2e/checkboxes.cy.js
describe('Checkboxes', () => {
beforeEach(() => {
cy.visit('https://bit.ly/3vswFBe')
})
it('checks all checkboxes with one command', () => {
cy.get('#check input[type="checkbox"]')
.as('checkboxes')
.check()
cy.get('@checkboxes')
.each(checkbox => {
expect(checkbox[0].checked).to.equal(true)
})
})
})
And since the .check() command chained to cy.get() checks more than one checkbox, all checkboxes are checked if the used selector is not specific to a single element, which is the case.
With that, I can have a new cy.get(), this time passing as argument the alias created earlier ('@checkboxes'), to check that they are all really checked.
Easy ha?
For more details on the .check() functionality, I recommend reading the Cypress official docs.
Did you like this “Pinch of Cypress”?
Leave a comment.
Are you curious and want to learn more about testing automation with Cypress? I was hoping you could check out my Cypress courses on Udemy.
Want to go deeper?
I built a hands-on learning platform to take you from your first test to a complete end-to-end testing workflow with confidence. You'll find interactive lessons, coding challenges, and quizzes that cover real user interactions, accessibility checks, network mocking, API testing, and the automatic running of tests in CI/CD.
New courses are added over time, so there's always more to explore.

Top comments (4)
Really appreciate this concise and practical tip—eliminating repetition in tests not only keeps the codebase clean but also makes maintenance much easier as the app evolves. I hadn't realized .check() could handle multiple elements so seamlessly when applied with a broad selector. Also loved the use of aliasing with @checkboxes to verify results—super readable and efficient! This kind of hands-on example is exactly what makes Cypress approachable for those new to test automation. Looking forward to trying this technique in one of my test suites soon. Would love to see more of these focused snippets—maybe one on .uncheck() or how to group conditional checks next?
This is one of a series of articles. All others are available at dev.to/walmyrlimaesilv/series/11377
Thats a nice one. Thanks @walmyrlimaesilv
I'm glad you liked it.