Support Corner: API Testing and Simple POST Requests

Sep 19, 2024 | Product Insights, Test Automation Insights

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, enhancing the versatility and depth of your test suites.

In part two of this Support Corner series on API testing, we explored how to create a function to deserialize the response for more robust testing. In part 3, we explore testing a POST request.

How It’s Done

To start, we already have the necessary packages from the previous blog posts, so we can go straight to writing the method for the POST request into the same user code collection from the previous API blog posts.

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 URL of the POST request we want to perform. Since this is a relatively simple example, the URL will be the only parameter. The data we send in the POST request can be included in the parameters, but for this example, the data will be hard coded using the method. It also returns the response as a string. If the POST request returns an exception, we handle it and log it in the report. If we wanted, we could use the Report.Failure method to have it show up as failure.

     public static string PostRequest(string url)

        {

            var stringResponse = "";

              using (var client = new WebClient())

              {

                 byte[] response = Array.Empty<byte>();    

                 var data = new NameValueCollection();

                 data["thing1"] = "hello";

                 data["thing2"] = "world";

                try {

                      response = client.UploadValues(url, data);

                  } 

                  catch (WebException ex)

                    {

                        if (ex.Response != null)

                        {

                            stringResponse = "The POST Request has returned an exception with the status: " +ex.Status + " - " + ex.Message;

                            Report.Info(stringResponse);

                            //Report.Failure(stringResponse

                    }

                }

                       stringResponse = Encoding.ASCII.GetString(response);

              }

              return stringResponse;

        }

      

To use the method we created in the user code collection, we can go to a recording module and Add New Action -> User Code -> Select from library. You should see the PostRequest method we just added.

You can use that method to send a simple POST request and save the request’s response. In this case, we save the response to a variable and then log the variable to the Ranorex report. Another quick test is to validate that the response is what we expect using the Ranorex validation action. In this case, we are using a sample URL from https://httpbin.org/. It will echo back the request to ensure it does what we expect.

Using RestSharp

This blog covered how to create a simple POST request in C# with Ranorex. As an alternative to system.Net, we can use the RestSharp library package to create the GET and POST requests. 

As a reminder to add a package to your Ranorex solution, the first step e is Right-Clicking the solution in the project panel and selecting “Manage Packages”:

In the package manager window, search for RestSharp and select the Add button. After adding the package, you can verify that it was added properly by checking if it shows under references in the project panel. Then, to use the library, you can import the following into the user code:

using RestSharp

Next Steps

The Ranorex API allows you to create powerful scripts to complement the no-code recording tools of Ranorex Studio. Using C#, we can utilize system libraries or third-party libraries to create simple methods for API testing. 

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.

Get a free trial of Ranorex Studio and streamline your automated testing tools experience.

Start your intelligent testing journey with a free DesignWise trial today.

Related Posts:

Ranorex Introduces Subscription Licensing

Ranorex Introduces Subscription Licensing

Software testing needs are evolving, and so are we. After listening to customer feedback, we’re excited to introduce subscription licensing for Ranorex Studio and DesignWise. This new option complements our perpetual licenses, offering teams a flexible, scalable, and...

Seamlessly Test SwiftUI Apps with Ranorex Studio

Seamlessly Test SwiftUI Apps with Ranorex Studio

As mobile development continues to evolve, Apple’s SwiftUI framework has become a favorite among developers for building intuitive, cross-platform user interfaces. But as SwiftUI’s adoption grows, ensuring these applications meet the highest quality standards requires...