How we can build “Params” dynamically in rest api automation

In api automation, let’s consider below code to hit the api and get response.

Response res= given().

              formParam("email", "value").

              formParam("password", "value").

              formParam("action", "login").

              header("token","value").

              when().post("MyResource").then().assertThat().statusCode(200).extract().response();

Actually in the above code we’re just building an api with formParams, header and resource with post request right!

so, there we have created 3 formParm manually and passed the values right? now i want that to added automatically based on no of parameters that we have from an excel sheet.

Thing is these parameters can be deleted or new parameters gets added in future, so that’s why i want to add those dynamically from an excel sheet data.

how i can do that? any suggestions?

Answer

If i’ve understood you right, you want to have the parameters on the spreadsheet to use dynamically and on runtime.

To do that, you’ll need to:

  1. Read the spreadsheet file and get the parameters contained in a data structure such as a Map<String,String>, in which, the key is the field and the value is… the value 😛

  2. Iterate upon the data structure selected and set the formParam generically

    final RequestSpecification given = given();
    for(final Map.Entry<String,String> entry : map.entrySet()) {
        given.formParam(entry.getKey(), entry.getValue());
    }
    final Response res = given.other_things() ...
    

I hope it helps, but keep in mind the use of Map is not necessary. Choose the data structure more appropriate to your situation!

Leave a Reply

Your email address will not be published. Required fields are marked *