0
 getListOfPlans() {
    return new Cypress.Promise((resolve) => {
      usersUtil.getCurrentCompany().then((company) => {
        cy.request({
          method: 'GET',
          url: `${TrainingPlanApi.baseTrainingPlanApiUrl}/company/${company.id}/plan`,
        }).then((resp) => {
          expect(resp.status).to.eq(200);
          cy.wrap(resp.body.data).as('existingTrainingPlans');
          resolve(resp.body.data);
        });
      });
      if (this.environment === 'local') {
        trainingTrainingPlanListPage.visit();
      } else {
        mainMenu.openTrainingPlans();
        trainingPlanApi.getListOfPlans().then((data) => {
          cy.get(`@existingTrainingPlans`).each((planData) => {
            const plan = planData as unknown as TrainingPlan;
            trainingPlanApi.delete(plan.ksuid);
          });
        });
      }
    }) as unknown as Chainable<TrainingPlanRequest>;

i need to call the above api function in the before:spec

on('before:spec', async (spec) => {
    getListOfPlans
  });

but the above doesnt work, not sure how to solve this please

Laxmi
  • 13
  • 5

1 Answers1

-1

First, you need to link your function to a custom command, in this way:

Cypress.Commands.add('getListOfPlans', getListOfPlans)

Then, you can invoke that command in your before hook, in this way:

Before(() => {
  cy.getListOfPlans()
})

Note: The function and the custom command should be in the same file, or import the function into the file where the command is declared.

Vict01
  • 300
  • 1
  • 10
  • The question is asking how to run the `before:spec` hook. BTW it's not necessary to create a custom command to call the function. – Sariget Oct 14 '22 at 20:33
  • My answer responses how to run the before:spec regardless I think it's a good manner to use a custom command as part of the process. And if you have a better answer you better provide it instead of just criticizing. – Vict01 Nov 02 '22 at 12:29