3

I can get current Url and pass it to const variable.

My URL like this http://bla/bla/ID

I want to take ID and pass it to const again.

This is my code.

    cy.url().then(url => {
        const currentURL = url;
    });

Pls help me, How can I do it with cypress.

Narine Poghosyan
  • 853
  • 3
  • 16
  • 26
  • Possible duplicate of [Last segment of URL](https://stackoverflow.com/questions/4758103/last-segment-of-url) – Dean Taylor Sep 27 '19 at 13:24
  • No, My question is different, Thanks – Narine Poghosyan Sep 27 '19 at 15:05
  • really it's not different the answer you have accepted is simply JavaScript manipulating a string - nothing to do with cypress. Most (if not all) of the answers in the question [Last segment of URL](https://stackoverflow.com/questions/4758103/last-segment-of-url) also apply. – Dean Taylor Sep 27 '19 at 18:25

1 Answers1

2

You are not necessarily searching for a solution within Cypress, but within Javascript. What you can do is this:

  const currentURL = 'http://bla/bla/ID'.split('/')
  const id = currentURL[4]
cy.log(id)

What does it do?

  • currentURL contains the full URL
  • split() will split the URL per every '/'. So you'll end up with an array with values 'http:', '', 'bla', 'bla' and 'ID'.
  • id will hold the actual ID part of the URL
  • log() will print the value of 'id' in your Cypress logging.
Mr. J.
  • 3,499
  • 1
  • 11
  • 25