json - 如何将响应添加到属于一种场景的 Cucumber 示例获得的 Json 文件中?
问题描述
我有一个场景 A
场景:获取主要记录的数据
当我在标记符号中获取所有主要记录“BookName”的数据
然后我将主要记录添加到标记符号中的测试数据记录“BookName”
示例:
|BookName |
|一个 |
|B |
Then 语句中的 BookName 用作标识符
将结果写入 Json
\\\\\\\\
场景 B
场景:获取主要记录的数据
当我在标签符号中获取所有主要记录的数据“BookName”
然后我将数据与从场景 A 获得的实际数据进行比较
示例:
|书名 |
|一个 |
|B |
如何将数据写入具有不同标识符的 Json 以及如何在不为每个书名使用多个场景的情况下比较数据?
解决方案
您可以为每本书设置一个场景,并在表格中使用 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
}
推荐阅读
- c++ - 被调用的对象类型 bool 不是函数或函数指针
- python-socketio - 是否有 socketio 客户端可以连接到 python-socketio 中支持的消息代理?
- r - Hosmer-Lemeshow 拟合优度误差线性模型 Y 响应检验 NULL
- python - 在某些条件之后和之前的Python提取
- php - 通过jquery将文件输入发送到php
- android - 未启用提示的垂直居中 TextInputLayout 文本
- reactjs - Why is my "sign in with google" (firebase + React) but not working when I deploy to AWS
- reactjs - 使用 Material UI 更改复选框的大小
- python - 烧瓶中指定的变量未显示在 git 页面中
- javascript - onEndReached on flatlist invoked endless [React Native]