Table of Contents

Custom Reporting via API

Ranorex provides a detailed reporting system out of the box, but you can also create custom reports and integrate with external reporting tools using the Ranorex API. This guide covers programmatic report generation, creating custom report formats, and integrating with other reporting systems.

Programmatic Report Generation

The Report class is the central point for all reporting activities. You can use it to log messages, add screenshots, and control the report structure.

Logging Custom Messages

You can log messages at different levels (Success, Failure, Info, Warn, Debug).

Example:

Report.Log(ReportLevel.Info, "This is an informational message.");
Report.Success("Validation", "The value was correct.");
Report.Failure("Validation", "The value was incorrect.");

Adding Screenshots to the Report

You can capture screenshots of the entire desktop or specific elements and add them to the report.

Example:

// Capture the entire desktop
Imaging.CaptureDesktop(true);

// Capture a specific element
var element = repo.MyApp.SomeElement;
Report.Log(ReportLevel.Info, "Screenshot of the element.", Imaging.CaptureImage(element));

Custom Report Formats

While Ranorex generates a detailed HTML report, you might want to create reports in other formats like XML, JSON, or a custom text format.

Generating a Simple XML Report

You can collect test results and write them to an XML file at the end of the test run.

Example:

using System.Xml.Linq;

public class XmlReporter
{
    private XDocument doc;
    private XElement root;

    public XmlReporter()
    {
        doc = new XDocument();
        root = new XElement("TestResults");
        doc.Add(root);
    }

    public void AddTestCaseResult(string testCaseName, bool passed)
    {
        root.Add(new XElement("TestCase",
            new XAttribute("name", testCaseName),
            new XAttribute("result", passed ? "Passed" : "Failed")));
    }

    public void Save(string filePath)
    {
        doc.Save(filePath);
    }
}

// Usage in a test suite teardown
var xmlReporter = new XmlReporter();
// ... add results ...
xmlReporter.Save("C:\\reports\\my-report.xml");

Report Integration Patterns

You can integrate Ranorex reporting with other systems like TestRail, JIRA, or a custom dashboard.

Disabling Default Reporting

In some integration scenarios, you might want to disable the default Ranorex report generation and rely solely on your custom reporting.

Example:

// Can be set at the beginning of the test run
Report.Setup(ReportLevel.Info, null, false); // Disables file-based logging

Sending Results to a Web Service

You can send test results to a web service to be displayed on a custom dashboard.

Example:

public async Task SendResultToDashboard(string testCaseName, bool passed)
{
    using (var client = new HttpClient())
    {
        var content = new StringContent(
            $"{{\"testCase\": \"{testCaseName}\", \"passed\": {passed.ToString().ToLower()}}}",
            Encoding.UTF8,
            "application/json");

        await client.PostAsync("https://my-dashboard.com/api/results", content);
    }
}

By leveraging the reporting API, you can create highly customized and integrated reporting solutions that fit the specific needs of your project and organization.