0

I have fixture file named alerts-escalation-param.params.json where I store operations with their fixture path file ,then I have a function command getOperationData that get the operation data to use it in the test cases just by calling the operation name .
so the problem that I have is that when I start the tests , in the case when I start using this function getOperationData I get this error:
cy.each() can only operate on an array like subject. Your subject was: Object{4}Learn more

here is the function getOperationdata:

Cypress.Commands.add('getOperationData', (mode, operationsArrayName = 'operations') => {
        cy.fixture('alerts-escalation-param.params.json').then((params) => {
          const fixturePath = params[operationsArrayName];
          cy.fixture(fixturePath).then((data) => {
            const opData = data.filter((element) => element.mode === mode);
            Cypress.env('fixtureData', opData);
          });
        });
      });

and here is the case test where I use it :

it.only('should do a search by criteria', () => {
    cy.getOperationData('search').each((searchCase) => {
      cy.setDataAndSearch(searchCase.criteria)
      if (searchCase.expectedRowsCountResult === 0) {
        cy.should(
          'equal',
          cy.getdefaultOperationExpectedValue()
            .numberOfColumnsOnEmptyRow
        )
      } else {
        cy.should(
          'equal',
          searchCase.expectedRowsCountResult
        )
        cy.searchAndGoToDetails(searchCase.criteria)
        for (const dataKey in searchCase.afterOpCriteria) {
          cy.should(
            'equal',
            searchCase.afterOpCriteria[dataKey]
          )
        }
        cy.back()
      }
    })
  })
Youssef
  • 1
  • 1

2 Answers2

2

You should be able to make it work by returning or wrapping the internal results of the custom command

Cypress.Commands.add('getOperationData', (mode, operationsArrayName = 'operations') => {
  cy.fixture('alerts-escalation-param.params.json').then((params) => {
    const fixturePath = params[operationsArrayName];
    cy.fixture(fixturePath).then((data) => {
      const opData = data.filter((element) => element.mode === mode);

      cy.wrap(opData);   // this makes the data subset the "subject"
    });
  });
});

cy.getOperationData('search')   // subject is unwrapped and passed to next command
  .each((searchCase) => {
    ...
0

Presumably, your cy.getOperationData() does not yield anything to the .each(). Instead, after running cy.getOperationData() to set the Cypress.env() variable, you can use the data set from that to run the .each().

cy.getOperationData()
  .then(() => {
    cy.wrap(Cypress.env('fixtureData')).each((searchCase) => {
      // code here
    });

  });
agoff
  • 5,818
  • 1
  • 7
  • 20