Reputation: 12874
verifyModuleTitles(expectedTitles) {
cy.get('h3[class*="block-module_title"]')
.should('have.length', 3)
.then((items) => {
cy.log(items[0])
expect(items[0]).to.contains.text(expectedTitles[0])
expect(items[1]).to.contains.text(expectedTitles[1])
expect(items[2]).to.contains.text(expectedTitles[2])
})
}
I've written a common function verifyModuleTitles
that can be invoked as below
verifyModuleTitles(['xx', 'yy', 'zz'])
Now everything works great, except i find that it's repetitive and i wish to improve to something as below, but its not working
verifyModuleTitles(expectedTitles) {
cy.get('h3[class*="block-module_title"]')
.should('have.length', expectedTitles.length)
.then((items) => {
items.forEach((x, index) => {
expect(x).to.contains.text(expectedTitles[index])
})
})
}
The exception thrown seems like looping is not being supported in cypress?
UPDATES
Upvotes: 2
Views: 198
Reputation: 44288
The items are the children which is not exactly an array, its an HTMLCollection (https://developer.mozilla.org/en-US/docs/Web/API/HTMLCollection)
so you can do
[...items].forEach()
which then turns the collection into an array
Upvotes: 3