![](/img/trans.png)
[英]How can i store a value as global variable from an API response and pass it to another API as parameter in Cucumber feature file using REST assured
[英]I want to take value from one API response body and use into another api request using Cucumber gherkin
Scenario: Verify the manifest of published app 1. Given Base url "baseUrl" and path "basepath" 2. And Headers are 3. And Query parameter 4. And App with below details 5. When I execute the another API with Base url " baseUrl" 和路徑 "basePath" 6. 和 Append 和 Attributevalue (完整的 url 將是,baseUrl + basePath + AttributeValue ) 7. 和 Z8A5DA52ED12644721A8A 標題AZ
8.及查詢參數
9. 然后是帶有 200 狀態碼的成功消息
我最近實現了一些非常相似的東西。 您可以使用以下代碼並根據需要對其進行修改。 您可能需要從功能中省略一些步驟。 這些步驟包含在下面的代碼中,作為 step def 實現的一部分
特征
@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
步驟定義
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());
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.