I want to get the number from the URL located in an API response. For that I get the URL, but I don't know how to convert that in to text to extract the number.
cy.intercept('GET', 'http://viasphere.localhost/documents/page_elements/client/**',).as('response')
goTo.plusClientsButton()
cy.wait('@response', {timeout: 10000})
cy.get('@response').then( xhr => {
const link = xhr.request.headers.referer
cy.log(link)
link has the value: http://viasphere.localhost/documents/page_elements/client/19537
Obviosly const link = xhr.request.headers.referer.text()
is not working...
CodePudding user response:
You have to add .replace(/^\D /g, '')
to extract the number from the url.
cy.intercept(
'GET',
'http://viasphere.localhost/documents/page_elements/client/**'
).as('response')
goTo.plusClientsButton()
cy.wait('@response', {timeout: 10000})
cy.get('@response').then((xhr) => {
const client_id = xhr.request.headers.referer.replace(/^\D /g, '')
cy.log(client_id) //prints 19537
})
CodePudding user response:
Alternatively to the .then()
Alapan Das provided, you can use cypress commands to perform the same actions.
cy.intercept('GET', 'http://viasphere.localhost/documents/page_elements/client/**',).as('response')
goTo.plusClientsButton()
cy.wait('@response', {timeout: 10000})
// can access request.headers.referer from .wait()
.its('request.headers.referer')
// returns 'http://viasphere.localhost/documents/page_elements/client/'
.invoke('replace', /^D /g, '')
// now you have the link and can print it
.then(cy.log)