How to Bypass login in Cypress + Cucumber

Viewed 574

I am trying to Bypass UI login using Cypress with Cucumber. I have added cy.session in my Cypress.Commands.add(login) method. But when I am trying to execute the scripts, clearing the session in each step in scenario file. Ex:

Feature: Login
Scenario: Validate Login
Given Launch the URL And Login With Valid Credentials
When Login With Correct Username and Password
Then Verify Manage Home Page

In above scenario session is clearing in each step, like Given, When, Then. I need to clear after each scenario in the feature file. Please help me on this

Cypress.Commands.add('login', (username, password) => {
  cy.session([username, password], () => {
    cy.visit('/')
    cy.get(selectors.user_input,{ timeout: 60000 }).type(username)
    cy.get(selectors.login_button, { timeout: 60000 }).should('be.visible')
    cy.get(selectors.login_button).click()
    cy.wait(2000)
    cy.get(selectors.password_input, { timeout: 60000 }).should('be.visible')
    cy.get(selectors.submitButton, { timeout: 60000 }).should('be.visible')
    cy.get(selectors.password_input).type(password)
    cy.get(selectors.submitButton).click()
    cy.get(selectors.homePage, { timeout: 60000 }).should('be.visible')
  })
})

Or, please help me how to avoid login in each scenario in feature file.

1 Answers

Did you try putting the login feature into beforeEach?

That is how I use cy.session:

describe('...', () => {

    beforeEach(() => {
        cy.login()         
    })

    it('test a', () => {

    ...
    })
...
})

{{edit}} 2nd possibility is to preserve Cookies to stay logged in:

describe('...', () => {
beforeEach(function () {
        cy.preserveAllCookiesOnce()
    })
it('test a', () => {

    ...
    })
...
})
Related