public static void AfterFeature(FeatureContext featureContext)
        {
            lock (LockHelper.GetLock(FeatureInfoEqualityComparer.GetFeatureInfoHashCode(featureContext.FeatureInfo)))
            {
                var currentFeature       = ReportPortalAddin.GetFeatureTestReporter(featureContext);
                var remainingThreadCount = ReportPortalAddin.DecrementFeatureThreadCount(featureContext);

                if (currentFeature != null && currentFeature.FinishTask == null && remainingThreadCount == 0)
                {
                    var request = new FinishTestItemRequest
                    {
                        EndTime = DateTime.UtcNow,
                        Status  = Status.Skipped
                    };

                    var eventArg = new TestItemFinishedEventArgs(Bridge.Service, request, currentFeature);
                    ReportPortalAddin.OnBeforeFeatureFinished(null, eventArg);

                    if (!eventArg.Canceled)
                    {
                        currentFeature.Finish(request);
                        currentFeature.FinishTask.Wait();

                        ReportPortalAddin.OnAfterFeatureFinished(null, new TestItemFinishedEventArgs(Bridge.Service, request, currentFeature));
                    }
                }
            }
        }
Example #2
0
        public void AfterScenario()
        {
            if (CurrentScenarioId != null)
            {
                var request = new FinishTestItemRequest
                {
                    EndTime = DateTime.UtcNow,
                };

                var eventArg = new TestItemFinishedEventArgs(Bridge.Service, request);
                if (BeforeScenarioFinished != null)
                {
                    BeforeScenarioFinished(this, eventArg);
                }
                if (!eventArg.Canceled)
                {
                    var message = Bridge.Service.FinishTestItem(CurrentScenarioId, request).Info;
                    if (AfterScenarioFinished != null)
                    {
                        AfterScenarioFinished(this, new TestItemFinishedEventArgs(Bridge.Service, request, message));
                    }

                    CurrentScenarioId = null;
                }
            }
        }
 public void SuiteFinished(TestResult result)
 {
     // finish the last suite in stack
     if (_suiteIds.Count != 0)
     {
         var requestFinishSuite = new FinishTestItemRequest
         {
             EndTime = DateTime.UtcNow,
             Status  = _statusMap[result.ResultState]
         };
         var suiteId  = _suiteIds.Pop();
         var eventArg = new TestItemFinishedEventArgs(Bridge.Service, requestFinishSuite, result, null, suiteId);
         if (BeforeSuiteFinished != null)
         {
             BeforeSuiteFinished(this, eventArg);
         }
         if (!eventArg.Canceled)
         {
             var message = Bridge.Service.FinishTestItem(suiteId, requestFinishSuite).Info;
             if (AfterSuiteFinished != null)
             {
                 AfterSuiteFinished(this, new TestItemFinishedEventArgs(Bridge.Service, requestFinishSuite, result, message, suiteId));
             }
         }
     }
 }
Example #4
0
        public static void AfterFeature()
        {
            if (CurrentFeatureId != null)
            {
                var request = new FinishTestItemRequest
                {
                    EndTime = DateTime.UtcNow,
                };

                var eventArg = new TestItemFinishedEventArgs(Bridge.Service, request);
                if (BeforeFeatureFinished != null)
                {
                    BeforeFeatureFinished(null, eventArg);
                }
                if (!eventArg.Canceled)
                {
                    var message = Bridge.Service.FinishTestItem(CurrentFeatureId, request).Info;
                    if (AfterFeatureFinished != null)
                    {
                        AfterFeatureFinished(null, new TestItemFinishedEventArgs(Bridge.Service, request, message));
                    }

                    CurrentFeatureId = null;
                }
            }
        }
Example #5
0
        private static void ReportPortalAddin_AfterFeatureFinished(object sender, TestItemFinishedEventArgs e)
        {
#if NETCOREAPP
            // Workaround how to avoid issue https://github.com/techtalk/SpecFlow/issues/1348 (launch doesn't finish on .netcore tests)
            e.TestReporter.FinishTask.Wait();
#endif
        }
Example #6
0
        public void AfterScenario()
        {
            if (CurrentScenario != null)
            {
                var request = new FinishTestItemRequest
                {
                    EndTime = DateTime.UtcNow,
                    Status  = Status
                };

                var eventArg = new TestItemFinishedEventArgs(Bridge.Service, request, CurrentScenario);
                if (BeforeScenarioFinished != null)
                {
                    BeforeScenarioFinished(this, eventArg);
                }
                if (!eventArg.Canceled)
                {
                    CurrentScenario.Finish(request);
                    if (AfterScenarioFinished != null)
                    {
                        AfterScenarioFinished(this,
                                              new TestItemFinishedEventArgs(Bridge.Service, request, CurrentScenario));
                    }

                    CurrentScenarioDescription = string.Empty;
                }
            }
        }
Example #7
0
        public static void AfterFeature()
        {
            if (CurrentFeature != null)
            {
                var request = new FinishTestItemRequest
                {
                    EndTime = DateTime.UtcNow,
                    Status  = Status.Skipped
                };

                var eventArg = new TestItemFinishedEventArgs(Bridge.Service, request, CurrentFeature);
                if (BeforeFeatureFinished != null)
                {
                    BeforeFeatureFinished(null, eventArg);
                }
                if (!eventArg.Canceled)
                {
                    CurrentFeature.Finish(request);
                    if (AfterFeatureFinished != null)
                    {
                        AfterFeatureFinished(null,
                                             new TestItemFinishedEventArgs(Bridge.Service, request, CurrentFeature));
                    }
                }
            }
        }
 internal static void OnAfterScenarioFinished(object sender, TestItemFinishedEventArgs eventArg)
 {
     try
     {
         AfterScenarioFinished?.Invoke(sender, eventArg);
     }
     catch (Exception exp)
     {
         Logger.Error($"Exception occured in {nameof(OnAfterScenarioFinished)} event handler: {exp}");
     }
 }
Example #9
0
 private static void ReportPortalAddin_BeforeScenarioFinished(object sender, TestItemFinishedEventArgs e)
 {
     if (e.ScenarioContext.TestError != null && e.ScenarioContext.ScenarioInfo.Title == "System Error")
     {
         e.FinishTestItemRequest.Issue = new Issue
         {
             Type    = WellKnownIssueType.SystemIssue,
             Comment = "my custom system error comment"
         };
     }
 }
        public void TestFinished(TestResult result)
        {
            if (result.Message != null && _testId != null)
            {
                Bridge.Service.AddLogItem(new AddLogItemRequest
                {
                    TestItemId = _testId,
                    Time       = DateTime.UtcNow,
                    Level      = LogLevel.Error,
                    Text       = result.Message + "\n" + result.StackTrace
                });
            }

            var requestUpdateTest = new UpdateTestItemRequest
            {
                Description = result.Description,
                Tags        = (from object tag in result.Test.Categories select tag.ToString()).ToList()
            };

            Bridge.Service.UpdateTestItem(_testId, requestUpdateTest);

            var requestFinishTest = new FinishTestItemRequest
            {
                EndTime = DateTime.UtcNow,
                Status  = _statusMap[result.ResultState]
            };

            var eventArg = new TestItemFinishedEventArgs(Bridge.Service, requestFinishTest, result, null, _testId);

            if (BeforeTestFinished != null)
            {
                BeforeTestFinished(this, eventArg);
            }
            if (!eventArg.Canceled)
            {
                var message = Bridge.Service.FinishTestItem(_testId, requestFinishTest).Info;

                if (AfterTestFinished != null)
                {
                    AfterTestFinished(this, new TestItemFinishedEventArgs(Bridge.Service, requestFinishTest, result, message, _testId));
                }

                _testId = null;
                Bridge.Context.TestId = null;
            }
        }
Example #11
0
        public static void AfterFeature(FeatureContext featureContext)
        {
            try
            {
                lock (LockHelper.GetLock(FeatureInfoEqualityComparer.GetFeatureInfoHashCode(featureContext.FeatureInfo)))
                {
                    var currentFeature       = ReportPortalAddin.GetFeatureTestReporter(featureContext);
                    var remainingThreadCount = ReportPortalAddin.DecrementFeatureThreadCount(featureContext);

                    if (currentFeature != null && currentFeature.FinishTask == null && remainingThreadCount == 0)
                    {
                        var request = new FinishTestItemRequest
                        {
                            EndTime = DateTime.UtcNow,
                            Status  = Status.Skipped
                        };

                        var eventArg = new TestItemFinishedEventArgs(_service, request, currentFeature, featureContext, null);
                        ReportPortalAddin.OnBeforeFeatureFinished(null, eventArg);

                        if (!eventArg.Canceled)
                        {
                            currentFeature.Finish(request);

                            ReportPortalAddin.OnAfterFeatureFinished(null, new TestItemFinishedEventArgs(_service, request, currentFeature, featureContext, null));
                        }

                        ReportPortalAddin.RemoveFeatureTestReporter(featureContext, currentFeature);
                    }
                }
            }
            catch (Exception exp)
            {
                _traceLogger.Error(exp.ToString());
            }
            finally
            {
                ContextAwareLogHandler.ActiveFeatureContext = null;
            }
        }
        private void FinishSuite(XmlDocument xmlDoc)
        {
            try
            {
                var id       = xmlDoc.SelectSingleNode("/*/@id").Value;
                var result   = xmlDoc.SelectSingleNode("/*/@result").Value;
                var parentId = xmlDoc.SelectSingleNode("/*/@parentId");

                // at the end of execution nunit raises 2 the same events, we need only that which has 'parentId' xml tag
                if (parentId != null)
                {
                    if (_suitesFlow.ContainsKey(id))
                    {
                        var updateSuiteRequest = new UpdateTestItemRequest();

                        // adding categories to suite
                        var categories = xmlDoc.SelectNodes("//properties/property[@name='Category']");
                        if (categories != null)
                        {
                            updateSuiteRequest.Tags = new List <string>();

                            foreach (XmlNode category in categories)
                            {
                                updateSuiteRequest.Tags.Add(category.Attributes["value"].Value);
                            }
                        }

                        // adding description to suite
                        var description = xmlDoc.SelectSingleNode("//properties/property[@name='Description']");
                        if (description != null)
                        {
                            updateSuiteRequest.Description = description.Attributes["value"].Value;
                        }

                        if (updateSuiteRequest.Description != null || updateSuiteRequest.Tags != null)
                        {
                            _suitesFlow[id].AdditionalTasks.Add(Task.Run(async() =>
                            {
                                _suitesFlow[id].StartTask.Wait();
                                await Bridge.Service.UpdateTestItemAsync(_suitesFlow[id].TestId, updateSuiteRequest);
                            }));
                        }

                        // finishing suite
                        var finishSuiteRequest = new FinishTestItemRequest
                        {
                            EndTime = DateTime.UtcNow,
                            Status  = _statusMap[result]
                        };

                        var eventArg = new TestItemFinishedEventArgs(Bridge.Service, finishSuiteRequest, _suitesFlow[id]);

                        try
                        {
                            if (BeforeSuiteFinished != null)
                            {
                                BeforeSuiteFinished(this, eventArg);
                            }
                        }
                        catch (Exception exp)
                        {
                            Console.WriteLine("Exception was thrown in 'BeforeSuiteFinished' subscriber." + Environment.NewLine + exp);
                        }

                        _suitesFlow[id].Finish(finishSuiteRequest);

                        try
                        {
                            if (AfterSuiteFinished != null)
                            {
                                AfterSuiteFinished(this, new TestItemFinishedEventArgs(Bridge.Service, finishSuiteRequest, _suitesFlow[id]));
                            }
                        }
                        catch (Exception exp)
                        {
                            Console.WriteLine("Exception was thrown in 'AfterSuiteFinished' subscriber." + Environment.NewLine + exp);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine("ReportPortal exception was thrown." + Environment.NewLine + exception);
            }
        }
        private void FinishSuite(XmlDocument xmlDoc)
        {
            try
            {
                var type = xmlDoc.SelectSingleNode("/*/@type").Value;

                var id       = xmlDoc.SelectSingleNode("/*/@id").Value;
                var result   = xmlDoc.SelectSingleNode("/*/@result").Value;
                var parentId = xmlDoc.SelectSingleNode("/*/@parentId");
                var duration = float.Parse(xmlDoc.SelectSingleNode("/*/@duration").Value, System.Globalization.CultureInfo.InvariantCulture);

                // at the end of execution nunit raises 2 the same events, we need only that which has 'parentId' xml tag
                if (parentId != null)
                {
                    if (!_flowItems.ContainsKey(id))
                    {
                        StartSuite(xmlDoc);
                    }

                    if (_flowItems.ContainsKey(id))
                    {
                        // finishing suite
                        var finishSuiteRequest = new FinishTestItemRequest
                        {
                            EndTime = _flowItems[id].StartTime.AddSeconds(duration),
                            Status  = _statusMap[result]
                        };

                        // adding categories to suite
                        var categories = xmlDoc.SelectNodes("//properties/property[@name='Category']");
                        if (categories != null)
                        {
                            finishSuiteRequest.Tags = new List <string>();

                            foreach (XmlNode category in categories)
                            {
                                finishSuiteRequest.Tags.Add(category.Attributes["value"].Value);
                            }
                        }

                        // adding description to suite
                        var description = xmlDoc.SelectSingleNode("//properties/property[@name='Description']");
                        if (description != null)
                        {
                            finishSuiteRequest.Description = description.Attributes["value"].Value;
                        }

                        var eventArg = new TestItemFinishedEventArgs(Bridge.Service, finishSuiteRequest, _flowItems[id].TestReporter, xmlDoc.OuterXml);

                        try
                        {
                            BeforeSuiteFinished?.Invoke(this, eventArg);
                        }
                        catch (Exception exp)
                        {
                            Console.WriteLine("Exception was thrown in 'BeforeSuiteFinished' subscriber." + Environment.NewLine + exp);
                        }

                        Action <string, FinishTestItemRequest, string, string> finishSuiteAction = (__id, __finishSuiteRequest, __report, __parentstacktrace) =>
                        {
                            // find all defferred children test items to finish
                            var deferredFlowItems = _flowItems.Where(fi => fi.Value.ParentId == __id && fi.Value.DeferredFinishAction != null).Select(fi => fi.Value).ToList();
                            foreach (var deferredFlowItem in deferredFlowItems)
                            {
                                deferredFlowItem.DeferredFinishAction.Invoke(deferredFlowItem.Id, deferredFlowItem.FinishTestItemRequest, deferredFlowItem.Report, __parentstacktrace);
                            }

                            _flowItems[__id].TestReporter?.Finish(__finishSuiteRequest);

                            try
                            {
                                AfterSuiteFinished?.Invoke(this, new TestItemFinishedEventArgs(Bridge.Service, __finishSuiteRequest, _flowItems[__id].TestReporter, __report));
                            }
                            catch (Exception exp)
                            {
                                Console.WriteLine("Exception was thrown in 'AfterSuiteFinished' subscriber." + Environment.NewLine + exp);
                            }

                            _flowItems.Remove(__id);
                        };

                        // understand whether finishing test suite should be defferred. Usually we need it to report stacktrace in case of OneTimeSetup method fails, and stacktrace is avalable later in "FinishSuite" method
                        if (xmlDoc.SelectSingleNode("/*/@site")?.Value == "Parent")
                        {
                            _flowItems[id].FinishTestItemRequest = finishSuiteRequest;
                            _flowItems[id].Report = xmlDoc.OuterXml;
                            _flowItems[id].DeferredFinishAction = finishSuiteAction;
                        }
                        else
                        {
                            var failurestacktrace = xmlDoc.SelectSingleNode("//failure/stack-trace")?.InnerText;

                            finishSuiteAction.Invoke(id, finishSuiteRequest, xmlDoc.OuterXml, failurestacktrace);
                        }
                    }
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine("ReportPortal exception was thrown." + Environment.NewLine + exception);
            }
        }
        public void FinishTest(XmlDocument xmlDoc)
        {
            try
            {
                var id       = xmlDoc.SelectSingleNode("/*/@id").Value;
                var result   = xmlDoc.SelectSingleNode("/*/@result").Value;
                var parentId = xmlDoc.SelectSingleNode("/*/@parentId");

                if (_testFlowIds.ContainsKey(id))
                {
                    var updateTestRequest = new UpdateTestItemRequest();

                    // adding categories to test
                    var categories = xmlDoc.SelectNodes("//properties/property[@name='Category']");
                    if (categories != null)
                    {
                        updateTestRequest.Tags = new List <string>();

                        foreach (XmlNode category in categories)
                        {
                            updateTestRequest.Tags.Add(category.Attributes["value"].Value);
                        }
                    }

                    // adding description to test
                    var description = xmlDoc.SelectSingleNode("//properties/property[@name='Description']");
                    if (description != null)
                    {
                        updateTestRequest.Description = description.Attributes["value"].Value;
                    }

                    if (updateTestRequest.Description != null || updateTestRequest.Tags != null)
                    {
                        _testFlowIds[id].Update(updateTestRequest);
                    }

                    // adding console output
                    var outputNode = xmlDoc.SelectSingleNode("//output");
                    if (outputNode != null)
                    {
                        _testFlowIds[id].Log(new AddLogItemRequest
                        {
                            Level = LogLevel.Trace,
                            Time  = DateTime.UtcNow,
                            Text  = "Test Output: " + Environment.NewLine + outputNode.InnerText
                        });
                    }

                    // adding failure message
                    var failureNode = xmlDoc.SelectSingleNode("//failure");
                    if (failureNode != null)
                    {
                        var failureMessage    = failureNode.SelectSingleNode("./message").InnerText;
                        var failureStacktrace = failureNode.SelectSingleNode("./stack-trace").InnerText;

                        _testFlowIds[id].Log(new AddLogItemRequest
                        {
                            Level = LogLevel.Error,
                            Time  = DateTime.UtcNow,
                            Text  = failureMessage + Environment.NewLine + failureStacktrace
                        });
                    }

                    // finishing test
                    var finishTestRequest = new FinishTestItemRequest
                    {
                        EndTime = DateTime.UtcNow,
                        Status  = _statusMap[result]
                    };

                    var isRetry = xmlDoc.SelectSingleNode("//properties/property[@name='Retry']");
                    if (isRetry != null)
                    {
                        finishTestRequest.IsRetry = true;
                    }

                    var eventArg = new TestItemFinishedEventArgs(Bridge.Service, finishTestRequest, _testFlowIds[id]);

                    try
                    {
                        if (BeforeTestFinished != null)
                        {
                            BeforeTestFinished(this, eventArg);
                        }
                    }
                    catch (Exception exp)
                    {
                        Console.WriteLine("Exception was thrown in 'BeforeTestFinished' subscriber." +
                                          Environment.NewLine + exp);
                    }

                    _testFlowIds[id].Finish(finishTestRequest);

                    try
                    {
                        if (AfterTestFinished != null)
                        {
                            AfterTestFinished(this,
                                              new TestItemFinishedEventArgs(Bridge.Service, finishTestRequest, _testFlowIds[id]));
                        }
                    }
                    catch (Exception exp)
                    {
                        Console.WriteLine("Exception was thrown in 'AfterTestFinished' subscriber." +
                                          Environment.NewLine + exp);
                    }
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine("ReportPortal exception was thrown." + Environment.NewLine + exception);
            }
        }
Example #15
0
        public void AfterScenario()
        {
            try
            {
                var currentScenario = ReportPortalAddin.GetScenarioTestReporter(this.ScenarioContext);

                if (currentScenario != null)
                {
                    if (this.ScenarioContext.ScenarioExecutionStatus == ScenarioExecutionStatus.TestError)
                    {
                        currentScenario.Log(new CreateLogItemRequest
                        {
                            Level = LogLevel.Error,
                            Time  = DateTime.UtcNow,
                            Text  = this.ScenarioContext.TestError?.ToString()
                        });
                    }
                    else if (this.ScenarioContext.ScenarioExecutionStatus == ScenarioExecutionStatus.BindingError)
                    {
                        currentScenario.Log(new CreateLogItemRequest
                        {
                            Level = LogLevel.Error,
                            Time  = DateTime.UtcNow,
                            Text  = this.ScenarioContext.TestError?.Message
                        });
                    }
                    else if (this.ScenarioContext.ScenarioExecutionStatus == ScenarioExecutionStatus.UndefinedStep)
                    {
                        currentScenario.Log(new CreateLogItemRequest
                        {
                            Level = LogLevel.Error,
                            Time  = DateTime.UtcNow,
                            Text  = new MissingStepDefinitionException().Message
                        });
                    }

                    Issue issue = null;

                    // determine scenario status
                    var status = this.ScenarioContext.ScenarioExecutionStatus == ScenarioExecutionStatus.OK ? Status.Passed : Status.Failed;

                    // handle well-known unit framework's ignore exceptions
                    if (this.ScenarioContext.TestError != null)
                    {
                        var testErrorException = this.ScenarioContext.TestError.GetType();

                        if (testErrorException.FullName.Equals("NUnit.Framework.IgnoreException") ||
                            testErrorException.FullName.Equals("NUnit.Framework.InconclusiveException") ||
                            testErrorException.FullName.Equals("Microsoft.VisualStudio.TestTools.UnitTesting.AssertInconclusiveException") ||
                            testErrorException.FullName.Equals("Xunit.SkipException"))
                        {
                            status = Status.Skipped;
                            issue  = new Issue
                            {
                                Type    = WellKnownIssueType.NotDefect,
                                Comment = this.ScenarioContext.TestError.Message
                            };
                        }
                    }

                    var request = new FinishTestItemRequest
                    {
                        EndTime = DateTime.UtcNow,
                        Status  = status,
                        Issue   = issue
                    };

                    var eventArg = new TestItemFinishedEventArgs(_service, request, currentScenario, this.FeatureContext, this.ScenarioContext);
                    ReportPortalAddin.OnBeforeScenarioFinished(this, eventArg);

                    if (!eventArg.Canceled)
                    {
                        currentScenario.Finish(request);

                        ReportPortalAddin.OnAfterScenarioFinished(this, new TestItemFinishedEventArgs(_service, request, currentScenario, this.FeatureContext, this.ScenarioContext));

                        ReportPortalAddin.RemoveScenarioTestReporter(this.ScenarioContext, currentScenario);
                    }
                }
            }
            catch (Exception exp)
            {
                _traceLogger.Error(exp.ToString());
            }
            finally
            {
                ContextAwareLogHandler.ActiveScenarioContext = null;
            }
        }
        public void AfterScenario()
        {
            var currentScenario = ReportPortalAddin.GetScenarioTestReporter(this.ScenarioContext);

            if (currentScenario != null)
            {
                Issue issue  = null;
                var   status = Status.Passed;

                switch (this.ScenarioContext.ScenarioExecutionStatus)
                {
                case ScenarioExecutionStatus.TestError:
                    status = Status.Failed;

                    issue = new Issue
                    {
                        Type    = WellKnownIssueType.ToInvestigate,
                        Comment = this.ScenarioContext.TestError?.Message
                    };

                    currentScenario.Log(new AddLogItemRequest
                    {
                        Level = LogLevel.Error,
                        Time  = DateTime.UtcNow,
                        Text  = this.ScenarioContext.TestError?.ToString()
                    });

                    break;

                case ScenarioExecutionStatus.BindingError:
                    status = Status.Failed;

                    issue = new Issue
                    {
                        Type    = WellKnownIssueType.AutomationBug,
                        Comment = this.ScenarioContext.TestError?.Message
                    };

                    currentScenario.Log(new AddLogItemRequest
                    {
                        Level = LogLevel.Error,
                        Time  = DateTime.UtcNow,
                        Text  = this.ScenarioContext.TestError?.Message
                    });

                    break;

                case ScenarioExecutionStatus.UndefinedStep:
                    status = Status.Failed;

                    issue = new Issue
                    {
                        Type    = WellKnownIssueType.AutomationBug,
                        Comment = new MissingStepDefinitionException().Message
                    };

                    currentScenario.Log(new AddLogItemRequest
                    {
                        Level = LogLevel.Error,
                        Time  = DateTime.UtcNow,
                        Text  = new MissingStepDefinitionException().Message
                    });

                    break;

                case ScenarioExecutionStatus.StepDefinitionPending:
                    status = Status.Failed;

                    issue = new Issue
                    {
                        Type    = WellKnownIssueType.ToInvestigate,
                        Comment = "Pending"
                    };

                    break;
                }

                var request = new FinishTestItemRequest
                {
                    EndTime = DateTime.UtcNow.AddMilliseconds(1),
                    Status  = status,
                    Issue   = issue
                };

                var eventArg = new TestItemFinishedEventArgs(Bridge.Service, request, currentScenario);
                ReportPortalAddin.OnBeforeScenarioFinished(this, eventArg);

                if (!eventArg.Canceled)
                {
                    currentScenario.Finish(request);
                    currentScenario.FinishTask.Wait();

                    ReportPortalAddin.OnAfterScenarioFinished(this, new TestItemFinishedEventArgs(Bridge.Service, request, currentScenario));
                }
            }
        }
Example #17
0
 internal static void OnAfterScenarioFinished(object sender, TestItemFinishedEventArgs eventArg)
 {
     AfterScenarioFinished?.Invoke(sender, eventArg);
 }
Example #18
0
        public void FinishTest(XmlDocument xmlDoc)
        {
            try
            {
                var id       = xmlDoc.SelectSingleNode("/*/@id").Value;
                var result   = xmlDoc.SelectSingleNode("/*/@result").Value;
                var parentId = xmlDoc.SelectSingleNode("/*/@parentId");
                var duration = float.Parse(xmlDoc.SelectSingleNode("/*/@duration").Value, System.Globalization.CultureInfo.InvariantCulture);

                if (!_flowItems.ContainsKey(id))
                {
                    StartTest(xmlDoc);
                }

                if (_flowItems.ContainsKey(id))
                {
                    // adding console output
                    var outputNode = xmlDoc.SelectSingleNode("//output");
                    if (outputNode != null)
                    {
                        var outputLogRequest = new AddLogItemRequest
                        {
                            Level = LogLevel.Trace,
                            Time  = DateTime.UtcNow,
                            Text  = "Test Output: " + Environment.NewLine + outputNode.InnerText
                        };

                        var outputEventArgs = new TestItemOutputEventArgs(Bridge.Service, outputLogRequest, _flowItems[id].TestReporter, xmlDoc.OuterXml);

                        try
                        {
                            BeforeTestOutput?.Invoke(this, outputEventArgs);
                        }
                        catch (Exception exp)
                        {
                            Console.WriteLine("Exception was thrown in 'BeforeTestOutput' subscriber." + Environment.NewLine + exp);
                        }

                        if (!outputEventArgs.Canceled)
                        {
                            _flowItems[id].TestReporter.Log(outputLogRequest);

                            try
                            {
                                AfterTestOutput?.Invoke(this, outputEventArgs);
                            }
                            catch (Exception exp)
                            {
                                Console.WriteLine("Exception was thrown in 'AfterTestOutput' subscriber." + Environment.NewLine + exp);
                            }
                        }
                    }

                    // adding attachments
                    var attachmentNodes = xmlDoc.SelectNodes("//attachments/attachment");
                    foreach (XmlNode attachmentNode in attachmentNodes)
                    {
                        var filePath        = attachmentNode.SelectSingleNode("./filePath").InnerText;
                        var fileDescription = attachmentNode.SelectSingleNode("./description")?.InnerText;

                        if (File.Exists(filePath))
                        {
                            try
                            {
                                var attachmentLogItemRequest = new AddLogItemRequest
                                {
                                    Level = LogLevel.Info,
                                    Time  = DateTime.UtcNow,
                                    Text  = fileDescription != null ? fileDescription : Path.GetFileName(filePath)
                                };

                                byte[] bytes;

                                using (var fileStream = File.Open(filePath, FileMode.Open, FileAccess.Read))
                                {
                                    using (var memoryStream = new MemoryStream())
                                    {
                                        fileStream.CopyTo(memoryStream);
                                        bytes = memoryStream.ToArray();
                                    }
                                }

                                attachmentLogItemRequest.Attach = new Client.Models.Attach
                                {
                                    Name     = Path.GetFileName(filePath),
                                    MimeType = Shared.MimeTypes.MimeTypeMap.GetMimeType(Path.GetExtension(filePath)),
                                    Data     = bytes
                                };

                                _flowItems[id].TestReporter.Log(attachmentLogItemRequest);
                            }
                            catch (Exception attachmentExp)
                            {
                                _flowItems[id].TestReporter.Log(new AddLogItemRequest
                                {
                                    Level = LogLevel.Warning,
                                    Time  = DateTime.UtcNow,
                                    Text  = $"Cannot read '{filePath}' file: {attachmentExp}"
                                });
                            }
                        }
                        else
                        {
                            _flowItems[id].TestReporter.Log(new AddLogItemRequest
                            {
                                Level = LogLevel.Warning,
                                Time  = DateTime.UtcNow,
                                Text  = $"Attachment file '{filePath}' doesn't exists."
                            });
                        }
                    }

                    // adding failure message
                    var failureNode = xmlDoc.SelectSingleNode("//failure");
                    if (failureNode != null)
                    {
                        var failureMessage    = failureNode.SelectSingleNode("./message")?.InnerText;
                        var failureStacktrace = failureNode.SelectSingleNode("./stack-trace")?.InnerText;

                        _flowItems[id].TestReporter.Log(new AddLogItemRequest
                        {
                            Level = LogLevel.Error,
                            Time  = DateTime.UtcNow,
                            Text  = string.Join(Environment.NewLine, new List <string> {
                                failureMessage, failureStacktrace
                            }.Where(m => !string.IsNullOrEmpty(m)))
                        });

                        // walk through assertions
                        foreach (XmlNode assertionNode in xmlDoc.SelectNodes("test-case/assertions/assertion"))
                        {
                            var assertionMessage    = assertionNode.SelectSingleNode("message")?.InnerText;
                            var assertionStacktrace = assertionNode.SelectSingleNode("stack-trace")?.InnerText;

                            if (assertionMessage != failureMessage && assertionStacktrace != failureStacktrace)
                            {
                                _flowItems[id].TestReporter.Log(new AddLogItemRequest
                                {
                                    Level = LogLevel.Error,
                                    Time  = DateTime.UtcNow,
                                    Text  = string.Join(Environment.NewLine, new List <string> {
                                        assertionMessage, assertionStacktrace
                                    }.Where(m => !string.IsNullOrEmpty(m)))
                                });
                            }
                        }
                    }

                    // adding reason message
                    var reasonNode = xmlDoc.SelectSingleNode("//reason");
                    if (reasonNode != null)
                    {
                        var reasonMessage = reasonNode.SelectSingleNode("./message")?.InnerText;

                        _flowItems[id].TestReporter.Log(new AddLogItemRequest
                        {
                            Level = LogLevel.Error,
                            Time  = DateTime.UtcNow,
                            Text  = $"Reason: {reasonMessage}"
                        });
                    }

                    // finishing test
                    var finishTestRequest = new FinishTestItemRequest
                    {
                        EndTime = _flowItems[id].StartTime.AddSeconds(duration),
                        Status  = _statusMap[result]
                    };

                    // adding categories to test
                    var categories = xmlDoc.SelectNodes("//properties/property[@name='Category']");
                    if (categories != null)
                    {
                        finishTestRequest.Tags = new List <string>();

                        foreach (XmlNode category in categories)
                        {
                            finishTestRequest.Tags.Add(category.Attributes["value"].Value);
                        }
                    }

                    // adding description to test
                    var description = xmlDoc.SelectSingleNode("//properties/property[@name='Description']");
                    if (description != null)
                    {
                        finishTestRequest.Description = description.Attributes["value"].Value;
                    }

                    var isRetry = xmlDoc.SelectSingleNode("//properties/property[@name='Retry']");
                    if (isRetry != null)
                    {
                        finishTestRequest.IsRetry = true;
                    }

                    var eventArg = new TestItemFinishedEventArgs(Bridge.Service, finishTestRequest, _flowItems[id].TestReporter, xmlDoc.OuterXml);

                    try
                    {
                        BeforeTestFinished?.Invoke(this, eventArg);
                    }
                    catch (Exception exp)
                    {
                        Console.WriteLine("Exception was thrown in 'BeforeTestFinished' subscriber." + Environment.NewLine + exp);
                    }

                    Action <string, FinishTestItemRequest, string, string> finishTestAction = (__id, __finishTestItemRequest, __report, __parentstacktrace) =>
                    {
                        if (!string.IsNullOrEmpty(__parentstacktrace))
                        {
                            _flowItems[__id].TestReporter.Log(new AddLogItemRequest
                            {
                                Level = LogLevel.Error,
                                Time  = DateTime.UtcNow,
                                Text  = __parentstacktrace
                            });
                        }

                        _flowItems[__id].TestReporter.Finish(__finishTestItemRequest);

                        try
                        {
                            AfterTestFinished?.Invoke(this, new TestItemFinishedEventArgs(Bridge.Service, __finishTestItemRequest, _flowItems[__id].TestReporter, __report));
                        }
                        catch (Exception exp)
                        {
                            Console.WriteLine("Exception was thrown in 'AfterTestFinished' subscriber." + Environment.NewLine + exp);
                        }

                        _flowItems.Remove(__id);
                    };
                    // understand whether finishing test item should be deferred. Usually we need it to report stacktrace in case of OneTimeSetup method fails, and stacktrace is avalable later in "FinishSuite" method
                    if (xmlDoc.SelectSingleNode("/*/@site")?.Value == "Parent")
                    {
                        _flowItems[id].FinishTestItemRequest = finishTestRequest;
                        _flowItems[id].Report = xmlDoc.OuterXml;
                        _flowItems[id].DeferredFinishAction = finishTestAction;
                    }
                    else
                    {
                        finishTestAction.Invoke(id, finishTestRequest, xmlDoc.OuterXml, null);
                    }
                }
            }
            catch (Exception exception)
            {
                Console.WriteLine("ReportPortal exception was thrown." + Environment.NewLine + exception);
            }
        }
Example #19
0
 internal static void OnBeforeFeatureFinished(object sender, TestItemFinishedEventArgs eventArg)
 {
     BeforeFeatureFinished?.Invoke(sender, eventArg);
 }