1

Suppose I have the following spec containing 2 it blocks, one checks the menu items for teacher role, and another one checks for student role. The variable rolesToCheck contains the roles on which basis it block will be activated, Currently, the rolesToCheck variable only contains the student hence it will run only the second it block. Is it possible to do so?

   describe('side menu check', () => {
    var sideMenuHelper;
    var rolesToCheck = ['student'];
    beforeEach(() => {
        cy.login();
        cy.wait(5000);
    });

    it('menu item check for teacher', () => {
        .....
    })

    it('menu item check for student', () => {
        .....
    })

})

3 Answers3

2

Please see this question How to add test case grouping in Cypress which can be adapted to use the rolesToCheck array.

const rolesToCheck = ['student'];

beforeEach(function() {

  const testName = Cypress.mocha.getRunner().test.fullTitle();
  if (!rolesToCheck.some(role => testName.includes(role))) {
    this.skip();
  }
})

it('menu item check for teacher', () => {        // skipped
    .....
})

it('menu item check for student', () => {        // run
    .....
})

You may find it is a little difficult to filter by test name when there's a lot of tests, so instead you could use tags

const rolesToCheck = ['student'];

beforeEach(function() {

  const tag = Cypress.mocha.getRunner().test._testConfig?.tag;
  
  if (!tag) return;    // no tag, run it 

  if (!rolesToCheck.includes(tag)) {
    this.skip();
  }
})

it('menu item check for teacher', { tag: 'teacher' }, () => {     // skipped
    .....
})

it('menu item check for student', { tag: 'student' }, () => {     // run
    .....
})

Note the optional chaining _testConfig?.tag in case there's no tag defined on the test.

  • Inside the beforEach block this.skip() throws an error showing this.skip is not a function. Can you tell me the reason behind it? – Ashikur Rahman Nabir May 06 '21 at 06:39
  • 2
    I think you have used an arrow function `beforeEach(() => {`, but above I have `beforeEach(function() {`. The Mocha library (which Cypress uses) recommends not to use arrow functions, preferring "normal" functions so that `this` can be used. –  May 06 '21 at 08:02
1

You can loop over the rolesToCheck array and run your tests based on roles in your array.

describe('side menu check', () => {
    var sideMenuHelper;
    var rolesToCheck = ['student'];
    beforeEach(() => {
        cy.login();
        cy.wait(5000);
    });

    rolesToCheck.forEach(role => {
        it(`menu item check for ${role}`, () => {
            .....
        })
    })    
})
Dharman
  • 30,962
  • 25
  • 85
  • 135
Sifat Moonjerin
  • 180
  • 1
  • 12
1

You can loop through the rolesToCheck array and login based on the role to check the menu items for that role instead of using different it block for different role.

You can do something like this.

const rolesToCheck = ['teacher', 'student'];
rolesToCheck.forEach((role) => {
    describe('side menu check', () => {
        before(() => {
            if (role === 'teacher') {
                login as teacher
            } else if(role === 'student') {
                login as student
            }
        });

        it(`menu items check for ${role}`, () => {

        });
    });
});