mercredi 26 août 2020

How can we do Parallel Execution of API Test Cases(GET/POST) using Rest Assured based Framework

Please find the below base class for API request. Due to static request/response, I am not able to do parallel test execution. My framework is based on Cucumber, Junit, Java, Json based request.

 public class APIreq {
private static Integer statusCode = 0;
private static String statusLine = null;
private static String resource = null;
protected static Response getResponse;
protected static Response postResponse;
public static JSONObject request = null;
public static JSONObject fileRequest = null;

protected static void postRequest(JSONObject requestBody, String endpoint) throws IOException {
    resource1 = endpoint;
    request = requestBody;
    HashMap<String, String> headerMap = new HashMap<>();
    headerMap.put( "Content-Type", "application/json" );
    headerMap.put("Authorization",ABCD);

    try {
        postResponse = RestAssured.given()
                .log()
                .all()
                .headers( headerMap )
                .body( request.toString() )
                .post( Hooks.targetURL + resource1 )
                .then()
                .extract()
                .response();
        postResponse.prettyPrint();
        statusCode = postResponse.getStatusCode();
        statusLine = postResponse.getStatusLine();
    } catch (Exception e) {
        logger.error( "Error: " + e.getMessage() );
    }
}
public static void assertJsonPostReq(String attribute, String expectedValue) {
    io.restassured.path.json.JsonPath jsonPathEvaluator = postResponse.jsonPath();
    String attributeValue = jsonPathEvaluator.get(attribute);
    SYSO( attribute + ": Expected: " + expectedValue + ", Actual: " + attributeValue );
    Assert.assertTrue(attributeValue.contains(expectedValue));
}

}

Aucun commentaire:

Enregistrer un commentaire