Noel Dulin
Noel Dulin

Reputation: 419

Refining repeating selectors on Cypress

Hi i'm doing a code clean up in my project and i just want to clarify it this 2 codes is providing the same behavior? I just want to try shorter methods and avoid repeating of selectors

it('Check all logos if visible', () => {
        cy.get('[class="images"] img:nthchild(1)').should('be.visible')
        cy.get('[class="images"] img:nth-child(2)').should('be.visible')
        cy.get('[class="images"] img:nth-child(3)').should('be.visible')
it('Check all logos if visible', () => {
        cy.get('[class="images"] img').each(($el) => {
            expect($el).to.be.visible
          })

Upvotes: 1

Views: 39

Answers (1)

Fody
Fody

Reputation: 31904

One difference is cy.get('[class="images"] img').each() will not provide regression coverage if one of the img doesn't load for some reason (e.g resource missing from the server).

Try

cy.get('[class="images"] img')
  .should('have.length', 3)
  .each(($el) => {
    expect($el).to.be.visible
  })

Upvotes: 1

Related Questions