-
Notifications
You must be signed in to change notification settings - Fork 476
Expand file tree
/
Copy pathSubFlow.cs
More file actions
121 lines (108 loc) · 4.69 KB
/
SubFlow.cs
File metadata and controls
121 lines (108 loc) · 4.69 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using System.Windows.Forms;
using Castle.Core.Logging;
using TestStack.White.Configuration;
namespace TestStack.White.Reporting.Domain
{
public class SubFlow
{
private readonly string archiveLocation;
private readonly IList<SubFlowStep> flowSteps = new List<SubFlowStep>();
private int currentFlowStepSnapShot;
private readonly string directory;
private DateTime screenCreationTime;
private readonly string name;
private static readonly ILogger Logger = CoreConfigurationLocator.Get().LoggerFactory.Create(typeof(SubFlow));
public SubFlow(string subFlowName, string flowName, string archiveLocation)
{
this.archiveLocation = archiveLocation;
name = subFlowName;
directory = CreateNewDirectory(subFlowName, archiveLocation, flowName);
}
public virtual IList<SubFlowStep> FlowSteps
{
get { return flowSteps; }
}
public virtual void Next(Type type)
{
currentFlowStepSnapShot = 0;
var subFlowStep = new SubFlowStep {Label = type.Name};
subFlowStep.AddScreenShot(TakeScreenShot(type.Name));
flowSteps.Add(subFlowStep);
screenCreationTime = DateTime.Now;
}
public virtual void Act()
{
if (!IsEmpty)
{
SubFlowStep previousSubFlowStep = flowSteps[flowSteps.Count - 1];
previousSubFlowStep.AddScreenShot(TakeScreenShot(previousSubFlowStep.Label));
previousSubFlowStep.TimeSpent = (DateTime.Now - screenCreationTime).Milliseconds;
}
}
public override string ToString()
{
var builder = new StringBuilder();
foreach (SubFlowStep node in flowSteps)
builder.AppendLine(node.ToString());
return builder.ToString();
}
private bool IsEmpty
{
get { return flowSteps.Count == 0; }
}
public virtual string Name
{
get { return name; }
}
private ScreenShot TakeScreenShot(string typeName)
{
string fileName = string.Format(@"{0}\{1}-{2}.png", directory, typeName, ++currentFlowStepSnapShot);
string thumbNailName = string.Format(@"{0}\thumbNails\{1}-{2}-tn.png", directory, typeName, currentFlowStepSnapShot);
CaptureScreenTo(fileName, thumbNailName);
return new ScreenShot(fileName.Replace(archiveLocation + @"\", string.Empty), thumbNailName.Replace(archiveLocation + @"\", string.Empty));
}
private static void CaptureScreenTo(string fileName, string thumbNailName)
{
using (var bmpScreenshot = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height, PixelFormat.Format24bppRgb))
{
using (Graphics gfxScreenshot = Graphics.FromImage(bmpScreenshot))
{
gfxScreenshot.CopyFromScreen(Screen.PrimaryScreen.Bounds.X, Screen.PrimaryScreen.Bounds.Y, 0, 0, Screen.PrimaryScreen.Bounds.Size,
CopyPixelOperation.SourceCopy);
}
try
{
using (FileStream stream = File.Create(fileName))
bmpScreenshot.Save(stream, ImageFormat.Png);
using (FileStream stream = File.Create(thumbNailName))
bmpScreenshot.GetThumbnailImage(50, 50, () => true, IntPtr.Zero).Save(stream, ImageFormat.Png);
}
catch (Exception e)
{
Logger.Error(string.Format("Error saving : {0}", fileName), e);
throw new WhiteException(string.Format("Error saving image {0}", fileName), e);
}
}
}
private static string CreateNewDirectory(string subFlowName, string archiveLocation, string flowName)
{
string result = string.Format(@"{0}\{1}\{2}", archiveLocation, flowName, subFlowName);
if (Directory.Exists(result))
result = string.Format(@"{0}\{1}\{2}", archiveLocation, flowName, subFlowName + CurrentTimeStamp());
Directory.CreateDirectory(result);
Directory.CreateDirectory(result + @"\thumbNails");
return result;
}
private static string CurrentTimeStamp()
{
DateTime now = DateTime.Now;
return "" + now.Day + now.Month + now.Year + now.Hour + now.Minute + now.Second;
}
}
}