cypress icon indicating copy to clipboard operation
cypress copied to clipboard

Calling Login() a second time does not work

Open Mushoz opened this issue 3 years ago • 1 comments

Whenever I call Login() a second time on a different user, it does not login as expected. Calling login() a second time on the same user works just as expected. Why is this small test case not logging in as expected?

describe('empty spec', () => {

  beforeEach(function () {

    cy.create({model: 'App\\Models\\User', state: {'representative':[],}}).as('regularUser1')
    cy.create({model: 'App\\Models\\User', state: {'representative':[],}}).as('regularUser2')

  });

  it('Example Test Case', function () {
    cy.visit('/')
    cy.login({id:this.regularUser1.id})
    cy.visit('/')
    cy.url().should('contain', '/dashboard') // this works
    cy.logout()
    cy.visit('/')
    cy.login({id:this.regularUser2.id})
    cy.visit('/')
    cy.url().should('contain', '/dashboard') // this fails, will be redirected to the login page instead
  })
})

Mushoz avatar Dec 12 '22 15:12 Mushoz

Okay, so clearing cookies between the login calls seem to work, but this is pretty cumbersome having to do this every time. Is there any way around this? Working example below:

describe('empty spec', () => {

  beforeEach(function () {

    cy.create({model: 'App\\Models\\User', state: {'representative':[],}}).as('regularUser1')
    cy.create({model: 'App\\Models\\User', state: {'representative':[],}}).as('regularUser2')

  });

  it('Example Test Case', function () {
    cy.visit('/')
    cy.log(this.regularUser1.id)
    cy.login({id:this.regularUser1.id})
    cy.visit('/')
    cy.url().should('contain', '/dashboard') // this works
    cy.logout()
    cy.visit('/')
    cy.clearCookies().then(function () {
        cy.login({id:this.regularUser2.id})
        cy.visit('/')
        cy.url().should('contain', '/dashboard') // This works as well now!
    })
    
  })
})

Mushoz avatar Dec 12 '22 15:12 Mushoz