Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

453
Visualizações
Validations using if else and hasClass statements in Cypress

I am trying to validate the set of titles for a component. Below is my Cypress code snippet:

it('Validate the titles of all the tiles', () => {
    cy.get('.bms-scoreboard__game-tile')
      .each(($el) => {
        if($el.hasClass('bms-scoreboard__game-tile--cancelled')) {
            $el.get('.bms-scoreboard__game-tile-status--cancelled')
               .invoke('text')
               .then((text) => {
                  expect(text).equals('Cancelled')
               })
        } else if($el.hasClass('bms-scoreboard__game-tile--pre-game')) {
            $el.get('.bms-scoreboard__game-time--en')
               .invoke('text')
               .then((text) => {
                    const gameTime = text.split(" ").pop()
                    expect(['AM', 'PM']).to.include(gameTime)
               })
        } else if($el.hasClass('bms-scoreboard__game-tile--final')) {
            $el.get('.bms-scoreboard__game-time--en')
               .invoke('text')
               .then((text) => {
                   const finalTitle = text.trim()
                   expect(finalTitle).to.be.oneOf(['Final','Final (OT)'])
               })
        } else if($el.hasClass('bms-scoreboard__game-tile--ongoing')) {
            $el.get('.bms-scoreboard__game-time--en')
               .invoke('text')
               .then((text) => {
                   const ongoingTitle = text.trim()
                   expect(ongoingTitle).equals('Ongoing')
               })
        }
    })
})

But I get an error message: 'Cannot read properties of undefined (reading 'invoke')'.

It works fine if I try it with only if block.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Each time that you are doing $el.get(), you should be wrapping the $el before doing the .get(), because when $el is yielded from your initial cy.get(), it is a JQuery<HTMLElement>, and thus out of the Cypress chain.

Additionally, once you wrap $el, you can use .find() to search within the wrapped element.

cy.get('.bms-scoreboard__game-tile')
      .each(($el) => {
        if($el.hasClass('bms-scoreboard__game-tile--cancelled')) {
             cy.wrap($el)
               .find('.bms-scoreboard__game-tile-status--cancelled')
               .invoke('text')
               .then((text) => {
                  expect(text).equals('Cancelled')
               })
...
about 4 years ago · Juan Pablo Isaza Relatório

0

You can shorten it a bit with jQuery operators.

$el.find(...) works since $el is a jQuery object.

Also change .invoke('text') to .text().

cy.get('.bms-scoreboard__game-tile')
  .each(($el) => {

    if ($el.hasClass('bms-scoreboard__game-tile--cancelled')) {

      // can work with jQuery operators here
      const text = $el.find('.bms-scoreboard__game-tile-status--cancelled').text()
      expect(text).equals('Cancelled')
    }

    if ($el.hasClass('bms-scoreboard__game-tile--pre-game')) {
      const text = $el.find('.bms-scoreboard__game-time--en').text()
      const gameTime = text.split(" ").pop()
      expect(['AM', 'PM']).to.include(gameTime)
    }

    if ($el.hasClass('bms-scoreboard__game-tile--final')) {
      const text = $el.find('.bms-scoreboard__game-time--en').text()
      const finalTitle = text.trim()
      expect(finalTitle).to.be.oneOf(['Final','Final (OT)'])
    }

    if ($el.hasClass('bms-scoreboard__game-tile--ongoing')) {
      const text = $el.find('.bms-scoreboard__game-time--en').text()
      const ongoingTitle = text.trim()
      expect(ongoingTitle).equals('Ongoing')
    }
  })
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda