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

175
Visualizações
how to validate comment-size text from the text inserted in the comment box using Cypress

hi folks as I am new to this automation/cypress world i need your help to handle this problem statement

i have a comment box something similar to this i am able to put text inside this text with

cy.get('#comments').type('TEST')

but there is a element similar to this which shows character count as

*4/4000 Characters Used

instead of highlighted one

what i wanted ? i wanted to validate the text with the on run time

what i have done so far

i have created a custom command which return the text out of element by passing its locator

Cypress.Commands.add('getElementText', (locator) => {
  cy.get(locator).invoke('text').then((text)=>{
  return text.length
})
})

what i needed

cy.get('#commentSize').invoke('text').should('eq','*'+cy.getElementText('#comments').then((text)=>{return text})+'/4000 Characters Used')

so that the final validation should appear like this

cy.get('#commentSize').invoke('text').should('eq','*'+ '4' +'/4000 Characters Used')

but i am getting assert error expected 4 to equal * [object Object]NaN

enter image description here

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

0

You have to wrap and then return your text like this:

Cypress.Commands.add('getElementText', (locator) => {
  cy.get(locator)
    .invoke('text')
    .then((text) => {
      return cy.wrap(text.length)
    })
})

Then in your test you have to do this:

cy.getElementText('#comments').then((len) => {
  cy.get('#commentSize')
    .invoke('text')
    .should('eq', '*' + len + '/4000 Characters Used')
})

You can also directly assert using the should('have.text')like this:

cy.getElementText('#comments').then((len) => {
  cy.get('#commentSize').should(
    'have.text',
    '*' + len + '/4000 Characters Used'
  )
})
about 4 years ago · Juan Pablo Isaza Relatório

0

Here's another variation of your test, using the element passed down after .type().

cy.get('#comments').type('TEST')
  .then($comments => {
    cy.get('#comments-size')
      .should('have.text', `*${$comments.text().length}/4000 Characters Used`)
  })

Since you are using .type() I am surprised to see .text() work.

Normally you use .type() on an <input> which requires .val() to fetch the typed-in text.

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