Reputation: 101
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?
Upvotes: 10
Views: 11886
Reputation: 199
I know this is old but I was looking for the same thing and I couldn't find the good option for me. I needed to read the CSV file and use the same test case with different set of data (actually, what DDT is about, real parametrization). I found the best answer here: https://github.com/cypress-io/cypress-example-recipes/tree/master/examples/fundamentals__dynamic-tests-from-csv
So what you need to do is:
// imports
const fs = require('fs')
const path = require('path')
const neatCSV = require('neat-csv')
// the func itself
async setupNodeEvents(on, config) {
const filename = path.join(__dirname, 'cypress/fixtures/contact.csv')
const text = fs.readFileSync(filename, 'utf8')
const csv = await neatCSV(text)
config.env.usersList = csv
return config
}
describe('Data-driven testing from CSV file', () => {
const csvUsers = Cypress.env('usersList')
csvUsers.forEach(user => {
it(`Fill input fields from the CSV file - user "${user.name}"`, () => {
cy.visit('https://your-app/')
cy.get('#some-id1').type(user.name)
cy.get('#some-id2').type(user.email)
})
})
})
Upvotes: 0
Reputation: 227
It is also possible to put the data in a json
file in the fixtures folder, and import it at the top of the spec file.
This methodology is still working in Cypress version 12.5.0.
fixture
[
{ "name": 'Name1', "password": 'Pass1' },
{ "name": 'Name2', "password": 'Pass2' },
{ "name": 'Name3', "password": 'Pass3' }
]
test
const testData = require('../fixtures/test-data.json')
testData.forEach((credentials) => {
it('example test for ' + credentials.name, () => {
getOnDefaultForm.typeUserName(credentials.name);
getOnDefaultForm.typePassword(credentials.password);
getOnDefaultForm.clickSubmit();
})
})
Upvotes: 14
Reputation: 8322
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();
})
});
Upvotes: 11