How to read key/value pairs from data table

Consider you have a feature file containing a dataTable of field/value pairs:

Can create role:
Expand Collapse Copy
Given I login as user TestUser1
    When I create the role
        | field         | value             |
        | name          | My First Role     |
        | description   | Hello World       |

If you want to read the table values out by keying on name, and description, there are a number of ways to achieve this, but one way is to transpose the dataTable:

What does "transpose" table mean?

"Transposing a dataset means swapping its rows and columns so that the rows become columns and the columns become rows"

my.steps.ts:
Expand Collapse Copy
import { When } from '@cucumber/cucumber';

When('I create the role', async function (this: ICustomWorld, dataTable) {
    dataTable.transpose().hashes().map((row) => {
    console.log('name:', row.name);
    console.log('description:', row.description);
});

Console output:
Expand Collapse Copy
name: My First Role
description: Hello World
 
Last edited:
Back
Top