Skip to main content

Cypress location Command

Cypress location works the same as the cypress URL command. It gets the global location object of the page and checks whether it is on the right page.

Syntax

cy.location()
cy.location(key)
cy.location(options)
cy.location(key, 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.
  • Options: Can pass options object to change the typical default behavior of the cy.location().
  • Key: A key provided within the location object will return the value instead of the entire location object.

Correct Use:

cy.location()
  • Get location object
cy.location('host')
  • Gets the host of the location object.
cy.location('port')
  • Gets the port of the location object.

cy.location() yields the location object with the following properties:

  1. hash
  2. host
  3. hostname
  4. href
  5. origin
  6. pathname
  7. port
  8. protocol
  9. search
  10. toString

The URL command is an alias of cy.location('href'), meaning that whatever we are doing in the URL can be done the same in the location command.
Let us check it out.

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

  • Here we have used some of the yields to provide the location as an object to get the provided key instead of the whole thing.
  • We have configured the base URL and used that URL to validate using the location command.
Tags
Submitted by arilio666 on January 31, 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.