Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

179
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!