首页 > 解决方案 > 如何将响应添加到属于一种场景的 Cucumber 示例获得的 Json 文件中?

问题描述

我有一个场景 A
场景:获取主要记录的数据
当我在标记符号中获取所有主要记录“BookName”的数据
然后我将主要记录添加到标记符号中的测试数据记录“BookName”
示例:
|BookName |
|一个 |
|B |
Then 语句中的 BookName 用作标识符

将结果写入 Json
\\\\\\\\
场景 B
场景:获取主要记录的数据
当我在标签符号中获取所有主要记录的数据“BookName”
然后我将数据与从场景 A 获得的实际数据进行比较
示例:
|书名 |
|一个 |
|B |

如何将数据写入具有不同标识符的 Json 以及如何在不为每个书名使用多个场景的情况下比较数据?

标签: jsoncucumbercompare

解决方案


您可以为每本书设置一个场景,并在表格中使用 json 进行比较。我正在使用我的员工示例,但您明白了这一点并进行了相应的调整。前3步比较简单,所以不包括

 Scenario: get employee
    Given an employee exist in the database with id "2"
    When  user retrieves employee info by id
    Then the status code for get employee is 200
    And response includes the following employee info
     | status               | success        |
    | data.employee_name    | Garrett Winters|
    | data.id               | 2              |
    | data.employee_age     | 63             |

为了比较我使用

 @And("response includes the following employee info$")
public void employee_response_equals(Map<String, Object> ExpectedFields){
Map<String, Object> actualFields =  ExpectedFields.keySet().stream().collect(Collectors.toMap(expectedKey -> expectedKey, expectedKey -> jsonPathEvaluator.get(expectedKey).toString()));

assertThat(actualFields).containsAllEntriesOf(ExpectedFields); 
}

或者

您可以将 json 放在example表中,但您必须使用@ParameterType和解析 . 我在这里展示我的示例,@ParameterType但您可以对其进行调整以输入您的 json 而不是用户名等。重点是使用@ParameterType

  Scenario Outline: Add a user using pojo class in scenario outline using @ParameterType
   
       Given I open the site "http://www.way2automation.com/angularjs-protractor/webtables/"
       When I add  user by passing <userInfo> to pojo class from scenario outline
       Then I see user <userInfo> added to the table
        Examples:
        |userInfo|
        |TestFirstName4,TestLastName4,testusername4,test98760,testuser4@testcompany4.com,7890-123-2233|
        |TestFirstName5,TestLastName5,testusername5,test67890,testuser5@testcompany5.com,456-789-1122|

步骤定义

@ParameterType(".*?")
  public addUserPojo defineStringToPojo(String adduser) {
    List<String> usr = Arrays.asList(adduser.split(","));
   //use `usr` as needed
  }

推荐阅读