# 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()); ``` - Convert the object into a map in order to access the key and value in each row, use (source: https://bobbyhadz.com/blog/javascript-convert-object-to-map) ``` const dataMap = new Map(Object.entries(table.rowsHash())); ``` - Do not convert the object into whatever, but iterate it directly instead. (source: https://effectivetypescript.com/2020/05/26/iterate-objects/#:~:text=If%20you%20want%20to%20iterate,and%20you%20want%20precise%20types.) ``` 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`