Java Cucumber:结合大纲场景和数据表

Posted

技术标签:

【中文标题】Java Cucumber:结合大纲场景和数据表【英文标题】:Java Cucumber : Combine outline scenario and data tables 【发布时间】:2021-03-11 04:01:24 【问题描述】:

我问是否有任何方法可以将大纲场景和数据表结合起来,如下例所示:

Feature: User Sign UP

  Scenario Outline: User <User> tries to signup with improper combination of password
    Given the user <User> has browsed to the signup page
    When the user <User> tries to signup entering the following details
      | email           | <Email>            |
      | password        | <Password>         |
      | confirmPassword | <ConfirmPassword>  |
    Then an error message <validation> should be shown above the password field
    Examples:
      | User    | Email                   |Password          | ConfirmPassword  | validation                         |
      | user1   | email1@gmail.com        | 234567569        | 234567569        | This password is entirely numeric. |
      | user2   | email2@gmail.com        | 123456789        | 123456789        | This password is too common.       |

Java 步骤类:

   @When("the user (.+) tries to signup entering the following details")
    public void testAdd2(String user,DataTable dataTable) throws Throwable 
    //Asserts
    

感谢您的宝贵时间。

【问题讨论】:

应该可以 - 检查***.com/a/44198697/2235381 【参考方案1】:

有可能,但不使用数据表可能更容易:

Scenario Outline: User <User> tries to signup with improper combination of password
    Given the user <User> has browsed to the signup page
    When the user <User> tries to signup entering <Email> <Password> and <Confirm Password>
    Then an error message <validation> should be shown above the password field
    Examples:
      | User    | Email                   |Password          | ConfirmPassword  | validation                         |
      | user1   | email1@gmail.com        | 234567569        | 234567569        | This password is entirely numeric. |
      | user2   | email2@gmail.com        | 123456789        | 123456789        | This password is too common.       |

还有java步骤

@When("the user (.+) tries to signup entering the following (.+) (.+) and (.+)")
    public void testAdd2(String user,String email,String password,String confirmPassword,) throws Throwable 
    //Asserts
    

但如果你想使用数据表,你需要将其转换为列表,如下面的示例代码:

public void readDataTableElements(DataTable dt) 
    List<String> list = dt.asList(String.class);
    System.out.println("First element - " + list.get(0));
    System.out.println("Next element - " + list.get(1));

【讨论】:

如果我的回答有帮助请给我反馈。 我的代码工作正常,我只是在我的项目中配置了一些错误,你的代码工作也很好,但在这种情况下使用数据表更灵活。

以上是关于Java Cucumber:结合大纲场景和数据表的主要内容,如果未能解决你的问题,请参考以下文章

定义多个场景大纲的示例

行为驱动:Cucumber + Selenium + Java - Cucumber简单操作实例

Cucumber-Java:避免多个登录并在多个独立场景中使用相同的会话

如何将场景绑定到cucumber java中的特定步骤类

行为驱动:Cucumber + Selenium + Java - 实现测试用例的参数化

行为驱动:Cucumber + Selenium + Java - 实现测试用例的参数化