【问题标题】:How to use data from scenario one into another using Cucumber and RestAssured如何使用 Cucumber 和 RestAssured 将场景中的数据使用到另一个场景中
【发布时间】:2020-06-16 20:42:23
【问题描述】:

我正在使用 Cucumber 和 RestAssured 进行 API 自动化。

我有一个包含 2 个场景的功能文件。 成功执行场景 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);
}

}

【问题讨论】:

标签: automation cucumber rest-assured


【解决方案1】:
猜你喜欢
  • 1970-01-01
  • 2016-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多