Skip to main content

PARAMETERIZATION IN CYPRESS

 Tutorial 8 – Parameterization in Cypress

Adding the PDF link as the images are not fully loaded:

https://docs.google.com/document/d/1jS0DNytAiMntqVN5fhHRjheA_CCPWCtMAaNEoxi6kjw/edit?usp=sharing


What you will Learn :
Data driven testing using fixtures (parameterization)

  • 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!

Comments

Popular posts from this blog

Add, remove, search an item in listview in C#

Below is the C# code which will help you to add, remove and search operations on listview control in C#. Below is the design view of the project: Below is the source code of the project: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Treeview_control_demo {     public partial class Form2 : Form     {         public Form2()         {             InitializeComponent();             listView1.View = View.Details;                   }         private void button1_Click(object sender, EventArgs e)         {             if (textBox1.Text.Trim().Length == 0)...

display files and directories in Listview

Below is the C# code which displays all files and directories in listview control with their file size and creation date. If it is file then it also displays the extension of the file e.g. .txt, .jpg etc Below is the design view of the project: Listview to display files and directories with size and date created Below is the source code of the project: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; namespace search_in_listview {     public partial class Form1 : Form     {         public Form1()         {             InitializeComponent();                   }         private void button1_Click(object sender, EventArgs ...

Add worklog in Jira using Python

 Below is the Python code to add the worklog in Jira. You need to install a request library for this. Here is the code: import requests from requests.auth import HTTPBasicAuth import json url = "https://your jira address here/rest/api/2/issue/ticket_number/worklog" auth = HTTPBasicAuth("username", "jira access token") headers = {     "Accept": "application/json",     "Content-Type": "application/json" } payload = json.dumps({     "comment": {         "content": [             {                 "content": [                     {                         "text": "This is for QA Testing",                         "type": "text"                     } ...