-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestBase.cs
401 lines (316 loc) · 13.6 KB
/
TestBase.cs
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
using AventStack.ExtentReports;
using AventStack.ExtentReports.Reporter;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Support.Events;
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 AutomationPractice
{
public class TestBase
{
#region Globals
public const int DefaulTimeOut = 30;
private static IWebDriver _webDriver;
public static EventFiringWebDriver Driver;
public static Actions Action;
public string Url { get; set; }
public string User { get; set; }
public string Password { get; set; }
public string Browser { get; set; }
public string ExtentFileName;
public static string TestResultsDirectory;
public static string EvidenceFileName;
public static string TestInfo;
public static string Loger = string.Empty;
public string Description = string.Empty;
public string Title = string.Empty;
public ExtentReports Extent;
public static ExtentTest Test;
public static Screenshot Screenshot;
public ExtentHtmlReporter HtmlReporter;
#endregion
#region Attr
[TestInitialize()]
public void MyTestInitialize()
{
string testCase = TestContext.TestName;
if (testCase.Contains("API"))
{
InitializeAPI();
}
else if (testCase.Contains("Front"))
{
InitializeFront();
}
}
public void InitializeFront()
{
TestResultsDirectory = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "TestResults", DateTime.Now.ToString("dd-MM-yyyy hh_mm_ss"));
ExtentFileName = Path.Combine(TestResultsDirectory, TestContext.TestName + '_' + DateTime.Now.ToString("ddMMyyyy_hhmmss") + ".html");
if (!Directory.Exists(TestResultsDirectory))
{
Directory.CreateDirectory(TestResultsDirectory);
}
if (!File.Exists(ExtentFileName))
{
File.Create(ExtentFileName);
}
HtmlReporter = new ExtentHtmlReporter(ExtentFileName);
Extent = new ExtentReports();
Extent.AttachReporter(HtmlReporter);
Test = Extent.CreateTest(TestContext.TestName + " " + Title, Description);
GetDriverBrowser();
Driver = new EventFiringWebDriver(_webDriver);
Action = new Actions(Driver);
SetEnviornment();
Driver.Manage().Window.Position = new System.Drawing.Point(1000, 0);
Driver.Navigate().GoToUrl(Url);
Driver.Manage().Window.Maximize();
Driver.ElementValueChanged += FiringDriver_ElementValueChanged;
Driver.ElementClicked += Driver_ElementClicked;
}
public void InitializeAPI()
{
var dateTimeNow = $"{DateTime.Now:yyyy-MM-ddThh_mm_ss}";
TestResultsDirectory = $"C:\\TestsAPI{Environment.UserName}_{dateTimeNow}\\In";
if (!Directory.Exists(TestResultsDirectory))
{
Directory.CreateDirectory(TestResultsDirectory);
}
EvidenceFileName = Path.Combine(TestResultsDirectory, $"{dateTimeNow}.txt");
}
[TestCleanup()]
public void MyTestCleanup()
{
string testCase = TestContext.TestName;
if (testCase.Contains("API"))
{
CleanUpAPI();
}
else if (testCase.Contains("Front"))
{
CleanUpFront();
}
}
public void CleanUpAPI()
{
Logger($"Tests: {TestContext.TestName} was {TestContext.CurrentTestOutcome}.");
Logger($"Tests: {TestContext.TestName} was finished.");
TestContext.AddResultFile(EvidenceFileName);
}
public void CleanUpFront()
{
if (TestContext.CurrentTestOutcome != UnitTestOutcome.Passed)
{
Test.Fail(GetErrorMessage());
}
Extent.Flush();
Driver.Quit();
_webDriver.Quit();
HtmlReporter.Stop();
using (var sw = new StreamWriter(ExtentFileName, true))
{
sw.WriteLine("<style>img {border: 1px solid #ddd;border-radius: 4px;padding: 5px;width: 150px;} img:hover {box-shadow: 0 0 2px 1px rgba(0, 140, 186, 0.5);}</style><script>function OpenImage(src){ var newTab = window.open(); newTab.document.body.innerHTML = " + '"' + "<img src=" + '"' + " + src + " + '"' + ">" + '"' + ";}</script>");
sw.Close();
}
Extent = null;
HtmlReporter = null;
Test = null;
Loger = null;
GC.Collect();
GC.WaitForPendingFinalizers();
ExecuteCmd("taskkill /im chromedriver.exe /f /t");
TestContext.AddResultFile(ExtentFileName);
}
#endregion
#region Properties
private TestContext mTestContext;
public TestContext TestContext
{
get
{
return mTestContext;
}
set
{
mTestContext = value;
}
}
#endregion
#region Methods
public void Logger(string info)
{
using (StreamWriter sw = new StreamWriter(EvidenceFileName, true))
{
sw.WriteLine($"{DateTime.Now:yyyy-MM-ddThh_mm_ss} {info}");
}
}
public void SetEnviornment()
{
using (var sr =
new StreamReader(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data", "Ambiente.txt")))
{
while (!sr.EndOfStream)
{
var line = sr.ReadLine();
if (line != null && line.Contains("Url:"))
{
Url = line.Split(new[] { "Url:" }, StringSplitOptions.None).Last().Trim();
}
}
}
}
public void GetDriverBrowser()
{
SetBrowser();
if (Browser.Equals("Chrome"))
{
_webDriver = new ChromeDriver(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Drivers"));
}
else if (Browser.Equals("Firefox"))
{
_webDriver = new FirefoxDriver(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Drivers"));
}
}
private void SetBrowser()
{
using (var sr =
new StreamReader(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Data", "Ambiente.txt")))
{
while (!sr.EndOfStream)
{
var line = sr.ReadLine();
if (line != null && line.Contains("Browser:"))
{
Browser = line.Split(new[] { "Browser:" }, StringSplitOptions.None).Last().Trim();
}
}
}
}
public string GetErrorMessage()
{
const BindingFlags privateGetterFlags = BindingFlags.GetField |
BindingFlags.GetProperty |
BindingFlags.NonPublic |
BindingFlags.Instance |
BindingFlags.FlattenHierarchy;
var mMessage = string.Empty; // Returns empty if TestOutcome is not failed
if (TestContext.CurrentTestOutcome == UnitTestOutcome.Failed)
{
// Get hold of TestContext.m_currentResult.m_errorInfo.m_stackTrace (contains the stack trace details from log)
var field = TestContext.GetType().GetField("m_currentResult", privateGetterFlags);
if (field != null)
{
var mCurrentResult = field.GetValue(TestContext);
field = mCurrentResult.GetType().GetField("m_errorInfo", privateGetterFlags);
if (field != null)
{
var mErrorInfo = field.GetValue(mCurrentResult);
field = mErrorInfo.GetType().GetField("m_stackTrace", privateGetterFlags);
if (field != null) mMessage = field.GetValue(mErrorInfo) as string;
}
}
}
return mMessage;
}
public List<KeyValuePair<string, string>> ExecuteCmd(string command)
{
var process = new Process();
var list = new List<KeyValuePair<string, string>>();
process.StartInfo.FileName = "cmd.exe";
process.StartInfo.Arguments = "/C " + command;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;
process.Start();
process.WaitForExit();
list.Add(new KeyValuePair<string, string>("Output", process.StandardOutput.ReadToEnd()));
list.Add(new KeyValuePair<string, string>("Error", process.StandardError.ReadToEnd()));
System.Threading.Thread.Sleep(3000);
return list;
}
public static void Checkpoint(bool condition, string message)
{
Screenshot = ((ITakesScreenshot)Driver).GetScreenshot();
EvidenceFileName = Path.Combine(TestResultsDirectory, "evidence" + DateTime.Now.ToString("ddMMyyyyThhmmss") + ".png");
Screenshot.SaveAsFile(EvidenceFileName, ScreenshotImageFormat.Png);
TestInfo = "<h5>CheckPoint: " + message + "</h5><br/><a target='_blank' onclick=OpenImage('data:image/png;base64," + ConvertImageToBase64(EvidenceFileName) + "')><img src='data:image/png;base64," + ConvertImageToBase64(EvidenceFileName) + "' alt='Forest'></a>";
if (condition)
{
Test.Pass(TestInfo);
File.Delete(EvidenceFileName);
}
else
{
Test.Fail(TestInfo);
File.Delete(EvidenceFileName);
Assert.Fail(message);
}
}
public static string ConvertImageToBase64(string fileName)
{
var imageArray = File.ReadAllBytes(fileName);
return Convert.ToBase64String(imageArray);
}
public string GetDescription(WebElementEventArgs e)
{
try
{
if (!String.IsNullOrEmpty(e.Element.GetAttribute("id")))
{
return e.Element.TagName + " " + e.Element.GetAttribute("type") + " [Id=" + e.Element.GetAttribute("id") + ']';
}
if (!String.IsNullOrEmpty(e.Element.GetAttribute("class")))
{
return e.Element.TagName + " " + e.Element.GetAttribute("type") + " [Class=" + e.Element.GetAttribute("class") + ']';
}
if (!String.IsNullOrEmpty(e.Element.GetAttribute("name")))
{
return e.Element.TagName + " " + e.Element.GetAttribute("type") + " [Name=" + e.Element.GetAttribute("name") + ']';
}
if (!String.IsNullOrEmpty(e.Element.GetAttribute("innertext")))
{
return e.Element.TagName + " " + e.Element.GetAttribute("type") + " [InnerText=" + e.Element.GetAttribute("innertext") + ']';
}
}
catch (Exception)
{
// ignored
}
return "";
}
#endregion
#region Events
public void FiringDriver_ElementValueChanged(object sender, WebElementEventArgs e)
{
Screenshot = ((ITakesScreenshot)Driver).GetScreenshot();
EvidenceFileName = Path.Combine(TestResultsDirectory, "evidence" + DateTime.Now.ToString("ddMMyyyyThhmmss") + ".png");
Screenshot.SaveAsFile(EvidenceFileName, ScreenshotImageFormat.Png);
TestInfo = "<h5>" + Loger + "</h5><br/>ElementValueChanged: " + GetDescription(e) + "<br/><a target='_blank' onclick=OpenImage('data:image/png;base64," + ConvertImageToBase64(EvidenceFileName) + "')><img src='data:image/png;base64," + ConvertImageToBase64(EvidenceFileName) + "' alt='Forest'></a>";
Test.Info(TestInfo);
File.Delete(EvidenceFileName);
Loger = String.Empty;
}
private void Driver_ElementClicked(object sender, WebElementEventArgs e)
{
Screenshot = ((ITakesScreenshot)Driver).GetScreenshot();
EvidenceFileName = Path.Combine(TestResultsDirectory, "evidence" + DateTime.Now.ToString("ddMMyyyyThhmmss") + ".png");
Screenshot.SaveAsFile(EvidenceFileName, ScreenshotImageFormat.Png);
TestInfo = "<h5>" + Loger + "</h5><br/>ElementClicked: " + GetDescription(e) + "<br/><a target='_blank' onclick=OpenImage('data:image/png;base64," + ConvertImageToBase64(EvidenceFileName) + "')><img src='data:image/png;base64," + ConvertImageToBase64(EvidenceFileName) + "' alt='Forest'></a>";
Test.Info(TestInfo);
File.Delete(EvidenceFileName);
Loger = String.Empty;
}
#endregion
}
}