如何使用 Cucumber 和 RestAssured 将一个场景中的数据用于另一个场景
How to use data from scenario one into another using Cucumber and RestAssured
我正在使用 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);
}
}
我正在使用 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);
}
}