Search code examples
testingurlautomationpathcypress

How to save just part of URL in Cypress (without domain name)


I'm writing CY test and was trying to solve it by myself for couple of hours but unsuccessfully. Could you please help me here a bit :)

Each time I run the test I'm getting new URL, e.g.

https://website.com/en/info/is/here/

And I need to save only

/en/info/is/here/ (so without domain name)

I need to compare it later with another href.

Could you please advise me the way how to do it or at least the direction? Thanks a lot!


Solution

  • The cy.location() command gives you named parts, so from the example pathname is the part you need

    cy.visit('http://localhost:8000/app/index.html?q=dan#/users/123/edit')
    
    cy.location().should((loc) => {
      ..
      cy.wrap(loc.pathname).as('url1')    
      ...
    })
    

    If you have search or hash as well

    cy.visit('http://localhost:8000/app/index.html?q=dan#/users/123/edit')
    
    cy.location().should((loc) => {
      ..
      cy.wrap(loc.pathname + loc.search + loc.hash).as('url1')    
      ...
    })