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

187
Visualizações
How can I Split text and log the value in Cypress?

I am trying split text that gets printed in the Web application and log just the value in cypress. I just need the number from the text. But seem to to be stuck because the value is not getting printed(Logs are empty) as it is not recognized. Tried various approach but still cant seem to print it. I thought that since I need the numeric value, cypress does not recognize the number since I specified as a text. But I even try to invoke 'val'. The result seems the same.

cy.get('#__label14-bdi').invoke('text').then((text) => {
       cy.log(text)
       var splittext= text.split('')[1];
       cy.log(splittext);

From the above code the letter 'i' is getting printed. Here is a ss of the text from the web application. enter image description here

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

0

You are close you have to add a space in split(' ')

"Ticket #502582".split(' ')[1]
//You get #502582

Or, if you want to remove the special characters from the number string, you can do:

"Ticket #502582".split(' ')[1].replace(/[&\/\\#,+()$~%.'":*?<>{}]/g, '');
//You get 502582

Or, If you just want to remove #, you can

"Ticket #502582".split(' ')[1].replace(/#/g, '')
//You get 502582

So your program should like this:

cy.get('#__label14-bdi')
  .invoke('text')
  .then((text) => {
    cy.log(text) //logs Ticket #502582
    var splittext = text.split(' ')[1].replace(/#/g, '')
    cy.log(splittext) //logs 502582 as string
    cy.log(+splittext) //logs 502582 as Number
  })
about 4 years ago · Juan Pablo Isaza Relatório

0

It may be easier using a regular expression

cy.get('#__label14-bdi').invoke('text')
  .then((text) => {
    return text.match(/\d+/)[0]  // extract digits from text (first match)
  })
  .should('eq', '502582')

As a number

cy.get('#__label14-bdi').invoke('text')
  .then(text => text.match(/\d+/)[0])   // extract digits (first match)
  .then(parseInt)
  .should('eq', 502582)

Using .split(), note extra the () for grouping

cy.get('#__label14-bdi').invoke('text')
  .then(text => text.split(/(\d+)/)[1])   // 2nd part of split
  .then(parseInt)
  .should('eq', 502582)
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