我对此进行了一些搜索,但找不到确切的示例。我有一个表格要填写作为一个步骤。表单字段看起来像这样:
日期:
货币:
总计:
说明:
但并非所有字段都需要用户输入数据。而不是像这样编写多种方法来解释不同的组合:
(When I enter the 'Date' and 'Currency' and 'Total' and 'Description')
(When I enter the 'Date' and 'Total')
(When I enter the 'Currency' and 'Description')
etc...
我想以某种方式在功能文件中实现这样的功能:
When I enter the following details:
|Date |x |
|Currency |USD |
|Total |100 |
|Description |Test |
然后使用一种方法来处理用户在第二列中输入的任意数据组合。
我找到了具有此数据表驱动示例的网站:
When I enter the following details:
|Date |<date> |
|Currency |<currency> |
|Total |<total> |
|Description |<description> |
Example data:
|date |currency |total |description |
|x |USD |100 |foo |
|y |EUR |200 |test |
|z |HKD |124 |bar |
但这不是我所追求的。我不需要遍历预先确定的示例数据列表。我希望我已经足够清楚地总结了问题,并且有人知道可以找到此类实现示例的好地方。感谢您的任何建议!
最佳答案
是的,您可以使用数据表作为单个非重复步骤的参数。数据表的第一行必须是表头:
When I enter the following details:
|Name |Value|
|Date |x |
|Currency |USD |
|Total |100 |
|Description |Test |
这是在一个步骤中使用它的一种可能方法:
@Given("^I enter the following details:$")
public void i_enter_the_following_details(Map<String, String> details) throws Throwable {
for Map.Entry<String, String> entry : details.entrySet() {
String key = entry.getKey();
String value = entry.getValue();
switch (key) {
case "Date":
// add the date to the form
break;
// ...
}
}
}
您还可以将表格作为 DataTable
获取, 一个 List
值对象,一个 List<List<String>>
或 List<Map<String>>
通过声明具有该类型的参数。 Map<String, String>
这里似乎最简单。
我以这种方式编写示例,因为我假设您需要编写不同的代码来将每个值放入其字段中。如果每个字段的代码都相同,您可能只需将字段的 CSS 选择器放在数据表中并去掉开关即可。
关于java - 如何创建步骤定义来处理动态数据输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39148948/