Skip to main content

Cypress Aliases Sharing Context

  • In cypress aliases, there is a topic of sharing context.
  • This is generally called because of its aliasing ability to name alias and call it via this keyword.
  • This is another method we can access the alias name from anywhere.
  • As we know aliases are accessible using the @aliasname.
  • But in the world of shared context, we can only call it by using this keyword.
  • The arrow bind used in the test steps 'it' cannot bind their own this keyword.

So thus in this situation to access the alias we use the traditional function() instead of => inside our test block.

Syntax For Alias:

cy.visit('www.autopract.com').as('theSite')

Syntax For The Usage Of Shared Context:

it('test', function() {

 expect(this.theSite).to.eq('www.autopract.com')

})

So let us dive into a real-time example for the shared context alias.

describe('Automate AutoPract',()=>{
    before(() => {
        cy.visit('http://www.autopract.com/#/home/fashion')
        })
    beforeEach(()=> {
        cy.get('.close').as('Popup')
    })
    it('Should click POPUP',function(){
        this.Popup.click()

    })

})
  • so here the before each hook I have named the alias selector as a popup which closes the popup, as soon as the website loads up.
  • As we discussed we are using the function() bind to invoke this keyword.
  • So normally this keyword will call up the alias instead of @ and proceeds to execute the test.
Submitted by arilio666 on October 20, 2021

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.