Mr.Prasad J
Mr.Prasad J

Reputation: 95

I am unable to use saved cookies cy.session() in my 'it' block

I tried to use the saved session in the 'it' block, but it's opening the "baseurl/index.php/dashboard" so it means opening the login page afresh. I am trying to use the saved cookies to directly visit or use in another 'it' block.

import testdata from '../../fixtures/testdata.json'
describe('Example to demonstrate the use `each` in Cypress', function () {
    const loginPage = (username, password) => {
        cy.session([username, password], () => {
            cy.visit('/') // baseUrl: "https://opensource-demo.orangehrmlive.com/"
            cy.get('#txtUsername').type(username)
            cy.get('#txtPassword').type(username)
            cy.get('#btnLogin').click()
        })
    }
    beforeEach('Loading test data', function () {
        loginPage(testdata.username, testdata.password)
    })
    it('Validating the Quick Launch menu', function () {
        cy.visit('/index.php/dashboard')
        cy.get('.quickLaungeContainer').each(($el, index) => {
            expect($el).to.contain(this.testdata.quickLaunch[index])
        })
    })
})

Upvotes: 0

Views: 800

Answers (1)

Fody
Fody

Reputation: 31872

The code looks like it should work, you have username twice instead of password but I assume that's a typo.

In case the cookie isn't set as expected you can add a validate function to help you debug the cy.session()

See Gleb's video Use cy.session Command To Prepare Test Data But Only When Needed, about 4 minutes in is the TLDR point.

The idea is to use validate() to assert the condition you assume will be true after the setup() has run.

In this case, just want to check a cookie has been set on the domain. If it does not happen, the setup() tries again and if still not passing validate() the test fails.

It's also useful if one of your tests deliberately removes the cookie, then the next test will cause it to be re-fetched.

testdata.json

{
  username: ...,
  password: ...,
  quicklaunch: [...],
  cookieName: 'orangehrmlive'  // check actual name in devtools
}

Test

import testdata from '../../fixtures/testdata.json'

describe('Example to demonstrate the use `each` in Cypress', function () {
    const loginPage = (username, password) => {
        const setup = () => {
          cy.visit('/') // baseUrl: "https://opensource-demo.orangehrmlive.com/"
          cy.get('#txtUsername').type(username)
          cy.get('#txtPassword').type(password)
          cy.get('#btnLogin').click()
        }
        const validate = () => {
          cy.getCookie(testdata.cookieName).should('exist')
        }
        cy.session([username, password], setup, {validate})
    }
    beforeEach('Loading test data', function () {
        loginPage(testdata.username, testdata.password)
    })
    it('Validating the Quick Launch menu', function () {
        cy.visit('/index.php/dashboard')
        cy.get('.quickLaungeContainer').each(($el, index) => {
            expect($el).to.contain(this.testdata.quickLaunch[index])
        })
    })
})
 

Upvotes: 3

Related Questions