Ranorex Studio supports both UI automation and API testing. This allows you to validate backend services and business logic. You can run API tests independently or integrate them with UI automation for full end-to-end coverage.
This guide explains how to send API requests, parse JSON responses, and log results directly in Ranorex Studio. It also contains practical tips for combining API and UI tests for more robust automation workflows.
Create a User Code Collection
A User Code Collection groups related user code methods for reuse across multiple recording modules. While you can add methods directly to a code module, using a dedicated collection makes them easier to maintain and share.
- In Ranorex Studio, right-click your project and select Add > New Item > User Code Collection.
- Name your collection (for example, ApiTesting).
- Import the System.Net namespace at the top of the file:
using System.Net;- Right-click inside the main class and select Insert New User Code Method.
Making a simple GET request
Thefollowing example accepts a URL containing all necessary parameters and performs a GET request.
You can adapt this method to accept a base URL and separate parameters, then combine them programmatically:
public static string GetRequest(string url)
{
using (var wb = new WebClient())
{
var response = wb.DownloadString(url);
return response;
}
}To use this method in a recording module:
- Create a variable for the API URL.
- Add a User Code action and select the method from the library.
- Pass the URL variable as the parameter.
Parsing JSON responses
You use the Newtonsoft.Json library to parse JSON.
Install the library via NuGet:
- In Ranorex Studio, right-click the solution and select Manage packages…
- In the Manage packages window, search for Newtonsoft.Json (JSON.NET) and click Add.
- Confirm the package appears in the References section of your project.
After installation, import the required namespaces:
using Newtonsoft.Json;Deserialization example
First, create a class that matches the JSON structure. For the Cat Facts API, the response has the following format:
{“fact”:”A cat's back is extremely flexible because it has up to 53 loosely fitting vertebrae. Humans only have 34.”,”length”:106}The class you create should look as the example below:
public class CatFact
{
public string fact { get; set; }
public int length { get; set; }
}Then, use a deserialization method to convert the response:
[UserCodeMethod]
public static void DeserializeResponse(string response)
{
CatFact catFact = JsonConvert.DeserializeObject<CatFact>(response);
Report.Log(ReportLevel.Info, “Here is the fact: ” + catFact.fact);
}This method takes the raw JSON response from the GET request, converts it into a CatFact object, and logs the fact to the report. You could also return the value for use in later steps of a test.
Using in a recording module
In your recording:
- Call GetRequest() and store the result in a variable (for example, apiResponse).
- Call DeserializeResponse() and pass apiResponse as the parameter.