-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathF14N.cs
More file actions
340 lines (282 loc) · 11.3 KB
/
F14N.cs
File metadata and controls
340 lines (282 loc) · 11.3 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
using FluentAutomation;
using FluentAutomation.Exceptions;
using FluentAutomation.Interfaces;
using ScriptCs.Contracts;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace ScriptCs.FluentAutomation
{
public class F14N : IScriptPackContext
{
public F14N()
{
if (!hasShownWarning)
{
hasShownWarning = true;
ConsoleHelper.Clear();
Console.WriteLine();
ConsoleHelper.DrawMessageBox(2, string.Format("F14N - Fluent Automation Console{0}Testing will begin momentarily. You may see output from the browsers in this console, ignore it.", Environment.NewLine), true);
Console.WriteLine();
}
}
internal IEnumerable<MethodInfo> bootstrapMethods = null;
internal static bool hasShownWarning = false;
public static DateTime? StartTime = null;
public static List<TestResult> Results = new List<TestResult>();
public F14N Init<T>()
{
MethodInfo[] methods = typeof(T).GetMethods(BindingFlags.Static | BindingFlags.Public);
this.bootstrapMethods = methods.Where(x => x.Name == "Bootstrap");
return this;
}
public F14N Bootstrap()
{
var method = this.bootstrapMethods.FirstOrDefault(m => m.GetParameters().Length == 0);
if (method == null)
throw new FluentException("No matching Bootstrap method located for the selected provider.");
method.Invoke(null, new object[] { });
return this;
}
public F14N Bootstrap(string browser)
{
ParameterInfo[] parameters = null;
var method = this.bootstrapMethods.FirstOrDefault(m =>
{
parameters = m.GetParameters();
if (parameters.Length == 1 && parameters.First().ParameterType.BaseType == typeof(Enum))
return true;
return false;
});
if (method == null)
throw new FluentException("No matching Bootstrap method located for the selected provider.");
var browserEnumValue = Enum.Parse(parameters[0].ParameterType, browser);
method.Invoke(null, new object[] { browserEnumValue });
return this;
}
public F14N Bootstrap(Browser browser)
{
return this.Bootstrap(browser.ToString());
}
public F14N Bootstrap(Uri remoteDriverUri, string browser)
{
ParameterInfo[] parameters = null;
var method = this.bootstrapMethods.FirstOrDefault(m =>
{
parameters = m.GetParameters();
if (parameters.Length == 2 && parameters[1].ParameterType.BaseType == typeof(Enum))
return true;
return false;
});
if (method == null)
throw new FluentException("No matching Bootstrap method located for the selected provider.");
var browserEnumValue = Enum.Parse(parameters[1].ParameterType, browser);
method.Invoke(null, new object[] { remoteDriverUri, browserEnumValue });
return this;
}
public F14N Boostrap(Uri remoteDriverUri, Browser browser)
{
return this.Bootstrap(remoteDriverUri, browser.ToString());
}
public F14N Bootstrap(Uri remoteDriverUri, Dictionary<string, object> capabilities)
{
ParameterInfo[] parameters = null;
var method = this.bootstrapMethods.FirstOrDefault(m =>
{
parameters = m.GetParameters();
if (parameters.Length == 2 && parameters[1].ParameterType == typeof(Dictionary<string, object>))
return true;
return false;
});
if (method == null)
throw new FluentException("No matching Bootstrap method located for the selected provider.");
method.Invoke(null, new object[] { remoteDriverUri, capabilities });
return this;
}
public F14N Bootstrap(Uri remoteDriverUri, Browser browser, Dictionary<string, object> additionalCapabilities)
{
ParameterInfo[] parameters = null;
var method = this.bootstrapMethods.FirstOrDefault(m =>
{
parameters = m.GetParameters();
if (parameters.Length == 3 && parameters[2].ParameterType == typeof(Dictionary<string, object>))
return true;
return false;
});
if (method == null)
throw new FluentException("No matching Bootstrap method located for the selected provider.");
method.Invoke(null, new object[] { remoteDriverUri, browser, additionalCapabilities });
return this;
}
public F14N Config(Action<FluentAutomationSettingsProxy> action)
{
try
{
action(new FluentAutomationSettingsProxy());
}
catch (Exception) { };
return this;
}
public F14N Run(Action action)
{
return Run(string.Format("Test {0}", Results.Count + 1), action);
}
public F14N Run(string name, Action action)
{
if (!StartTime.HasValue) StartTime = DateTime.Now;
if (Results.Count != 0) Console.WriteLine();
ConsoleHelper.DrawMessageBox(2, string.Format("Running test: {0}", name));
Console.WriteLine();
try
{
action();
Results.Add(new TestResult()
{
Name = name,
Passed = true
});
}
catch (Exception ex)
{
Results.Add(new TestResult()
{
Name = name,
Passed = false,
Exception = ex,
Message = GetExceptionMessageString(ex)
});
}
return this;
}
public F14N Run(Action<IActionSyntaxProvider> action)
{
return Run(string.Format("Test {0}", Results.Count + 1), action);
}
public F14N Run(string name, Action<IActionSyntaxProvider> action)
{
if (!StartTime.HasValue) StartTime = DateTime.Now;
if (Results.Count != 0) Console.WriteLine();
ConsoleHelper.DrawMessageBox(2, string.Format("Running test: {0}", name));
Console.WriteLine();
var test = new FluentTest();
try
{
var provider = test.I; // Must be accessed to initialize values
action(provider);
Results.Add(new TestResult()
{
Name = name,
Passed = true
});
}
catch (Exception ex)
{
Results.Add(new TestResult()
{
Name = name,
Passed = false,
Exception = ex,
Message = GetExceptionMessageString(ex)
});
}
finally
{
test.Dispose();
}
return this;
}
public F14NProxy Proxy()
{
return new F14NProxy(this);
}
internal void Terminate()
{
if (Results.Count == 0) return;
if (ScriptCsSettings.PrettyPrintResults)
{
ConsoleHelper.Clear();
Console.WriteLine();
ConsoleHelper.DrawMessageBox(2, "F14N - Fluent Automation Console", true);
Console.WriteLine();
var index = 1;
foreach (var result in Results)
{
if (!result.Passed)
{
Console.ForegroundColor = result.Passed ? ConsoleColor.DarkGreen : ConsoleColor.Red;
Console.WriteLine(" {0}) {1}", index, result.Name);
Console.ResetColor();
if (result.Message != null)
{
Console.WriteLine();
ConsoleHelper.DrawMessageBox(6, result.Message + Environment.NewLine + result.Exception.StackTrace);
Console.WriteLine();
}
}
index++;
}
var timeDiff = DateTime.Now - StartTime.Value;
string timeString = string.Empty;
if (timeDiff.TotalSeconds <= 59)
timeString = string.Format("{0} seconds", timeDiff.TotalSeconds);
else
if (timeDiff.Seconds == 0)
timeString = string.Format("{0} minutes", timeDiff.TotalMinutes);
else
timeString = string.Format("{0} minutes, {1} seconds", timeDiff.TotalMinutes, timeDiff.Seconds);
var hasErrors = Results.Count(x => x.Passed == false) == 0;
Console.ForegroundColor = hasErrors ? ConsoleColor.DarkGreen : ConsoleColor.Red;
var stream = Console.Out;
if (hasErrors)
stream = Console.Error;
stream.WriteLine(" {0} tests complete ({1}){2}", Results.Count, timeString, Environment.NewLine);
Console.ResetColor();
}
else
{
List<dynamic> errorResults = new List<dynamic>();
foreach (var result in Results)
{
if (!result.Passed)
{
var stackTrace = new StackTrace(result.Exception, true);
var errorFrame = stackTrace.GetFrame(0);
errorResults.Add(new
{
Column = errorFrame.GetFileColumnNumber(),
Row = errorFrame.GetFileLineNumber(),
Message = result.Message,
StackTrace = result.Exception.StackTrace
});
}
}
var resultsContainer = new
{
TestCount = Results.Count,
ElapsedSeconds = (DateTime.Now - StartTime.Value).TotalSeconds,
Errors = errorResults
};
var stream = Console.Out;
if (errorResults.Count > 0)
stream = Console.Error;
stream.WriteLine(SimpleJson.SimpleJson.SerializeObject(resultsContainer));
}
}
private string GetExceptionMessageString(Exception ex)
{
var errorMessage = new StringBuilder();
if (ex.InnerException != null)
{
errorMessage.AppendLine(ex.InnerException.Message);
errorMessage.AppendLine("--------");
}
errorMessage.AppendLine(ex.Message);
return errorMessage.ToString();
}
}
}