Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

270
Vistas
Cypress - assert if are duplicates in array

Can someone please assist in following:

I have to assert array with x elements (usually not more than 6 or 7) and if there is any duplicates, it has to throw error - or step to fail. So far I did the following:

Cypress.Commands.add('addTextElementIntoArray', (list) => {
  var textsArray = []
  cy.xpath(list).each(($el) => {
    const text = $el.text().replace(' ', '')

    textsArray.push(text)
    cy.log(text)
  })
})

Tried this solution: Find duplicates, but it does not work in Cypress. How can I solve this? Thank you in advance

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Found solution, and here it is:

Cypress.Commands.add('addTextElementIntoArray', (list) => {
var textsArray = []
var non_unique = []
cy.xpath(list)
.each(($el) => {
  const text = $el.text().replace(' ', '')

  textsArray.push(text)
  cy.log(text)
  non_unique = textsArray.filter((item, i) =>
    textsArray.includes(item, i + 1)
  )
})
.then(() => {
  expect(non_unique.length).equal(0)
})

})

about 4 years ago · Juan Pablo Isaza Denunciar

0

Using the answer from the linked question in Cypress,

Cypress.Commands.add('listHasNoDuplicates', (list) => {

  cy.xpath(list)
    .then($els => [...$els].map(el => el.innerText.trim()))
    .then(texts => {
      const unique = new Set(texts)
      expect(texts.length).to.eq(unique.size)
    })
  })
})
about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda