1

I have some global variables on my cypress test scenario.

    describe('AutoLogin Test Case',function(){
        beforeEach(function(){
            Cypress.Cookies.preserveOnce('_session_id')
        })
        afterEach(function(){
            cy.get('[id="ajax_working"]',{timeout:6000}).should('not.be.visible')
        })
    
    
        it('input login info',function(){ 
            cy.visit('https://***********.******.com/')
            cy.get('[id^=user_username]')
            .type('ChrisPbacon').should('have.value','ChrisPbacon')
            cy.get('[id^=user_password]')
            .type('welcome123').should('have.value','welcome123')
            cy.contains('Sign In Now').click()
        })
})

After the test case is completed the system is gonna check for the "after each" function and look for "ajax_working"... I need to skip that check ONLY on the shown "it" test, but I still need to run it on the rest of the program. I don't wanna write the aftermath function on each test as it's cumbersome and overall not clean. Anyone got any tips?

Bruno Rodriguez
  • 65
  • 1
  • 1
  • 8

1 Answers1

0

Not sure if this will fit your problem, but one of the ways you could achieve this is to wrap afterEach hook in the same context with tests than need it using describe declaration, like this:

beforeEach(function () {
    cy.log("Before each hook");
});

describe('AutoLogin Test Case',function() {

    afterEach(function () {
        cy.log("After each hook");
    });

    it('Some test', function () {
        cy.log("Your first test case")
    });

});

describe('AutoLogin Test Case 2',function() {
    it('Some other test', function() {
        cy.log("Your second test case");
    });
});

Note the output which has your expected behavior, beforeEach hook called in both tests, while afterEach hook is called only on first test:

enter image description here

Zaista
  • 1,272
  • 8
  • 18