Ranorex Blog - Software Automation & Automated Testing Blog

Posted by ahoisl on Wednesday, May 26th, 2010 at 2:33 pm to Test Automation

Customizing Ranorex Reports

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:


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='http://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:
http://www.ranorex.com/support/user-guide-20/reporting-logging.html

For an instruction on how to add additional data to the default XML report see the following forum post:
http://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("from@domain.com", "to@domain.com",
    "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: 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!

Share/Save/Bookmark

Tags: ,

8 comments

  1. Tom Amiro

    This is great stuff. I tried it out. Being able to send result emails is something I’d been thinking about. It would even be better if the email sent was the html report rather than plain text. Any idea how to do that?

  2. clayton

    Hiya, Is LogText the only log method defined in the interface? I mean custom logging of screen shots isnt implemented?

  3. ahoisl

    If you want to have HTML content, you can set the “mail.IsBodyHtml” property to “true” and change the LogText and LogData methods so they add HTML code to the mail’s body.

    To add screen shots to the email, you have to extend the LogData method to handle data of type Bitmap - just check out the MailLogger class source, there is a comment for that in the LogData method.

    I will try to add an example on how to create HTML emails with bitmaps in there, so stay tuned!

  4. Tom Amiro

    Maybe I shouldn’t have said HTML. What I really wanted was the Ranorex XML report format in the body of the email.

    I tried to attach the .rxlog and .xsl files to the text email, so I could save them to my system and then view the nicely formatted report in a browser. However, attaching the .rxlog from the bin\debug directory, prevented the .tmp report from being renamed, so the automatic viewing of the report in Ranorex Studio was broken.

  5. ahoisl

    Tom,
    I guess the problem is that your custom logger is shutting down before the XmlLogger is finished. Try attaching your custom logger (AttachLogger method) right after calling the “Report.Setup” method. That way your logger will be the last one to get the “End” method call and the XmlLogger should already have completed writing the report file.

    By the way, I updated the MailLogger class in the project to send HTML instead of plain text emails. This is also an example on how to handle Bitmap data (e.g. for screen shots) in an IReportLogger implementation.

  6. Tom Amiro

    Thank you twice. Attaching the custom logger after Report.Setup solved the problem with attaching the files. I’ll look at your updates for html and Bitmap data. These kind of examples are invaluable.

  7. Cheong

    Can i store certain report log instead of the whole report to others file(for example xml,txt or csv file)?

  8. ahoisl

    Cheong,
    You need to create your own custom logger to do this by implementing the IReportLogger interface like explained in this blog. In your custom logger you can then save messages to whatever file or storage type you want.

Leave a Reply