When you run your daily user interface tests, the key point is to review the results of the individual tests. You want to know which tests succeeded and which failed, and maybe want to be notified of the results. Additionally, you might want the style of the reports to match the corporate identity design. Ranorex provides you with a flexible and customizable reporting engine that you can adapt to fit your needs.

This blog shows you how to:

Please notice that this blog post targets Ranorex 2.X reports only!


Customizing the default Ranorex XML report

You can easily customize the default Ranorex XML report by providing a new style sheet that is used to format the plain XML logging data to an HTML page. For example, by adding following XSL code to the default “RanorexReport.xsl” style sheet you get a summary heading at the top of the report page:

<h2>Summary:
  <xsl:choose>
    <xsl:when test="//message[@level='FAILED' or @level='ERROR']">
      <span class="failure">FAILED</span>
    </xsl:when>
    <xsl:otherwise>
      <span class="success">PASSED</span>
    </xsl:otherwise>
  </xsl:choose>
</h2>

In order to use the custom style sheet you have to add following line of code in your Ranorex code before the call to Report.Setup:

// set a custom stylesheet for the default XML report
XmlLogger.SetReportStylesheetFile("CustomStylesheet.xsl");

// append to an existing file with the same name
Report.Setup(ReportLevel.Info, logFileName, true);

Show report summary by customizing XSL style sheet

In the same way you can edit the style sheet to match your corporate identity design. The above report also shows how to add plain HTML to the report, e.g. adding a link to www.ranorex.com:

// add some custom HTML message to the XML report
Report.LogHtml(ReportLevel.Info, "Link", "<a href='https://www.ranorex.com/'>Visit www.ranorex.com!</a>");

For a detailed description on how to use the Ranorex reporting see the corresponding Ranorex User Guide section:
https://www.ranorex.com/help/latest/lesson-8-reporting

For an instruction on how to add additional data to the default XML report see the following forum post:
https://www.ranorex.com/forum/report-appending-in-2-2-t1101.html#p5043

Create a custom reporting mechanism

In order to store Ranorex reports to a database or send them by email, you have to create a custom logger class and attach it to the Ranorex report engine. This logger class has to implement the Ranorex.Core.IReportLogger interface that defines methods called by the report engine. Basically, there are four methods that you have to implement:

  • Start/Stop: These two methods are called when a new report should be created and when a report should be closed, respectively. In the start method you would, for instance, create a new report file, email, or database connection to store the actual messages. In the Stop method you would close the file and database connections, or send the email.
  • LogText/LogData: These two methods perform the actual storing of text and data messages. Usually, you take the method arguments passed by the report engine, format them, and store them in a particular way; e.g. write them to a file, insert a new row into a database, or add a new line to an email.

For demonstration purposes I will show you how send reports via email. We start by adding a new class MailLogger that implements the IReportLogger interface:

public class MailLogger : IReportLogger
{
}

If you use Ranorex Studio, you can right click on IReportLogger and select “MailLogger -> Implement Interface (implicit) -> IReportLogger” and Ranorex Studio will create stubs for the interface methods for you. In the constructor of our new class we create a new MailMessage that is sent when the report is finished, i.e. when the End method is called:

public MailLogger(string from, string to, string subject)
{
    mail = new MailMessage(from, to);
    mail.Subject = subject;
}
public void End()
{
    try
    {
        SmtpClient smtpClient = new SmtpClient(Host, Port);
        smtpClient.Send(mail);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex);
    }
}

The actual storing of report messages to the email is done in the LogText method, where we format the arguments by the report engine and store the formatted string to the email body:

public void LogText(ReportLevel level, string category, string message, bool escape)
{
    mail.Body += string.Format("[{0}][{1, -7}][{2}]: {3}n",
        GetTimeStamp(), level, category, message);
}

The last thing we have to do is create a new instance of our MailLogger class and attach it to the Ranorex report engine. This code needs to be placed before or right after the call to Report.Setup, typically in the Main method (in the Program.cs file) of your application:

MailLogger mailLogger = new MailLogger("[email protected]", "[email protected]",
    "Ranorex Report for CustomLogging");
Report.AttachLogger(mailLogger);

Don’t forget to set the MailLogger.Host property to the host name of your SMTP server:

MailLogger.Host = "yourSmtpServer";

When you run the test, additionally to the standard XML and console reports you will get a report sent via email:

Report sent via email
Report sent via email

Summary

The Ranorex report engine allows you to customize the existing XML/HTML based reports as well as create your own reporting mechanism by implementing the IReportLogger interface. In this blog I demonstrated how to use a custom XSL style sheet to add a summary to the default XML report and how to send reports via email. Download the complete project containing the MailLogger class and the customized XSL style sheet using the following link:

Ranorex Studio project illustrating report customizing (including HTML MailLogger and custom XSL style sheet)
UPDATE: Updated the project above to comply with Ranorex 3.X. The project is now compatible with both Ranorex 2.X and 3.x.
UPDATE: The MailLogger class in the project above now creates HTML emails, too!

If you have any questions or suggestions, please don’t hesitate to post your comments to this blog!

You might also like these articles