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.

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