Cypress Quick Tip: Loops

06/02/20171 Min Read — In Resources, Testing, Development

If you want to loop over a set of items, you can create an array of objects, use wrapping and each to chew through your items. Sweet!

describe('Test items', () => {
it('Filters items', () => {
const filters = [
{link: 'FooFoo', expectedLength: 2},
{link: 'BarbBar', expectedLength: 2},
{link: 'BazBaz', expectedLength: 4}
]
cy.triggerFixtureAndOpenPage('fixture:wholebunchofitems')
cy.wrap(filters)
.each(filters => {
cy.contains(filter.link).click()
cy.get('.foo li').should('have.length', filter.expectedLength)
})
})
})