Table of Contents

Integrating External Data Sources via API

Ranorex tests can be made more powerful and flexible by integrating with external data sources. This guide covers how to connect to databases, read from file-based data sources, and integrate with web services to drive your tests with dynamic data.

Database Connections

Connecting to a database allows you to fetch test data or validate application data directly.

Connecting to a SQL Server Database

Using standard .NET libraries, you can connect to a SQL Server and query for data.

Example:

using System.Data.SqlClient;

public class DatabaseConnector
{
    private SqlConnection connection;

    public void Connect(string connectionString)
    {
        connection = new SqlConnection(connectionString);
        connection.Open();
    }

    public SqlDataReader ExecuteQuery(string query)
    {
        var command = new SqlCommand(query, connection);
        return command.ExecuteReader();
    }

    public void Disconnect()
    {
        connection.Close();
    }
}

// Usage in a test module
var db = new DatabaseConnector();
db.Connect("Server=myServer;Database=myDataBase;User Id=myUser;Password=myPassword;");
var reader = db.ExecuteQuery("SELECT * FROM Users WHERE UserId=1");
if (reader.Read())
{
    string userName = reader["UserName"].ToString();
    // Use the data in your test
}
db.Disconnect();

File-Based Data Sources

Reading data from files like CSV or Excel is a common way to data-drive tests.

Reading from a CSV File

You can use a simple parser or a library like CsvHelper to read data from CSV files.

Example (using a simple manual parser):

public IEnumerable<string[]> ReadCsvFile(string filePath)
{
    using (var reader = new StreamReader(filePath))
    {
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            yield return line.Split(',');
        }
    }
}

// Usage in a test module
var data = ReadCsvFile("C:\\data\\testdata.csv");
foreach (var row in data)
{
    string username = row[0];
    string password = row[1];
    // Run a test with this data
}

Reading from an Excel File

Use a library like EPPlus to read data from Excel files.

Example (with EPPlus library):

// Add EPPlus NuGet package to your project first.
using OfficeOpenXml;

public string ReadFromExcel(string filePath, int row, int col)
{
    using (var package = new ExcelPackage(new FileInfo(filePath)))
    {
        var worksheet = package.Workbook.Worksheets[0];
        return worksheet.Cells[row, col].Value.ToString();
    }
}

Web Service Integration

Integrating with web services (APIs) allows you to fetch data, set up test conditions, or validate backend state.

Making a REST API Call

Use HttpClient to make REST API calls.

Example:

using System.Net.Http;
using System.Threading.Tasks;

public async Task<string> GetUserData(int userId)
{
    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri("https://api.example.com/");
        HttpResponseMessage response = await client.GetAsync($"users/{userId}");
        if (response.IsSuccessStatusCode)
        {
            return await response.Content.ReadAsStringAsync();
        }
        return null;
    }
}

// Usage in a test module
var userDataJson = await GetUserData(1);
// Parse the JSON and use the data

By integrating with external data sources, you can create data-driven tests that are more comprehensive and easier to maintain.