Data-driven Using Cypress + BDD

Pradap Pandiyan
3 min readOct 31, 2022

--

Data-driven is one of the core concepts of test automation which help us in structuring the test data based on our requirements. When we have a huge amount of test data, it is difficult to manage and data-driven help us to structure it.

In cypress, we can use same set of data and generate test data for both API and UI test automation.

There are multiple way of using the data-driven methodology in different files are as follows:

  1. JSON file usage
  2. Excel file usage
  3. .txt file usage
  4. .config file usage
  5. SQL database usage

Example of JSON file to read data for the data-driven:

{
"name": "Using fixtures to represent data",
"email": "hello@cypress.io",
"body": "Fixtures are a great way to mock data for responses to routes"
}

Reading test data from a JSON file and printing the data based on the field name:

cy.fixture('example.json').then((user) => {
cy.log(user.name)
})

Generate a JSON file and add fields to the JSON file:

cy.writeFile('cypress/fixtures/writeJson.json', { firstname: 'Pradap', lastname: 'Pandiyan' })

On the API request and response, it would be great, if we save all the responses as JSON files. This helps in keeping the evidence of the response file.

Reading test data from a .txt file and printing the data based on the field name.

cy.readFile('cypress/fixtures/readMe.txt').then(obj => {
cy.log(obj);
});

Example of .txt file to read data for the data-driven:

cy.writeFile('cypress/fixtures/writeFile.txt', 'medium.com')

Reading test data from an excel file and printing the doing assertion based on the count.

cy.task('readXlsx', { file: 'cypress/fixtures/sheet.xlsx', sheet: "Test" }).then((rows) => {
expect(rows.length).to.equal(199)
})

I have created a BDD for the test, based on the operation. This is actually based on the read-write mechanism

Feature: Data Driven Test for multiple cases

Scenario: Read data to a json file
Given Read data from the json file for the field name
Then Read data from the json file for the field email
And Read data from the json file for the field body

Scenario: Write data to a json file
Given Write data from the json file for the field name

Scenario: Read data from a txt file
Given Read data from the txt file

Scenario: Write data from a txt file
Given Write data to a txt file

Scenario: Read excel file
Given Read data in excel file

Scenario: write excel file
Given Write in excel file xlxs

I have created a step definition file and it will help to build the steps for Step definition.

import {Given,Then} from "cypress-cucumber-preprocessor/steps";

Given(/^Read data from the json file for the field name$/, function () {
cy.fixture('example.json').then((user) => {
cy.log(user.name)
})
});
Then(/^Read data from the json file for the field email$/, function () {
cy.fixture('example.json').then((user) => {
cy.log(user.email)
})
});
Then(/^Read data from the json file for the field body$/, function () {
cy.fixture('example.json').then((user) => {
cy.log(user.body)
})
});
Given(/^Read data from the txt file$/, function () {
cy.readFile('cypress/fixtures/readMe.txt').then(obj => {
cy.log(obj);
});
});
Given(/^Write data to a txt file$/, function () {
cy.writeFile('cypress/fixtures/writeFile.txt', 'medium.com')
});
Given(/^Write data from the json file for the field name$/, function () {
cy.writeFile('cypress/fixtures/writeJson.json', { firstname: 'Pradap', lastname: 'Pandiyan' })
});
Given(/^Read data in excel file$/, function () {
cy.task('readXlsx', { file: 'cypress/fixtures/sheet.xlsx', sheet: "Test" }).then((rows) => {
expect(rows.length).to.equal(199)
})
});
Given(/^Write in excel file xlxs$/, function () {
cy.writeFile('cypress/fixtures/testsheet.xlsx', 'Hello','UTF-8')
});

Added the cypress logged for all the test from the step definition.

output file

I have created a project on GitLab and added the code here.

Feel free to hit clap if you like the content. Happy Automation Testing :) Cheers. 👏

--

--

Pradap Pandiyan

I’m a passionate QA Engineer. I’m a motovlogger, content creator, off-roader and freelancer. Buy me a coffee here https://www.buymeacoffee.com/pradappandiyan