how do I stop a website from automatically logging me out when I'm using cypress?

Viewed 4088

I'm using cypress to automate testing at my companies' website, and upon logging in, clicking on any of the interactive elements immediately logs me out. however, this is only when I run it through cypress. manually clicking around still works. What could I do to fix this?

this is for a website built with javascript.

I expect the website to not log me out.

describe('signing in', function() {
    it('Visits the safe and reliable sign-in page', function() {
        cy.visit('https://testing.safeandreliable.care/sign-in') 
        cy.get('[id="at-field-username_and_email"]').type('bcramer@safeandreliablecare.com')
        cy.get('[id="at-field-password"]').type('******')
        cy.contains('Sign In').click()
  })
  it('signs into the default entity', function(){
      cy.get('[id="help-text-board"]').click({force:true})
      cy.wait(9000)
  })
})

2 Answers

What you should know is that Cypress clears the state of browser every time it starts a new it(). So something what is done in the first it() is not now in the second it(). In your case, the login is in the first it, in the second it the application is not logged in anymore. To take care that Cypress stays logged in, you should move the login step to a before() or a beforeEach() (depends wether you want to login once per describe or per every it.

Following your post it seems you like it to login once and stay logged in, so the before() does the job for you. Your code would look like this:

describe('signing in', function() {
  before('Logijn to the application', function() {
    cy.visit('https://testing.safeandreliable.care/sign-in')
    cy.get('[id="at-field-username_and_email"]').type('bcramer@safeandreliablecare.com')
    cy.get('[id="at-field-password"]').type('******')
    cy.contains('Sign In').click()
  })
  it('signs into the default entity', function(){
    cy.get('[id="help-text-board"]').click({force:true})
    cy.wait(9000)
  })
  it('next test', function () {
    // do other test but still logged in
  })
})
Related