Cypress Aliases Sharing Context

Profile picture for user arilio666
  • 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.