
Let us consider below table as an example, and in this example we need to get the value from the Columns(Contact/Country) based on the Company name.

Below is the HTML structure for the table
<table>
<tr>
<th>Company</th>
<th>Contact</th>
<th>Country</th>
</tr>
<tr>
<td>Alfreds Futterkiste</td>
<td>Maria Anders</td>
<td>Germany</td>
</tr>
<tr>
<td>Centro comercial Moctezuma</td>
<td>Francisco Chang</td>
<td>Mexico</td>
</tr>
<tr>
<td>Ernst Handel</td>
<td>Roland Mendel</td>
<td>Austria</td>
</tr>
<tr>
<td>Island Trading</td>
<td>Helen Bennett</td>
<td>UK</td>
</tr>
<tr>
<td>Laughing Bacchus Winecellars</td>
<td>Yoshi Tannamuri</td>
<td>Canada</td>
</tr>
<tr>
<td>Magazzini Alimentari Riuniti</td>
<td>Giovanni Rovelli</td>
<td>Italy</td>
</tr>
</table>
Before start writing cypress code, lets make a note of what we have as parameter and we need to find out the value we need.
We will have Company name and Column name as parameter
We need to find the index of the column
We need to find the company and then get the value of the column(based on the index we got) in the company row
Example: If i want to know the Contact Information of Company “Centro comercial Moctezuma”, then i need to Pass the Company Name and Column Name as parameter to my code which should return the value “Francisco Chang”
Below would be the code for this in cypress

Now in order to make this reusable we can create a custom command, which accepts two parameters, Company and Column Name and returns the value, which will be like below.

And our test will be like

This is one of the way and there can be other ways too. If you find better way, feel free to add it as comment.

Leave a comment