Model-based testing (MBT) has emerged as a powerful strategy for maintaining high standards of quality in an efficient and systematic manner. MBT transforms the development process by allowing teams to derive scenarios from models of the system under test. These...
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.
Related Posts:
Model-Based Testing with Ranorex DesignWise
Model-based testing (MBT) has emerged as a powerful strategy for maintaining high standards of quality in an efficient and systematic manner. MBT transforms the development process by allowing teams to derive scenarios from models of the system under test. These...
What Is OCR (Optical Character Recognition)?
Optical character recognition technology (OCR), or text recognition, converts text images into a machine-readable format. In an age of growing need for efficient data extraction and analysis processes, OCR has helped organizations revolutionize how they process and...
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...