Home > Net >  Asserting set of arrays. But 1st set of array only compare the first one
Asserting set of arrays. But 1st set of array only compare the first one

Time:09-02

Hi i have this code and i want to assert the emailDataVal to equal to obfuscateEmailText but the emailDataVal only comparing the first result in it's array. It goes like this

Expected result:

emailDataVal = [email1, email2, email3, email4]

obfuscateEmailText = [email1, email2, email3, email4]

Actual result:

emailDataVal = [email1, email1, email1, email1]

obfuscateEmailText = [email1, email2, email3, email4]

it('test', () => {
        cy.visit('/landing');

        cy.get('.mailto-obfuscated-email').each((emailVal) => {
            const emailDataVal = emailVal.data('value')
            cy.log(emailDataVal)

        cy.get('.amailto-obfuscated-email').each((emailContent) => {
            const emailContentText = emailContent.text()
            const obfuscateEmailText = emailContentText
            .replaceAll('.', '(dotted)')
            .replaceAll('@', '(at)')
        cy.log(obfuscateEmailText)

        // ASSERTION 
        expect(emailDataVal).to.deep.equal(obfuscateEmailText)

        });
        });
    });
    
});

CodePudding user response:

You can store the values of all the first iterations in an array. Then for the second iteration compare the values from he array, something like this:

it('test', () => {
  cy.visit('/landing')
  var emailDataVal = []
  cy.get('.mailto-obfuscated-email')
    .each((emailVal) => {
      emailDataVal.push(emailVal.data('value'))
    })
    .then(() => {
      cy.get('.amailto-obfuscated-email').each((emailContent, index) => {
        const emailContentText = emailContent.text()
        const obfuscateEmailText = emailContentText
          .replaceAll('.', '(dotted)')
          .replaceAll('@', '(at)')

        // ASSERTION
        expect(emailDataVal[index]).to.equal(obfuscateEmailText)
      })
    })
})

CodePudding user response:

It's easier to use .map() than to use .each() for this:

const obfusticate = ($el) => {
  return $el.text()
    .replaceAll('.', '(dotted)')
    .replaceAll('@', '(at)')
}

cy.get('.mailto-obfuscated-email')
  .then($els => Cypress.$.map($els, ($el) => $el.data('value')) )
  .then(emailDataVals => {

     cy.get('.amailto-obfuscated-email')
       .then($els => Cypress.$.map($els, ($el) => obfusticate($el)) )
       .then(obfusticated => {
         expect(emailDataVals).to.deep.equal(obfusticated)
       })
  })
  • Related