Convert cucumber dataTable into class object in TypeScript

Viewed 32

We have following cucumber test scenario in webdriver.io and cucumber.io framework. We used typescript as the language.

Scenario: can navigate to main page
Given I am on web home page
When I navigate to the next page
Then I can see following person data 
  | title | name | age | 
  | Mr    | John | 35  |

Also we have following Person model class

export class Person {

title: string
name: string
age : number

 } export default new Person();

In our steps we want to read the dataTable and assign to the Class Person.

@when(/^I can see flowing person data$/)
public async icanseeflowingpersondata(table: DataTable) {
Person.title= table.raw.title ;
Person.name = table.raw.name;
Person.age  = table.raw.age ;
}

Rather doing above want to create an instance of Person type from dataTable. I know this is possible in c# as bellow, but want to know how we can do the same in typeScript + WebDriver.io+ Cucumber.io ? I am very new to Webdriver.io and helps much appreciated.

var tableData = table.CreateInstance<Person>();
1 Answers

Generally its much simpler if you don't put the data in your scenarios. Instead encapsulate your data in a named thing and use the name instead. Then you can avoid data tables and translating things entirely resulting in much simpler cukes.

An example scenario might illustrate this for you

Scenario: Sign in
  Given I am registered
  When I sign in
  Then I should be signed in

Notice how there is no mention of username, password etc in the scenario. That is all done further down the stack and so you don't need to process any data tables to get this scenario to pass.

You can apply this to all cuking, no matter how complex. I've cuked for over a decade and now never use data tables.

I understand this might not be the answer you were looking for, I'm just providing you an alternative approach to consider. I hope its useful

Related