Cypress url Command

Profile picture for user arilio666

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.