Problem with a parametrized test with cypress.io

Viewed 395

I want want to start a parametrized dynamic test with cypress.io. The parameters for this are in a file called params. In before the variable params get the parameters from the file, but the forEach loop loops over the intial value 1 and 2. Can anbody give me an hint to solve the problem?

describe('Suite', () => {

    let params=[1,2];

    before(() =>{

        cy.readFile('cypress\\fixtures\\params.txt').then( data=> {

            params = data;
            params= params.replace(/(\r\n)/gm, "\t");
            params = params.split('\t');
            cy.log(params);
          })
    })

    params.forEach( param  =>{

        it('Testing with '+param+':', ()=>{

            ...
        })

    });

})
1 Answers

How bout this: In fixtures params.json file:

[
  {
    "name": "Test 1",
    "something": "Some text"
  },
  {
    "name": "Test 2",
    "something": "Some other text"
  }
]

And in integration test.spec.js file:

const params = require('../fixtures/params.json');

params.forEach((test) => {
    it(test.name, () => {
        cy.log(test.something);
    });
});

And this is the result: enter image description here

Related