private void SendEmailsForEvents()
        {
            try
            {
                if (!_configuration.SendEmails)
                {
                    return;
                }

                var eventSubs = _methodInfo.GetCustomAttributes <EventNotification>();
                foreach (var sub in eventSubs)
                {
                    var currentTestVersions = ReportTestHelper.GetTestsFromFolder(_test.AttachmentsPath);
                    var subscription        = _configuration.EventDurationSubscriptions.FirstOrDefault(x => x.Name.Equals(sub.Name));
                    if (currentTestVersions.Count > 1)
                    {
                        var previousTest = currentTestVersions
                                           .OrderByDescending(x => x.DateTimeFinish)
                                           .Skip(1)
                                           .First(x => x.Events.Any(e => e.Name.Equals(sub.EventName)));
                        var previuosEvent = previousTest.Events.First(x => x.Name.Equals(sub.EventName));
                        var currentEvent  = _test.Events.First(x => x.Name.Equals(sub.EventName));

                        if (currentEvent.Duration - previuosEvent.Duration > sub.MaxDifference)
                        {
                            if (subscription != null)
                            {
                                ReportEmailHelper.Send(_configuration.SendFromList, subscription.TargetEmails,
                                                       _test, _screenshotsPath, _configuration.AddLinksInsideEmail,
                                                       true, sub.EventName, previuosEvent);
                            }
                            else if (sub.FullPath != null)
                            {
                                subscription = ReportXMLHelper.Load <EventDurationSubscription>(sub.FullPath);
                                ReportEmailHelper.Send(_configuration.SendFromList, subscription.TargetEmails,
                                                       _test, _screenshotsPath, _configuration.AddLinksInsideEmail,
                                                       true, sub.EventName, previuosEvent);
                            }
                            else if (sub.Targets.Any())
                            {
                                ReportEmailHelper.Send(_configuration.SendFromList, sub.Targets,
                                                       _test, _screenshotsPath, _configuration.AddLinksInsideEmail,
                                                       true, sub.EventName, previuosEvent);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                InternalLogs.Exception(ex, "Exception in SendEmailsForEvents");
            }
        }
        private void SaveTestFiles()
        {
            try
            {
                Directory.CreateDirectory(_test.AttachmentsPath);
                _test.SaveAsXml(_test.AttachmentsPath + Output.Files.GetTestXmlName(_test.DateTimeFinish));
                var testVersions     = ReportTestHelper.GetTestsFromFolder(_test.AttachmentsPath);
                var testRemarks      = GetTestRemarks();
                var chartId          = Output.GetHistoryChartId(_test.Guid, _test.DateTimeFinish);
                var highstockHistory = new ReportJsHighstock(testVersions, testRemarks, chartId);
                highstockHistory.SaveScript(_test.AttachmentsPath);

                var testPath = _test.AttachmentsPath + Output.Files.GetTestHtmlName(_test.DateTimeFinish);
                _test.GenerateTestPage(testPath, _testOutput, Output.Files.GetTestHistoryScriptName(_test.DateTimeFinish));
            }
            catch (Exception ex)
            {
                InternalLogs.Exception(ex, "Exception in SaveTestFiles");
            }
        }
        public void GenerateReport()
        {
            try
            {
                if (!_configuration.GenerateReport)
                {
                    return;
                }

                const string cssPageName = Output.Files.ReportStyleFile;
                var          cssFullPath = Path.Combine(_outputPath, cssPageName);
                if (!File.Exists(cssFullPath))
                {
                    PageGenerator.GenerateStyleFile(cssFullPath);
                }

                /*
                 * var primerName = Output.Files.PrimerStyleFile;
                 * ExtractResource(primerName, _outputPath);
                 *
                 * var octiconsName = Output.Files.OcticonsStyleFiles;
                 * ExtractResources(octiconsName, _outputPath);
                 *
                 * //jquery - 1.11.0.min.js
                 * var jqueryName = Output.Files.JQueryScriptFile;
                 * ExtractResource(jqueryName, _outputPath);
                 */
                var tests      = ReportTestHelper.GetNewestTests(_attachmentsPath).OrderBy(x => x.DateTimeFinish).ToList();
                var stats      = new MainStatistics(tests, _startSuite);
                var statsChart = new MainInfoChart(stats, Output.GetStatsPieId());
                statsChart.SaveScript(_outputPath);
                tests.GenerateTimelinePage(Path.Combine(_outputPath, Output.Files.TimelineFile));
                stats.GenerateMainStatisticsPage(Path.Combine(_outputPath, Output.Files.TestStatisticsFile));
                tests.GenerateTestListPage(Path.Combine(_outputPath, Output.Files.TestListFile));
                tests.GenerateReportMainPage(_outputPath, stats);
            }
            catch (Exception ex)
            {
                InternalLogs.Exception(ex, "Exception in GenerateReport");
            }
        }
 private void CleanUpTestFiles()
 {
     try
     {
         var maxDate = DateTime.Now.AddDays(-_configuration.TestHistoryDaysLength);
         var folders = Directory.GetDirectories(_attachmentsPath);
         foreach (var folder in folders)
         {
             var dirInfo  = new DirectoryInfo(folder);
             var allFiles = dirInfo.GetFiles("*.xml").OrderByDescending(x => x.CreationTime);
             if (dirInfo.LastWriteTime < maxDate || dirInfo.CreationTime < maxDate || !allFiles.Any())
             {
                 Directory.Delete(dirInfo.FullName, true);
             }
             else
             {
                 var folderTestVersions       = ReportTestHelper.GetTestsFromFolder(folder);
                 var folderTestVersionsNumber = folderTestVersions.Count;
                 if (folderTestVersionsNumber >= _configuration.MaxTestVersionsNumber)
                 {
                     folderTestVersions.OrderByDescending(x => x.DateTimeFinish)
                     .Skip(_configuration.MaxTestVersionsNumber)
                     .ToList()
                     .ForEach(x => x.DeleteTestFiles(_screenshotsPath));
                 }
                 ReportTestHelper
                 .GetTestsFromFolder(folder)
                 .Where(x => x.DateTimeFinish < maxDate)
                 .ToList()
                 .ForEach(x => x.DeleteTestFiles(_screenshotsPath));
             }
         }
     }
     catch (Exception ex)
     {
         InternalLogs.Exception(ex, "Exception in CleanUpTestFiles");
     }
 }
        private void SendEmails(bool isSuccess)
        {
            try
            {
                if (!_configuration.SendEmails)
                {
                    return;
                }

                var subs = _methodInfo.GetCustomAttributes <Notification>();
                foreach (var sub in subs)
                {
                    var sendCondition = (sub.UnsuccessfulOnly && !isSuccess) || (!sub.UnsuccessfulOnly);
                    if (!sendCondition)
                    {
                        continue;
                    }

                    var subscription = _configuration.Subsciptions.FirstOrDefault(x => x.Name.Equals(sub.Name));
                    if (subscription != null)
                    {
                        ReportEmailHelper.Send(_configuration.SendFromList, subscription.TargetEmails,
                                               _test, _screenshotsPath, _configuration.AddLinksInsideEmail);
                    }
                    else if (sub.FullPath != null)
                    {
                        subscription = ReportXMLHelper.Load <Subsciption>(sub.FullPath);
                        ReportEmailHelper.Send(_configuration.SendFromList, subscription.TargetEmails,
                                               _test, _screenshotsPath, _configuration.AddLinksInsideEmail);
                    }
                    else if (sub.Targets.Any())
                    {
                        ReportEmailHelper.Send(_configuration.SendFromList, sub.Targets,
                                               _test, _screenshotsPath, _configuration.AddLinksInsideEmail);
                    }
                }

                /*
                 * var singleSubs = _methodInfo.GetCustomAttributes<SingleTestSubscriptionAttribute>();
                 * foreach (var singleSub in singleSubs)
                 * {
                 *  var sendCondition = (singleSub.UnsuccessfulOnly && !isSuccess) || (!singleSub.UnsuccessfulOnly);
                 *  if (!sendCondition) continue;
                 *
                 *  var singleTestSubscription =
                 *      _configuration.SingleTestSubscriptions.FirstOrDefault(x => x.TestGuid.Equals(_test.Guid));
                 *  if (singleTestSubscription != null)
                 *  {
                 *      ReportEmailHelper.Send(_configuration.SendFromList, singleTestSubscription.TargetEmails,
                 *          _test, _screenshotsPath, _configuration.AddLinksInsideEmail);
                 *  }
                 *  else if (singleSub.FullPath != null)
                 *  {
                 *      var singleSubFromXml = ReportXMLHelper.Load<SingleTestSubscription>(singleSub.FullPath);
                 *      ReportEmailHelper.Send(_configuration.SendFromList, singleSubFromXml.TargetEmails,
                 *          _test, _screenshotsPath, _configuration.AddLinksInsideEmail);
                 *  }
                 *  else if (singleSub.Targets.Any())
                 *  {
                 *      ReportEmailHelper.Send(_configuration.SendFromList, singleSub.Targets,
                 *          _test, _screenshotsPath, _configuration.AddLinksInsideEmail);
                 *  }
                 * }
                 */
                var eventSubs = _methodInfo.GetCustomAttributes <EventNotification>();
                foreach (var sub in eventSubs)
                {
                    var currentTestVersions = ReportTestHelper.GetTestsFromFolder(_test.AttachmentsPath);

                    if (currentTestVersions.Count <= 1)
                    {
                        continue;
                    }

                    var previousTest = currentTestVersions
                                       .OrderByDescending(x => x.DateTimeFinish)
                                       .Skip(1)
                                       .First(x => x.Events.Any(e => e.Name.Equals(sub.EventName)));
                    var previuosEvent = previousTest.Events.First(x => x.Name.Equals(sub.EventName));
                    var currentEvent  = _test.Events.First(x => x.Name.Equals(sub.EventName));

                    if (Math.Abs(currentEvent.Duration - previuosEvent.Duration) > sub.MaxDifference)
                    {
                        var subscription = _configuration.EventDurationSubscriptions.FirstOrDefault(x => x.Name.Equals(sub.Name));
                        if (subscription != null)
                        {
                            ReportEmailHelper.Send(_configuration.SendFromList, subscription.TargetEmails,
                                                   _test, _screenshotsPath, _configuration.AddLinksInsideEmail,
                                                   true, sub.EventName, previuosEvent);
                        }
                        else if (sub.FullPath != null)
                        {
                            subscription = ReportXMLHelper.Load <EventDurationSubscription>(sub.FullPath);
                            ReportEmailHelper.Send(_configuration.SendFromList, subscription.TargetEmails,
                                                   _test, _screenshotsPath, _configuration.AddLinksInsideEmail,
                                                   true, sub.EventName, previuosEvent);
                        }
                        else if (sub.Targets.Any())
                        {
                            ReportEmailHelper.Send(_configuration.SendFromList, sub.Targets,
                                                   _test, _screenshotsPath, _configuration.AddLinksInsideEmail,
                                                   true, sub.EventName, previuosEvent);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                InternalLogs.Exception(ex, "Exception in SendEmail");
            }
        }