java - 如何创建步骤定义来处理动态数据输入?

标签 java webdriver cucumber cucumber-jvm gherkin

我对此进行了一些搜索,但找不到确切的示例。我有一个表格要填写作为一个步骤。表单字段看起来像这样:

日期:
货币:
总计:
说明:

但并非所有字段都需要用户输入数据。而不是像这样编写多种方法来解释不同的组合:

(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 选择器放在数据表中并去掉开关即可。

更多例子是herehere .

关于java - 如何创建步骤定义来处理动态数据输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39148948/

相关文章:

java - 错误: Could not find or load main class hello in Windows 7

java - Selenium WebDriver session

c# - 'ElementNotVisibleException :element not interactable' error locating Google Search button even though element was waited for on Google Home Page

ruby - Watir-webdriver:为所有失败的场景获取相同的屏幕截图图像

java - 从包含大量文件的 zip 文件中提取 1 个文件的最快方法是什么?

java - 有什么方法可以通过 common-vfs2 为 ftp 或 cifs 支持 IPV6

gradle - 在运行测试之前如何运行应用程序(gradle/kotlin/spring引导)

Java 页面对象模型最佳实践 - 新对象

java - Java 中是否有类似于 C++ 的删除函数,以便将项目设置为 null 提示 GC?

selenium - 如何使用 selenium 从表内可用的复选框列表中单击复选框