我想从一个 API 响应 body 中获取价值并使用 Cucumber gherkin 用于另一个 api 请求
I want to take value from one API response body and use into another api request using Cucumber gherkin
场景:验证已发布应用的清单
1. 给定 Base url "baseUrl" 和路径 "basepath"
2. Headers 是
3.和查询参数
4. 和具有以下详细信息的应用程序
5. 当我使用 Base url "baseUrl" 和路径 "basePath" 执行另一个 API
6. 并附加属性值(完整的 url 将是 baseUrl + basePath + AttributeValue )
7.还有apiheaders
8.和查询参数
9. 然后带有 200 状态码的成功消息
我最近实现了一些非常相似的东西。您可以使用以下代码并根据需要对其进行修改。您可能需要从您的功能中省略一些步骤。这些步骤作为步骤定义实现的一部分包含在下面的代码中
特征
@get
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
StepDefs
import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.And;
import io.restassured.RestAssured;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import io.restassured.response.ValidatableResponse;
import io.restassured.specification.RequestSpecification;
import static org.junit.jupiter.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;
public class secondIT {
public static Response response;
public static ValidatableResponse json;
public static RequestSpecification request;
public static String id;
public static JsonPath jsonPathEvaluator;
@Given("an employee exist in the database with id {string}")
public void an_employee_exists_with_id(String ID){
secondIT.id=ID;
RestAssured.baseURI = "http://dummy.www.com/api/v1";
secondIT.request = RestAssured.given();
}
@When("user retrieves employee info by id")
public void user_retrieves_employee_info_by_id(){
secondIT.response = secondIT.request.pathParam("id", secondIT.id).get("/employee/{id}");
secondIT.jsonPathEvaluator = secondIT.response.jsonPath();
assertNotNull(response);
}
@Then("the status code for get employee is {int}")
public void verify_status(int sc){
System.out.println("status code check.. " );
secondIT.json = secondIT.response.then().statusCode(sc);
System.out.println("status code: " + secondIT.response.getStatusCode());
assertEquals(sc,secondIT.response.getStatusCode());
}
}
场景:验证已发布应用的清单
1. 给定 Base url "baseUrl" 和路径 "basepath"
2. Headers 是
3.和查询参数
4. 和具有以下详细信息的应用程序
5. 当我使用 Base url "baseUrl" 和路径 "basePath" 执行另一个 API
6. 并附加属性值(完整的 url 将是 baseUrl + basePath + AttributeValue )
7.还有apiheaders
8.和查询参数
9. 然后带有 200 状态码的成功消息
我最近实现了一些非常相似的东西。您可以使用以下代码并根据需要对其进行修改。您可能需要从您的功能中省略一些步骤。这些步骤作为步骤定义实现的一部分包含在下面的代码中
特征
@get
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
StepDefs
import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.And;
import io.restassured.RestAssured;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;
import io.restassured.response.ValidatableResponse;
import io.restassured.specification.RequestSpecification;
import static org.junit.jupiter.api.Assertions.*;
import static org.assertj.core.api.Assertions.assertThat;
public class secondIT {
public static Response response;
public static ValidatableResponse json;
public static RequestSpecification request;
public static String id;
public static JsonPath jsonPathEvaluator;
@Given("an employee exist in the database with id {string}")
public void an_employee_exists_with_id(String ID){
secondIT.id=ID;
RestAssured.baseURI = "http://dummy.www.com/api/v1";
secondIT.request = RestAssured.given();
}
@When("user retrieves employee info by id")
public void user_retrieves_employee_info_by_id(){
secondIT.response = secondIT.request.pathParam("id", secondIT.id).get("/employee/{id}");
secondIT.jsonPathEvaluator = secondIT.response.jsonPath();
assertNotNull(response);
}
@Then("the status code for get employee is {int}")
public void verify_status(int sc){
System.out.println("status code check.. " );
secondIT.json = secondIT.response.then().statusCode(sc);
System.out.println("status code: " + secondIT.response.getStatusCode());
assertEquals(sc,secondIT.response.getStatusCode());
}
}