-
Notifications
You must be signed in to change notification settings - Fork 754
/
ScenarioContext.cs
110 lines (92 loc) · 3.92 KB
/
ScenarioContext.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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using BoDi;
using TechTalk.SpecFlow.Bindings;
using TechTalk.SpecFlow.Infrastructure;
namespace TechTalk.SpecFlow
{
public interface IScenarioContext : ISpecFlowContext
{
ScenarioInfo ScenarioInfo { get; }
ScenarioBlock CurrentScenarioBlock { get; }
IObjectContainer ScenarioContainer { get; }
ScenarioExecutionStatus ScenarioExecutionStatus { get; }
}
public class ScenarioContext : SpecFlowContext, IScenarioContext
{
#region Singleton
private static bool isCurrentDisabled = false;
private static ScenarioContext current;
[Obsolete("Please get the ScenarioContext via Context Injection - https://go.specflow.org/Migrate-ScenarioContext-Current")]
public static ScenarioContext Current
{
get
{
if (isCurrentDisabled)
throw new SpecFlowException("The ScenarioContext.Current static accessor cannot be used in multi-threaded execution. Try injecting the scenario context to the binding class. See https://go.specflow.org/doc-multithreaded for details.");
if (current == null)
{
Debug.WriteLine("Accessing NULL ScenarioContext");
}
return current;
}
internal set
{
if (!isCurrentDisabled)
current = value;
}
}
internal static void DisableSingletonInstance()
{
isCurrentDisabled = true;
Thread.MemoryBarrier();
current = null;
}
#endregion
public ScenarioInfo ScenarioInfo { get; }
public ScenarioBlock CurrentScenarioBlock { get; internal set; }
public IObjectContainer ScenarioContainer { get; }
public ScenarioExecutionStatus ScenarioExecutionStatus { get; internal set; }
internal List<string> PendingSteps { get; }
internal List<StepInstance> MissingSteps { get; }
internal Stopwatch Stopwatch { get; }
private readonly ITestObjectResolver testObjectResolver;
internal ScenarioContext(IObjectContainer scenarioContainer, ScenarioInfo scenarioInfo, ITestObjectResolver testObjectResolver)
{
this.ScenarioContainer = scenarioContainer;
this.testObjectResolver = testObjectResolver;
Stopwatch = new Stopwatch();
Stopwatch.Start();
CurrentScenarioBlock = ScenarioBlock.None;
ScenarioInfo = scenarioInfo;
ScenarioExecutionStatus = ScenarioExecutionStatus.OK;
PendingSteps = new List<string>();
MissingSteps = new List<StepInstance>();
}
public ScenarioStepContext StepContext => ScenarioContainer.Resolve<IContextManager>().StepContext;
public void Pending()
{
throw new PendingStepException();
}
public static void StepIsPending()
{
throw new PendingStepException();
}
/// <summary>
/// Called by SpecFlow infrastructure when an instance of a binding class is needed.
/// </summary>
/// <param name="bindingType">The type of the binding class.</param>
/// <returns>The binding class instance</returns>
/// <remarks>
/// The binding classes are the classes with the [Binding] attribute, that might
/// contain step definitions, hooks or step argument transformations. The method
/// is called when any binding method needs to be called.
/// </remarks>
public object GetBindingInstance(Type bindingType)
{
return testObjectResolver.ResolveBindingInstance(bindingType, ScenarioContainer);
}
}
}