Api test automation

Api test primarily contains test of response generated based on input provided as part of a request response event as we already detailed in a previous post.

Here we deep dive into the code snippets needed to take care of test activities for the same.

Bearer token -

Bearer token is a secret code which needs to be generated as part of api calls. it provides authentication authorization for the request response chain of actions.

StringBuilder sb = new StringBuilder();

sb.Append("grant_type=client_credentials&client_id=");

sb.AppendLine(BearertokenClientId);

sb.AppendLine("&client_secret=");

sb.AppendLine(BearertokenClientSecret);

sb.AppendLine("&resource=");

sb.AppendLine(BearertokenResource);

var client = new RestClient(GetBearerRestClientToken);

var request = new RestRequest(Method.POST);

request.AddHeader("Content-Type", "application/x-www-form-urlencoded");

request.AddParameter("grant_type", "client_credentials");

request.AddParameter("client_id", BearertokenClientId);                    request.AddParameter("client_secret",BearertokenClientSecret);

request.AddParameter("resource", BearertokenResource);

IRestResponse response = client.Execute(request);

string target = response.Content.ToString();

BearerToken = "Bearer " + ExtractText(target, "access_token\":\"", "\"");

BearerToken = "Bearer " + ExtractText(target, "access_token\":\"", "\"");

TestLogger("Bearer Token Generated");

Api validation -

Below code snippet generates the api response post token generation, and reponse is being validated  by a re-usable funnction called as part of the testmethod.

 [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\TestData\\sampledata.csv", "sampledata#csv", DataAccessMethod.Sequential), DeploymentItem("TestData\\sampledata.csv"), TestMethod]

        [TestCategory("Api")]

        public void Apitestvalidation()

        {

            Library.Generatetoken();

            string data_value = TestContext.DataRow[0].ToString();

            Boolean respncode = Library.apivalidation(param1, param2, param3...);

            Assert.IsTrue(responsecode);

        }

-----------

  TestLogger("Fetching data at ='" + level1+ "',level2='" + level2 + "',level 3='" + level 3");

            string url = apiendpoint + "urlsuffix";

            var client = new RestClient(url);

            var request = new RestRequest(Method.POST);

            request.AddHeader("cache-control", "no-cache");

            //request.AddHeader("Authorization", bearerToken);

            request.AddHeader("Authorization", BearerToken_Tkn);

            request.AddHeader("Content-Type", "application/json");

            request.AddHeader("Ocp-Apim-Subscription-Key",Apim_Subscription_Key);

             request.AddParameter("undefined", "{ \"level1\": '" + level1 + "' ,\"level2\":'" + level2 + "',\"date1\":'" + date1 + "' ,\"date2\":'" + date2 + ",\"level3\":'" + level3 + "'}", RestSharp.ParameterType.RequestBody);

            IRestResponse response = client.Execute(request);

            TestLogger("Status Code: " + response.StatusCode);

            string target = response.Content.ToString();

            Boolean json_flag = false;

            TestLogger("JsonValidation_api : " + json_flag);

            }

            Reports(level1 + " api " + " " + level2 + " " + level2 + " " + date1 + " " + date2 + " " ", response.StatusCode.ToString());

            Boolean statuscode_flag = false;

            if (Convert.ToInt32(response.StatusCode) == 200)

                statuscode_flag = true;

            else

                statuscode_flag = false;

            return statuscode_flag && json_flag;

Report -

Reports logging is of paramount importance in the api test as tracking the point of failure is of utmost importance.  I feel the extent report is a nice way to track things so as to identify with ease where the failure happened. The logging is really strong in the testlogger and the way it gets rendered in the html reports generated as part of extent reports. 



Post a Comment

Previous Post Next Post