Iterating a Cypress Test Multiple times based on the number of records on fixture file

 Iterating a Cypress Test Multiple times based on the number of records on fixture file

Sometimes we may need to perform a test to run multiple times based on the test data available on a fixture file. This post looks at how this iterative test can be achieved.

Lets assume we have a fixture file within our Cypress framework as below:

{
  "invoices":
    [
    {"id": "ABCD1234","Amount": "10.00","Date":"30-02-2022","Reference": "Test1"},
    {"id": "ABCD4567","Amount": "20.00","Date":"30-02-2022","Reference": "Test2"}
   ]
}

As shown above, the fixture file has an array of data with parameters of id,amount and a reference. Our objective is to get the data from each row and pass it to the test as parameters.

   it.skip('TC_01_User Login and Pass data from fixture file',function(){

    cy.fixture('Invoices').then(function (data){
         data.invoiceData.forEach( (arrayData)=>{
        cy.login(this.Records.UserName,this.Records.PassWord)
        invoiceMenu.clickCollectiveInvoice()
        invoiceMenu.getSearchField(arrayData.id)
        invoiceMenu.getClickSearchButton()
        invoiceMenu.clickThreeDotMenu()
        invoiceMenu.clickAddPaymentsBtn()
        invoiceMenu.getAmount(arrayData.Amount)
        invoiceMenu.getDate(arrayData.Date)
        invoiceMenu.getReference(arrayData.Reference)
        invoiceMenu.getAddPaymentBtn()
        invoiceMenu.getLogoutButton()

         })
        })
   })

As shown above the test reads the fixture file titled Invoices that contains the array
of data and then using the forEach we loop through each line of the the fixture file.
Next, we call the parameters within the fixture file as arrayData.id, or arrayData.Amount
and pass to the relevant text fields.

Note how the cypress page objects are called within the forEach loop. This way, when this
test is invoked it will be executed multiple times for the number of rows that are
within the fixture file.

Comments