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 下创建


转载于:https://my.oschina.net/dlpinghailinfeng/blog/291512

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值