带有Map字段的黄瓜传递实体。如何映射从特征文件发送的变量

时间:2018-06-28 06:41:34

标签: java selenium automation cucumber

我正在尝试传递仅具有1个字段的实体,这是一个Map>。我在弄清楚如何使黄瓜通过实体中特征文件中的变量时遇到麻烦。

这是Entity对象:

public class Page {
    // change the List to represent a List of Questions when the class is implemented.
    private Map<Integer, List<String>> questions;

    public Map<Integer, List<String>> getQuestions() {
        return questions;
    }

    public void setQuestions(Map<Integer, List<String>> questions) {
        this.questions = questions;
    }
}

和功能文件:

Scenario: Create new Questionnaire
    Given the user has clicked the "New questionnaire" button
    When the user creates new questionnaire:
      | name |
      | [6A] |
    And the user completes the questionnaire design form:
      | title                 |
      | fitness questionnaire |
    And completes the questionnaire structure:
      | questionsPage | questions                                                                         |
      | 1             | {Q1 - NPS Question, Q2 - Fitness Driver Question, 3 - Feedback Request Question } |
      | 2             | {Q3 - Feedback Request Question}                                                  |
    Then questionnaire is created successfully

步骤定义:

@And("^completes the questionnaire structure:$")
    public void completesTheQuestionnaireStructure(List<Page> pages) throws Throwable {
        questionnaireCreator.createQuestionnaireStructure(pages.get(0));
    }

我想将地图的键设置为我从场景传递的int值,并将键后面的值设置为特定问题列表,当前它抛出一个异常:“ cucumber.runtime.CucumberException:Duplicate field问题”

任何人都遇到过这样的问题,您如何进行修复?

1 个答案:

答案 0 :(得分:0)

不确定将宁静和黄瓜混合在一起。如果可行,您可以尝试以下解决方案。

我还从github和maven假设此版本仍仅支持黄瓜2,因此XStream可以工作。

需要从数据表中删除标题并修改问题列表。 并完成问卷结构:

  | 1 | Q1 - NPS Question,Q2 - Fitness Driver Question,Q3 - Feedback Request Question |
  | 2 | Q3 - Feedback Request Question                                                |

在步骤定义代码中使用它。黄瓜本身无法将其转换为列表作为地图中的键。您可以使用新地图将其设置为Page对象。

@And("^completes the questionnaire structure:$")
    public void completesTheQuestionnaireStructure(Map<Integer, String> pages) throws Throwable {

        Map<Integer, List<String>> pageNew = new HashMap<>();
        pages.forEach((k, v) -> pageNew.put(k, Arrays.asList(v.split(","))));

        System.out.println(pageNew);
    }