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

189
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 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