ranorex自动化测试框架之日志篇

1.定义一个继承自IReportLogger接口的自定义日志类

代码清单1:


using System;
using System.Collections.Generic;
using System.Drawing;
using System.Net.Mail;
using System.Net.Mime;
using System.Text;

using Ranorex;
using Ranorex.Core;
using Ranorex.Core.Testing;

namespace TextReport
{
    /// <summary>
    /// Description of
TextReport
. /// </summary> ModuleType.UserCode, 1)] public class TxtReport : IReportLogger { private bool success = true; private string filename; private List<LinkedResource> linkedResources = new List<LinkedResource>(); private int cid = 1; private string text; public TxtReport() { // Do not delete - a parameterless constructor is required! } public TxtReport(string fname) { filename=fname; } public bool PreFilterMessages { get{ return true;} } public void Start() { throw new NotImplementedException(); } public void End() { //改动这里可以转换为重写,否则为追加 //System.IO.File.WriteAllText(filename, text); System.IO.File.AppendAllText(filename,text); } public void LogText(ReportLevel level, string category, string message, bool escape) { LogText(level, category, message, escape, new Dictionary<string, string>()); } public void LogText(ReportLevel level, string category, string message, bool escape, IDictionary<string, string> metaInfos) { CheckSuccess(level); text +=string.Format("[{0}][{1, -7}][{2}]: {3}\n", GetTimeStamp(), level, category, message); } public void LogData(ReportLevel level, string category, object data) { LogData(level, category, "Data logged.", data, new Dictionary<string, string>()); } public void LogData(ReportLevel level, string category, string message, object data, IDictionary<string, string> metaInfos) { string dataMessage; // special handling of Bitmap data if (data is Bitmap) { Bitmap bitmap = (Bitmap)data; // add special code to store bitmaps here -> add image to HTML email string cidString = AddBitmapToLinkedResources(bitmap); dataMessage = String.Format( @"<a href=""{0}""><img width=""300"" src=""{1}"" alt=""{2}""/></a>", "cid:" + cidString, "cid:" + cidString, message); } else { dataMessage = (data != null) ? data.GetType().ToString() : "(null)"; } LogText(level, category, dataMessage, false, metaInfos); } private void CheckSuccess(ReportLevel level) { if (level == ReportLevel.Error || level == ReportLevel.Failure) success = false; } /// <summary> /// Gets a formatted time stamp string. /// </summary> /// <returns>A time stamp string.</returns> private string GetTimeStamp() { return System.DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff", System.Globalization.CultureInfo.InvariantCulture); } /// <summary> /// Creates a new <see cref="LinkedResource"/> for the given bitmap, assigns it /// a new content ID, and adds it to the <c>linkedResources</c> list. /// </summary> /// <param name="bitmap">A bitmap.</param> /// <returns>The content ID that was assigned to the resource.</returns> private string AddBitmapToLinkedResources(Bitmap bitmap) { string cidString = "ID_" + cid++; AddBitmapToLinkedResources(bitmap, cidString); return cidString; } /// <summary> /// Creates a new <see cref="LinkedResource"/> for the given bitmap, assigns it /// the specified content ID, and adds it to the <c>linkedResources</c> list. /// </summary> /// <param name="bitmap">A bitmap.</param> /// <param name="cidString">The content ID that is assigned to the resource.</param> private void AddBitmapToLinkedResources(Bitmap bitmap, string cidString) { System.IO.MemoryStream stream = new System.IO.MemoryStream(); bitmap.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg); stream.Seek(0, System.IO.SeekOrigin.Begin); LinkedResource imageResource = new LinkedResource(stream, MediaTypeNames.Image.Jpeg); imageResource.ContentId = cidString; imageResource.TransferEncoding = TransferEncoding.Base64; linkedResources.Add(imageResource); } } }



2.修改Programe.cs

代码清单2:



namespace TextReport
{
    class Program
    {
        [STAThread]
        public static int Main(string[] args)
        {
            // Uncomment the following 2 lines if you want to automate Windows apps
            // by starting the test executable directly
            //if (Util.IsRestartRequiredForWinAppAccess)
            //    return Util.RestartWithUiAccess();

            Keyboard.AbortKey = System.Windows.Forms.Keys.Pause;
            int error = 0;

            try
            { TxtReport txRep = new TxtReport("Report.txt");
            	Report.AttachLogger(txRep); error = TestSuiteRunner.Run(typeof(Program), Environment.CommandLine);
            }
            catch (Exception e)
            {
                Report.Error("Unexpected exception occurred: " + e.ToString());
                error = -1;
            }
            return error;
        }
    }
}

重点是:

TxtReport txRep = new TxtReport("Report.txt");
            	Report.AttachLogger(txRep);



3.运行脚本

日志文件会在{项目}\bin\debug\Report.txt 下创建


你可能感兴趣的:(日志,自动化测试,ranorex)