Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
nvborisenko committed Nov 11, 2020
2 parents 856cf6c + eb92c7b commit be4f501
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
using ReportPortal.Shared.Internal.Logging;
using ReportPortal.Shared.Reporter;
using System.Collections.Generic;
using System.Threading;
using TechTalk.SpecFlow;

namespace ReportPortal.SpecFlowPlugin.LogHandler
{
public class FirstActiveTestLogHandler : ICommandsListener
public class ContextAwareLogHandler : ICommandsListener
{
private readonly ITraceLogger _traceLogger = TraceLogManager.Instance.GetLogger<FirstActiveTestLogHandler>();
private readonly ITraceLogger _traceLogger = TraceLogManager.Instance.GetLogger<ContextAwareLogHandler>();

public void Initialize(ICommandsSource commandsSource)
{
Expand Down Expand Up @@ -96,18 +98,60 @@ private void CommandsSource_OnEndLogScopeCommand(Shared.Execution.ILogContext lo
}
}

private static readonly AsyncLocal<ScenarioStepContext> _activeStepContext = new AsyncLocal<ScenarioStepContext>();

public static ScenarioStepContext ActiveStepContext
{
get
{
return _activeStepContext.Value;
}
set
{
_activeStepContext.Value = value;
}
}

private static readonly AsyncLocal<ScenarioContext> _activeScenarioContext = new AsyncLocal<ScenarioContext>();

public static ScenarioContext ActiveScenarioContext
{
get
{
return _activeScenarioContext.Value;
}
set
{
_activeScenarioContext.Value = value;
}
}

private static readonly AsyncLocal<FeatureContext> _activeFeatureContext = new AsyncLocal<FeatureContext>();

public static FeatureContext ActiveFeatureContext
{
get
{
return _activeFeatureContext.Value;
}
set
{
_activeFeatureContext.Value = value;
}
}

public ITestReporter GetCurrentTestReporter()
{
var testReporter = ReportPortalAddin.GetStepTestReporter(TechTalk.SpecFlow.ScenarioStepContext.Current);
var testReporter = ReportPortalAddin.GetStepTestReporter(ActiveStepContext);

if (testReporter == null)
{
testReporter = ReportPortalAddin.GetScenarioTestReporter(TechTalk.SpecFlow.ScenarioContext.Current);
testReporter = ReportPortalAddin.GetScenarioTestReporter(ActiveScenarioContext);
}

if (testReporter == null)
{
testReporter = ReportPortalAddin.GetFeatureTestReporter(TechTalk.SpecFlow.FeatureContext.Current);
testReporter = ReportPortalAddin.GetFeatureTestReporter(ActiveFeatureContext);
}

return testReporter;
Expand Down
19 changes: 19 additions & 0 deletions src/ReportPortal.SpecFlowPlugin/ReportPortalHooks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using ReportPortal.Shared.Reporter;
using ReportPortal.SpecFlowPlugin.EventArguments;
using ReportPortal.SpecFlowPlugin.Extensions;
using ReportPortal.SpecFlowPlugin.LogHandler;
using TechTalk.SpecFlow;

namespace ReportPortal.SpecFlowPlugin
Expand Down Expand Up @@ -137,6 +138,8 @@ public static void BeforeFeature(FeatureContext featureContext)
{
if (_launchReporter != null)
{
ContextAwareLogHandler.ActiveFeatureContext = featureContext;

lock (LockHelper.GetLock(FeatureInfoEqualityComparer.GetFeatureInfoHashCode(featureContext.FeatureInfo)))
{
var currentFeature = ReportPortalAddin.GetFeatureTestReporter(featureContext);
Expand Down Expand Up @@ -212,13 +215,19 @@ public static void AfterFeature(FeatureContext featureContext)
{
_traceLogger.Error(exp.ToString());
}
finally
{
ContextAwareLogHandler.ActiveFeatureContext = null;
}
}

[BeforeScenario(Order = -20000)]
public void BeforeScenario()
{
try
{
ContextAwareLogHandler.ActiveScenarioContext = this.ScenarioContext;

var currentFeature = ReportPortalAddin.GetFeatureTestReporter(this.FeatureContext);

if (currentFeature != null)
Expand Down Expand Up @@ -366,13 +375,19 @@ public void AfterScenario()
{
_traceLogger.Error(exp.ToString());
}
finally
{
ContextAwareLogHandler.ActiveScenarioContext = null;
}
}

[BeforeStep(Order = -20000)]
public void BeforeStep()
{
try
{
ContextAwareLogHandler.ActiveStepContext = this.StepContext;

var currentScenario = ReportPortalAddin.GetScenarioTestReporter(this.ScenarioContext);

var stepInfoRequest = new StartTestItemRequest
Expand Down Expand Up @@ -442,6 +457,10 @@ public void AfterStep()
{
_traceLogger.Error(exp.ToString());
}
finally
{
ContextAwareLogHandler.ActiveStepContext = null;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
using NUnit.Framework;

[assembly: Parallelizable(ParallelScope.Fixtures)]

0 comments on commit be4f501

Please sign in to comment.