Skip to main content

Cypress url Command

Cypress URL command is basically to get the currently active URL of the page. Used to confirm that the URL we visit is the correct one.

URL is similar to the location command. It is the exact alias of the location command.

Syntax

cy.url()
cy.url(options)

Some of the options we can include in the command can be a timeout, log, etc.

  • Log: To display the command in the command log.
  • Timeout: Wait for some particular thing to get finished before timing out.
  • decode: Decode url

Correct Use:

cy.url()
  • This will yield back the current URL as a string.

Example

Let us see the URL command in action.

describe('URL Command',()=> {
    it('Should load the url',()=>{
        cy.visit('/')
        cy.url().should('include','/user/login')
        cy.url().should('contain','https://www.programsbuzz.com/user/login')
        cy.url().should('eq','https://www.programsbuzz.com/user/login')
        
    })
})

This is one effective way to use the URL command to assert the active URL currently used. Here we have asserted the endpoints too.

In this case, we are using the baseURL in the cypress.json file. Instead of hardcoding the URL, we can call the baseUrl location directly and assert.

describe('URL Command',()=> {
    it('Should load the url',()=>{
        cy.visit('/')
        cy.url().should('include','/user/login')
        cy.url().should('contain',Cypress.config().baseUrl)
        cy.url().should('eq',Cypress.config().baseUrl)
        
    })
})

Does the same instead, here the URL is not hardcoded here like in the previous.

Tags
Submitted by arilio666 on January 25, 2022

Ashwin is working with iVagus as a Technical Lead. He has experience in various technologies like Cypress, JavaScript, TypeScript, Rest Assured, Selenium, Cucumber, and Java.

About

At ProgramsBuzz, you can learn, share and grow with millions of techie around the world from different domain like Data Science, Software Development, QA and Digital Marketing. You can ask doubt and get the answer for your queries from our experts.