Home > Back-end >  Restassured how to write common query param instead of repeating the same
Restassured how to write common query param instead of repeating the same

Time:09-14

In the below reassured i am reusing the same query param and Baseuri multiple time is there any way to write it globally so that can be called

String BASEURI=EnvironmentSpecificConfiguration.from(environmentVariables).getProperty("base.url");

    response = SerenityRest.given().contentType("application/json")
            .header("Content-Type", "application/json")
            .when().queryParam("ap_type", apptype)
            .queryParam("date_req", today.toString())
            .queryParam("days", days)
            .get(BASEURI   basePath);
            
            
            String BASEURI = EnvironmentSpecificConfiguration.from(environmentVariables).getProperty("base.url");

    response = SerenityRest.given().contentType("application/json")
            .header("Content-Type", "application/json")
            .when().queryParam("ap_type", apptype)
            .queryParam("date_req", Tomorrow.toString())
            .queryParam("days", days)
            .get(BASEURI   basePath);
            
            
            String BASEURI = EnvironmentSpecificConfiguration.from(environmentVariables).getProperty("base.url");

    response = SerenityRest.given().contentType("application/json")
            .header("Content-Type", "application/json")
            .when().queryParam("ap_type", apptype)
            .queryParam("date_req", Nextday.toString())
            .queryParam("days", days)
            .get(BASEURI   basePath);

CodePudding user response:

You can store your common part of request:

RequestSpecification common = SerenityRest
        .given()
        .baseUri("YOUR_BASE_URI")
        .contentType("YOUR_CONTENT_TYPE")
        .header("YOUR", "HEADER")
        .when()
        .queryParam("YOUR", "PARAM");

And then alter with some specific part:

response = common
        .queryParam("YOUR", "SPECIFIC_PARAM")
        .get();
  • Related