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
.
///
ModuleType.UserCode, 1)]
public class TxtReport : IReportLogger
{
private bool success = true;
private string filename;
private List linkedResources = new List();
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());
}
public void LogText(ReportLevel level, string category, string message, bool escape, IDictionary metaInfos)
{
CheckSuccess(level);
text +=string.Format("[{0}][{1, -7}][{2}]: {3}", GetTimeStamp(), level, category, message);
}
public void LogData(ReportLevel level, string category, object data)
{
LogData(level, category, "Data logged.", data, new Dictionary());
}
public void LogData(ReportLevel level, string category, string message, object data, IDictionary 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(
@"", "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;
}
///
///Gets a formatted time stamp string.
///

///A time stamp string.
private string GetTimeStamp()
{
return System.DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss.fff", System.Globalization.CultureInfo.InvariantCulture);
}
///
///Creates a new for the given bitmap, assigns it
///a new content ID, and adds it to the linkedResources list.
///

///A bitmap.
///The content ID that was assigned to the resource.
private string AddBitmapToLinkedResources(Bitmap bitmap)
{
string cidString = "ID_"+ cid++;
AddBitmapToLinkedResources(bitmap, cidString);
return cidString;
}
///
///Creates a new for the given bitmap, assigns it
///the specified content ID, and adds it to the linkedResources list.
///

///A bitmap.
///The content ID that is assigned to the resource.
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. Program 수정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에서 만들기

좋은 웹페이지 즐겨찾기