Automation 如何使用Cucumber和Restarsed将数据从一个场景转换到另一个场景

Automation 如何使用Cucumber和Restarsed将数据从一个场景转换到另一个场景,automation,cucumber,rest-assured,Automation,Cucumber,Rest Assured,我正在使用Cucumber和Restassed实现API自动化 我有一个功能文件,有两个场景。 成功执行场景1后,将生成cookie。我想使用这些cookie,因为授权是场景2。有人能给我一个建议吗 这是我的功能文件,看起来像: Scenario1: Verify SignUp with Valid Email Address Given SignUp Payload with Email "New" When User Calls SignUp API with PUT req

我正在使用Cucumber和Restassed实现API自动化

我有一个功能文件,有两个场景。 成功执行场景1后,将生成cookie。我想使用这些cookie,因为授权是场景2。有人能给我一个建议吗

这是我的功能文件,看起来像:

Scenario1: Verify SignUp with Valid Email Address
    Given SignUp Payload with Email "New"
    When User Calls SignUp API with PUT request
    Then API call is success with status code 200
    And "status.status_type" in response body as "true"
    And "status.status_message" in response body as "Request Successful"
    And "item.has_signup" in response body as "true"

Scenario2: Verify SignUp for already signedUp user
    Given SignUp Payload with Email "New1"
    When User Calls SignUp API with PUT request
    Then API call is success with status code 200
    And "status.status_type" in response body as "false"
    And "status.status_message" in response body as "Looks, like you have used different email address before. Please login with original email address you used to create an account."
以下是我的测试文件的外观:

public class signUp {

ResponseSpecification response_spec;
RequestSpecification request;
Response response;
TestData data = new TestData();
String ia_uid_cookie;
String ia_jwt_cookie;   

@Given("SignUp Payload with Email {string}")
public void signup_Payload_with_Email(String userEmail) throws IOException{

    response_spec = new ResponseSpecBuilder().expectStatusCode(200).expectContentType(ContentType.JSON).build();    
    if(userEmail.equals("New")  ){
        request = RestAssured.given().spec(requestSpecs()).body(data.signUpPayload());
        }
    else if (userEmail.equals("New1")) {
        request = RestAssured.given().spec(requestSpecs()).cookies("_ia_jwt", ia_jwt_cookie, "_ia_uid", ia_uid_cookie).body(data.signUpPayload());
    }
    else{
        request = RestAssured.given().spec(requestSpecs()).body(data.emptyPayload());
    }
}

@When("User Calls SignUp API with PUT request")
public void user_Calls_SignUp_API_with_PUT_request() throws IOException {
    response = request.when().put(getGlobalValue("signup_uri")).then()
               .spec(response_spec).extract().response();
        Map<String, String> userCookies = response.getCookies();
        ia_uid_cookie= userCookies.get("_ia_uid");
        ia_jwt_cookie= userCookies.get("_ia_jwt");
        }   

@Then("API call is success with status code {int}")
public void api_call_is_success_with_status_code(Integer int1) {
    assertEquals(response.getStatusCode(),200);
}

@Then("{string} in response body as {string}")
public void in_response_body_as(String actual, String expected) {
    JsonPath resp_string = responseInString(response);
    assertEquals(resp_string.get(actual).toString(), expected);
}

}
公共类注册{
响应规格响应规格;
请求规范请求;
反应;
TestData数据=新的TestData();
字符串ia_uid_cookie;
字符串ia_jwt_cookie;
@给定(“使用电子邮件{string}注册有效负载”)
public void signup_Payload_with_Email(字符串userEmail)引发IOException{
response_spec=new ResponseSpecBuilder().expectStatusCode(200).expectContentType(ContentType.JSON).build();
if(userEmail.equals(“新”)){
request=RestAssured.given().spec(requestSpecs()).body(data.signUpPayload());
}
else if(userEmail.equals(“New1”)){
request=RestAssured.given().spec(requestSpecs()).cookies(“_ia_jwt”,ia_jwt_cookie,“_ia_uid”,ia_uid_cookie”).body(data.signUpPayload());
}
否则{
request=RestAssured.given().spec(requestSpecs()).body(data.emptyPayload());
}
}
@当(“用户使用PUT请求调用注册API”)
public void user_调用_SignUp_API_,请求()抛出IOException{
response=request.when().put(getGlobalValue(“注册uri”)).then()
.spec(response_spec).extract().response();
Map userCookies=response.getCookies();
ia_uid_cookie=userCookies.get(“\u ia_uid”);
ia_jwt_cookie=userCookies.get(“_ia_jwt”);
}   
@然后(“API调用成功,状态代码为{int}”)
public void api_call_为_success_,状态代码为(整数int1){
assertEquals(response.getStatusCode(),200);
}
@然后({string}作为{string}在响应体中)
响应\u正文\u中的公共无效(实际字符串,预期字符串){
JsonPath resp_字符串=响应安装(响应);
assertEquals(resp_string.get(实际).toString(),应为);
}
}

使用任何DI容器使用依赖项注入


请整理您的问题,以便我们可以阅读您的情景这是否回答了您的问题@lauda thnx…数据提供商为我工作