Cypress.io

Cypress Data Validation Tutorial & Strategy

Create automation tests with data required also validate them to make sure the data was added or generate successfully, In this post I’m going to show you my Cypress data validation strategy.

Why Is Important?

Let’s take a simple example with CRM App, we have an option to create a new lead manually and we will type the lead name and lead email.

The test can be done when pressing the save lead button.
But is so important to validate that the lead was created and everything is working as expected.
This check is also known as Data Validation, we would like to validate that the lead name and email that we typed is the same as added to the CRM all leads list.

This data validation strategy can be implemented also with the Page Object Model, you can read more about it here – How To Use Page Object Model In Cypress

The Issue And The Solution

The issue is if we will type the same data for each time the test is run, we will have data conflict.
Because each test generates the same lead with the same data over and over again, cypress will not be able to know if the lead was generated successfully – there are multiple leads with the same data.

The solution is to generate different data on each test that will ensure the lead is unique and there is no other lead with the same data.

Code Example

First, we will click on the create new lead button and then get the lead name field and type some random string to make sure it stays unique.

        cy.get('[data-testid="creatNewLeadButton"]').click()

        cy.get('[name="name"]').type()

To write some random string I created a function that will generate it for us.
We will use the invoke command to invoke the data that was generated and we will also save it in a variable to be able to validate it later on the all leads list page.
I used the console.log to print the invoke data to the console just to make sure it worked.

      it('CRM Test',() => {
        
		let info
		cy.get('[name="name"]').type(leadName_Alpha()).invoke('val')
        .then((value) => {
            info = value;
            console.log(value)

        })

    	function leadName_Alpha() {
        var text = "";
        var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123456789";

        for (var i = 0; i < 10; i++)
            text += possible.charAt(Math.floor(Math.random() * possible.length));

        return text;
          
    	}
        
        })

To validate the lead name we generated I will get the lead name element in the all leads list and return the variable.

                cy.then(() => {

                        return cy.get('[data-testid="leadName"]').should('contain', info)

                })

In this strategy, we can be sure that our test also validates the data he generated.

Hope this was helpful for you, let me know what you think in a comment below.

Ofir Blumental

QA Automation Engineer - ofirblumental1@gmail.com

Related Articles

2 Comments

  1. Isn’t this strategy will fill up the database with a bunch of test data? Why not just delete (if exists) the lead with the test data details prior the test (ideally via API call)?

    1. Depends on what env do you test in, it’s not recommended to delete data from production.
      Deleting in most of the time is not an option and in most cases deleting not really delete the data from DB.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button