Class JsonScreenplayReporter
- Namespace
- CSF.Screenplay.Reporting
- Assembly
- CSF.Screenplay.dll
An object which subscribes/listens to the events of IHasPerformanceEvents and which produces a JSON-formatted report from them.
public sealed class JsonScreenplayReporter : IReporter, IDisposable
- Inheritance
-
JsonScreenplayReporter
- Implements
- Inherited Members
Remarks
Note that instances of this class are not reusable; this is a stateful type. Each instance builds and writes a single report file which corresponds to the execution of a single instance of Screenplay. To write a second report, or to write a report about a different Screenplay, please create a new instance of this type.
This type works by accumulating reporting information about IPerformance instances using ScreenplayReportBuilder. Each of these performances may themselves accumulate information from the various events which occur during the performance lifetime. Once a performance is ended (success or otherwise), the completed PerformanceReport may be retrieved from the screenplay report builder. This is then written & flushed to the JSON file.
The writing of JSON must be performed in a thread-safe manner, recall that multiple performances could be ongoing at the same time, in parallel.
Additionally, it is important to write the JSON as-we-progress, rather than waiting until the end of the Screenplay, or the ScreenplayEnded event. That's because in many scenarios, the end of the Screenplay is triggered by the assembly-unload events for the whole process. This is particularly true for software testing frameworks. If we waited to write all of the JSON at the very end of the process then it is very likely that the write will be interrupted. Logic triggered from unload event handlers is permitted only a tiny amount of system compute resource and is inappropriate for performing anything substantial. This is why this type writes each performance to JSON as it completes, essentially streaming the information to the JSON file. This means that all that is left to be done when the Screenplay completes is to write a few closing symbols and then close the file.
Constructors
JsonScreenplayReporter(Stream, ScreenplayReportBuilder)
Initializes a new instance of JsonScreenplayReporter for a specified file path.
public JsonScreenplayReporter(Stream writeStream, ScreenplayReportBuilder builder)
Parameters
writeStream
StreamThe stream to which the JSON report shall be written.
builder
ScreenplayReportBuilderThe Screenplay report builder
Exceptions
- ArgumentNullException
If
writeStream
is null.
Methods
Dispose()
public void Dispose()
SubscribeTo(IHasPerformanceEvents)
Subscribes to the events emitted by the specified Screenplay event notifier.
public void SubscribeTo(IHasPerformanceEvents events)
Parameters
events
IHasPerformanceEventsA Screenplay event notifier
Remarks
As events are received, this reporter instance may accumulate information about the Screnplay that it is to report upon.
UnsubscribeFrom(IHasPerformanceEvents)
Unsubscribes from the specified Screenplay event notifier.
public void UnsubscribeFrom(IHasPerformanceEvents events)
Parameters
events
IHasPerformanceEventsA Screenplay event notifier
Remarks
Use this method only after the event notifier has emitted the ScreenplayEnded event. If this reporter unsubscribes from Screenplay events before the Screenplay has ended then the results are undefined. This could lead to a corrupt report file.