alielie
2026-09-10 f3889f2d36c50cf99468594854def9b7ed069fb5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
using AddInPlugin.Setting;
using OpenTap;
using OpenTap.Addin.Util;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
 
namespace AddInPlugin
{
    public class TxtReportListener : ReportListener
    {
 
        public TxtReportListener(string name) : base(name)
        {
 
        }
 
        public override IResultListener CreateChild()
        {
            var res = new TxtReportListener($"{Name}_Child");
            if (this.Root == null)
            {
                res.Root = this;
            }
            else
            {
                res.Root = this.Root;
            }
            return res;
        }
 
        private void WriteReport(string text)
        {
            string file = Path.Combine(StationSettings.Current.ReportPath,
                $"{CurrentPlanRun.TestPlanName}_{CurrentPlanRun.StartTime.ToString("yyyyMMddHHmmssfff")}.txt");
            using (var fs = new FileStream(file, FileMode.Append, FileAccess.Write, FileShare.ReadWrite))
            using (var writer = new StreamWriter(fs, Encoding.UTF8))
            {
                writer.WriteLine(text);
                writer.Flush();
            }
        }
 
        protected override void PlanStarted()
        {
            lock (this)
            {
                WriteReport($"{CurrentPlanRun.TestPlanName} Start {CurrentPlanRun.StartTime:yyyy-MM-dd HH:mm:ss.fff}\r\n");
            }
        }
 
        protected override void StepComplated(TestStepRun stepRun)
        {
            lock (this)
            {
                if (stepRun.Report != null)
                {
                    string text = $"*********** {stepRun.TestStepPath} Start {stepRun.Report.StartTime.ToString("yyyy-MM-dd HH:mm:ss.fff")} ***********\r\n";
                    text += $"Result: {stepRun.Verdict}\r\n";
                    text += $"Parameters: ----------\r\n";
                    var datas = new List<string[]>();
 
                    datas.Add(new string[] { "Name", "Type", "Value" });
                    foreach (var detail in stepRun.Report.Details)
                    {
                        datas.Add(new string[] { detail.Name, detail.Type, detail.Normal ? detail.Value : detail.Exception.Message });
                    }
                    text += ChineseTableExporter.ToString(3, datas);
 
                    text += $"-----------------------\r\n";
                    text += $"{stepRun.TestStepPath} End {stepRun.Report.EndTime.ToString("yyyy-MM-dd HH:mm:ss.fff")} \r\n";
 
                    WriteReport(text);
                }
            }
        }
 
        protected override void PlanComplated()
        {
            lock (this)
            {
                WriteReport($"{CurrentPlanRun.TestPlanName} End {DateTime.Now:yyyy-MM-dd HH:mm:ss.fff}\r\nResult: {CurrentPlanRun.Verdict}\r\n");
            }
        }
    }
}