如何在Java函数中使用数据表?

huangapple go评论63阅读模式
英文:

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

问题

我有一个类似的特性文件:

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

和我的函数看起来是这样的:

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

我想使用一个类似这样的数据表,在我的.feature文件中,但我不确定如何在functionName(???)中调用它:

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

希望这对你有帮助。

英文:

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 |

答案1

得分: 1

我认为你期望Karate像Cucumber一样工作,但它并不是这样工作的。在Karate中没有DataTable这样的东西。

请花些时间了解Karate如何使用JSON数组,以便在数据源上进行循环操作:https://github.com/karatelabs/karate#data-driven-features

还有一种方法可以实现动态的场景大纲,查阅这里:https://github.com/karatelabs/karate#setup

英文:

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

huangapple
  • 本文由 发表于 2023年2月10日 04:57:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75404348.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定