Search code examples
javacucumberkarate

Karate: How to use a datatable in a java function?


I have a feature file that looks like:

* def javaFunction = Java.type('my.javaClass');
* def generateFile = new javaFunction().functionName(???)

And my function looks like

public String functionName(DataTable dm){
  for (DataModel aDataModel : dm) {
      String username = aDataModel.user;
      String password = aDataModel.password;
  }
}

I want to use a Data table that looks like this but I'm not sure how to call it in the functionName(???) in my .feature file

|user       |password |
|cucumber1  |cucumber |
|cucumber2  |cucumber |

Solution

  • I think you are expecting Karate to work like Cucumber, no that's not how it works. There's no such thing as a DataTable in Karate.

    Please spend some time how Karate uses JSON arrays where you want to loop over a data source: https://github.com/karatelabs/karate#data-driven-features

    And there is a way to do dynamic scenario outlines, look it up: https://github.com/karatelabs/karate#setup