Step Table
When there is a data table in the step (note that it is different from the sample table of the scene outline), the data table will be passed as an object, and the method of accessing the object will be followed according to whether the output contains title information **There are two categories:
- Contains header information for each column
hashes()
: returns an array of objects in which each row is converted to an object (that is, a key-value pair in which the column heading is the key and the content is the value).raw()
: Return the table as a two-dimensional array.
- No column header information
rowsHash()
: returns an object, where the attribute name of each attribute is the data in the first column of the table, and the attribute value is the data in the second column (or the first column is the key and the second column is the value). (Note: This method is suitable for data tables with only two columns).rows()
: returns a two-dimensional array, each row in the data table forms a one-dimensional array, and does not return the header row.
Suppose there is a data table as follows in the step:
id | password |
---|---|
user01 | 123456 |
user02 | 123456 |
user03 | 123456 |
Then pass the data into the parameter table
, and output the results of the four reading methods of the data table:
JavaScript
let table_hashes = table.hashes();
console.log("table.hashes():", table_hashes);
let table_raw = table.raw();
console.log("table.raw():", table_raw);
let table_rowsHash = table.rowsHash();
console.log("table.rowsHash():", table_rowsHash);
let table_rows = table.rows();
console.log("table.rows():", table_rows);
Output:
JavaScript
table.hashes(): [ { id: 'user01', password: '123456' },
{ id: 'user02', password: '123456' },
{ id: 'user03', password: '123456' } ]
table.raw(): [ [ 'id', 'password' ],
[ 'user01', '123456' ],
[ 'user02', '123456' ],
[ 'user03', '123456' ] ]
table.rowsHash(): { id: 'password',
user01: '123456',
user02: '123456',
user03: '123456' }
table.rows(): [ [ 'user01', '123456' ],
[ 'user02', '123456' ],
[ 'user03', '123456' ] ]
For examples, please refer to the [Script] sample of Cucumber.js on github (https://github.com/cucumber/cucumber-js/blob/master/features/data_tables.feature).
Chinese version click here.