1

I have a Cypress test that I wrote, and to avoid writing a second test, I'd like to be able to take the resulting URL, and do two things with it:

1) I'd like to modify it to force it to another page

2) While doing #1, I'll also need to extract a dynamic value from the URL to be used in the modified URL.

I have cy.url() but I don't know how to capture the value I need and set it as a variable. Does anyone know of a way to do this? I'm a new coder and still learning javascript so this might be easy and I just don't know how to go about it. Any advice is greatly appreciated!

1 Answers1

5

To extract values from cy elements, you must use promises (then syntax). Working with JS asynchronicity is a bit daunting at first, but do not panic! Study this core concept and you will quickly learn how Cypress works!

To answer question #1, you could do:

cy.url().then(urlValue => cy.visit(urlValue + '/about'));

The example above will extract the current url and you can use it in the next action. (url is exposed as an arrow function argument, here called urlValue).