Try   HackMD

Typescript + Cucumber data table

https://www.sitepoint.com/bdd-javascript-cucumber-gherkin/
https://vinodmuralidharan.medium.com/cucumber-js-data-table-guide-a2447b74c86e

Different between the data tables and Examples

At least two columns

.feature file

Scenario: Category dropdown
        When the user navigates to the home page
        When the user selects a category from the category dropdown:
            | category    | value  |
            | all         |        |
            | A           | a      |
            | B           | b      |
            | C           | c      |

.step file

Get the object form of the test data

table.rowsHash();
  • Print the hash result object in Typescript, use
JSON.stringify(table.rowsHash());
const dataMap = new Map(Object.entries(table.rowsHash()));
const dataObject = table.rowsHash();
for (const k in dataObject){
    console.log('key: ' + k+ ', value: '+data[k]);
}

Only one Column

.feature file

Scenario: Page url contains the selected category in the category filter
    When the user select the category in the category filter:
        | category        |
        | Cat             |
        | Dog             |
        | Bird            |
    Then the url of the page contains the selected category:
        | category        |
        | Cat             |
        | Dog             |
        | Bird            |

.step file

Use table.raw(); , and needs to access each row using index instead of using let element of elements . And the first index of the loop needs to be 1 instead of 0, because needs to exclude the column name, which is category.

When(
  /^the user select the category in the category filter:$/,
  function (table) {
    const categories = table.raw();
    for (let i = 1 ; i< categories.length ; i ++){
      console.log(' data.category ' + categories[i]);
      expect(category).to...();
    }
  }
);
tags: typescript webdriverio bdd cucumber