Support Corner: API Testing and Deserializing the Response

Aug 9, 2024 | Product Insights

Ranorex is a powerful no-code tool that automates web, mobile, and desktop application testing. In addition to the powerful no-code recording, you can utilize the Ranorex API to create code modules in C# or VB.NET. That’s beneficial because it can increase the effectiveness of your testing suite.

 

In part one of this Support Corner series on API testing, we explored how to test a simple GET request from an API. Now, in part two, we will create a function to deserialize the response for more robust testing. We will base our scripts on the free public Cat Facts API (available at https://catfact.ninja/fact).

How It’s Done

To start, we must add the required Nuget Package to parse the HTTP JSON responses. The first step in adding the JSON.net package is Right-Clicking the solution in the project panel and selecting “Manage Packages”:

 

In the package manager window, select “Add” on the JSON.Net package:

 

After adding the package, you can verify that it was added properly by checking if it shows under references in the project panel.

 

We will import the following into the user code collection we created in part one:

using Newtonsoft.Json;

using Newtonsoft.Json.Linq;

 

To deserialize JSON, create a .NET class with properties and fields representing one or more of the JSON properties. Then, to deserialize the response string we got from the method in part one, we will call the JsonConvert.DeserializeObject method. 

 

Here is the class we will create, which matches the properties needed to parse the response from the Cat Facts API:

     public class CatFact

    {

        public string fact { get; set; }

        public int length { get; set; }

    }

 

The following is an example of the response structure we receive using the Cat Facts API:

{“fact”:”A cat\u2019s back is extremely flexible because it has up to 53 loosely fitting vertebrae. Humans only have 34.”,”length”:106} 

 

As a reminder, you can easily add methods to a user code collection by right-clicking inside the main class and selecting “Insert new user code method…”:

The following method takes in one parameter — the response of the GET request that we made in part one of the API testing blogs. We also log the cat fact to the report but could return that fact as a value to use in sequential steps in the recording modules if desired.

 

         [UserCodeMethod]

        public static void DeserializeResponse(string response)

        {

            CatFact catFact = JsonConvert.DeserializeObject<CatFact>(response);

            Report.Log(ReportLevel.Info, “Here is the fact: “ + catFact.fact);

        }

         

Below is how it would look inside a recording module that uses the function from the first article and the function we created in this one. In this case, we save the response to a variable and then use that variable as the parameter to send into the function we created in this blog.

Another method we could create to parse the response could look like this:

         public static string ParseResponse(string response, string key)

        {                    

            JObject jsonResponse = JObject.Parse(response);

            string responseKeyValue  = jsonResponse.GetValue(key).ToString();

            return responseKeyValue;

        }

 

In this case, we pass two parameters in the method. The second parameter is crucial for parsing a specific value from the first parameter, which is the string response we get from the API Get Request. This method would help in cases where you don’t need to deserialize the whole response but rather just pull out a specific value.

 

Here is a recording module that uses this method to log a specific value from the GET request response. For this, we are passing the key to be “fact.”

This method of parsing the JSON response is more effective when working with simple responses. The best method for working with more complex responses would be to create a class with the JSON properties.

How to Try It Out for Yourself

The Ranorex API allows you to create powerful scripts to complement the no-code recording tools of Ranorex Studio. Using C# to generate random data to test input fields is just one of the many use cases that can be created with it.

If you want to try API testing with Ranorex Studio, request a free trial of our robust QA testing automation tools — including the Ranorex API.

Related Posts:

Support Corner: API Testing and Simple POST Requests

Support Corner: API Testing and Simple POST Requests

Ranorex Studio is renowned for its robust no-code capabilities, which allow tests to be automated seamlessly across web, mobile, and desktop applications. Beyond its intuitive recording features, Ranorex Studio allows custom code module creation using C# or VB.NET,...

The Top 10 Test Automation Challenges

The Top 10 Test Automation Challenges

It’s difficult for any IT organization to execute DevOps effectively without test automation. However, it’s often easier said than done. Overcoming the challenges of automated software testing can end up slowing down product delivery and impacting quality, the exact...

7 Best Android Testing Tools

7 Best Android Testing Tools

There are more and more Android testing tools available for mobile app developers. These are our favorites for performance, accessibility, and security.