Learn about Parameterization (Data driven testing using fixtures) in Cypress

Tutorial 8 – Parameterization​​ (Data driven testing using fixtures)​​ in Cypress

What you will Learn :

  • What is a fixture​​ and what is data driven testing

  • Where do we create a fixture file?

  • Prepare fixture file

  • Automate a simple flow

  • Load the fixture file​​ using ‘before’ hook

  • Parameterize the test and run it

What is a fixture​​ & what is ‘Data driven testing’

We should never hard code our test data (example, abc@gmail.com) inside the code. The reason being, if you hard code the test data inside your script & it is being used at 10 places, tomorrow if the test data changes for any reason (example,​​ abc@gmail.com​​ changes to abc1@gmail.com), you have to change your code at 10 places. This is not the correct practice. This would make your code error prone & is time consuming too. Instead, we should keep our test data in a central file location & access that file inside our test script.

So, a​​ lot times, we will have common test data that is required in multiple test cases. In that case, we can just define the test data in a fixture file and we can re-use the fixture file in multiple tets cases.

So, a​​ fixture is a file containing​​ test​​ data. This data we can use in our test cases.​​ This is called as data driven test. The test will be driven by data.

The data can be in different formats (json, excel, csv​​ etc..).

You can refer the cypress documentation

https://docs.cypress.io/api/commands/fixture.html#Syntax

Where do we create a fixture file?

We can create a fixture file under ‘fixtures’ folder

If you click the example.json, you will understand. You can see test data being written over here

Prepare fixture file

Let us modify the example .json file and write 2 key value pairs. Here,​​ firstname, lastname, address, email, phone​​ are the keys and their values are the ‘values’.

Save the file.

So now we will use this data in our test case.

Automate a simple flow

We will now launch​​ http://demo.automationtesting.in/Register.html​​ and will enter the values in the below 5 fields using cypress​​ 

Below are the respective 5 elements

Create a test. We have used [attribute=value] combination to locate the elements

Save the file & Run the tets using test runner. Notice that the 5 fields are populatedwith the respective values​​ 

Load the fixture file​​ using ‘before’ hook

https://docs.cypress.io/guides/core-concepts/writing-and-organizing-tests.html#Hooks

The function inside the ‘before’ hook would run once before all tests in the block.​​ You can see​​ the syntax of ‘before’ hook

Let us copy the synatx

Paste it under ‘describe’ block (before the ‘it’ block)

Next we will load the fixture file using​​ cy.fixture(‘name of the file’)​​ method and then we will load its test data using​​ .then((data)​​ =>​​ ​​ method, see line 6

So what line#6 does is: we are loading the fixture file and then saving the test data in the variable ‘data’.​​ 

Next let us save this test data in another variable ‘userDetails’. To do that, we will first define this variable at the global level (outside the ‘before’ block), see line 4​​ below​​ 

Now we can use this variable, see line 7

Since the variable ‘userDetails’ was created at the global level, it​​ is​​ also​​ accessible to​​ all the​​ test cases​​ inside the ‘it’ block.​​ 

Parameterize the test and run it

Now we can remove the hard coded test data values from lines 11-16. Instead we will use the variable​​ userDetails.<name of the key>

Save the file. Notice that the test data is called from the fixture file during runtime & is populated in the respective fields

This is how we can use fixtures to drive our test from an external data file.​​ 

Thanks for reading!

Share On

Share on facebook
Share on twitter
Share on linkedin
Share on whatsapp
Share on tumblr
Share on email

Leave a Comment

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

Scroll to Top

Lifetime Membership Club

LIFETIME MEMBERSHIP BIG SALE - ALL LIVE COURES JUST - 7000 RS/ 99 USD
Attend All Live courses in just 7000 rs / $99 - offer ends 31st DEC 2024