How to run a test multiple times with different sets of data in cypress?

Viewed 3658

While automating a website, I have a requirement to run a test case(it block) multiple times with different set of testdata in cypress.

Please consider the below example :

it('example test',  () => {

    //first run
    getOnDefaultForm.typeUserName('Name1');
    getOnDefaultForm.typePassword('Pass1');
    getOnDefaultForm.clickSubmit();
    
    //second run
    getOnDefaultForm.typeUserName('Name2');
    getOnDefaultForm.typePassword('Pass2');
    getOnDefaultForm.clickSubmit();
    
    //third run
    getOnDefaultForm.typeUserName('Name3');
    getOnDefaultForm.typePassword('Pass3');
    getOnDefaultForm.clickSubmit();

});

How can I achieve this in Cypress?

1 Answers

I think you need to have a look as this repo: https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/fundamentals__dynamic-tests Or just search this site, this is not the first time someone has asked this very question.

In general, you can wrap your it in a loop. In practice, it'd look e.g. like this:

const testData = [
    {
        name: 'Name1',
        password: 'Pass1'
    },
    {
        name: 'Name2',
        password: 'Pass2'
    },
    {
        name: 'Name3',
        password: 'Pass3'
    }
]

testData.forEach((credentials) => {
    it('example test', () => {
        getOnDefaultForm.typeUserName(credentials.name);
        getOnDefaultForm.typePassword(credentials.password);
        getOnDefaultForm.clickSubmit();
    })
});
Related