Record video of automation testcases using Specflow selenium c#. During the automation some time we need to record the complete execution, If we are using the Nunit or TDD approach we just need to use the tag “[WatchDog(SaveInClass.AllTests)]” before the class and it’s done.
It will record your execution and store them into your debug folder of the project. But when it comes to applying the same thing in BDD. We need to create a custom method. In this Specflow Tutorial, I am going to show you a way of Video recording in selenium using Specflow C#. Let’s dive into this step by step.
Steps to record video of automation testcases using Specflow
Step 1) Install the NuGet package for Microsoft.Expression.Encoder
Step 2) Install Microsoft Expression Encoder 4
Step 3) Implement the code in c# to record the video
Install Microsoft.Expression.Encoder package from NuGet and install the exe in your machine from Microsoft.
Record video of automation testcases in C#
Private static readonly ScreenCaptureJob videoRec = new ScreenCaptureJob(); videoRec .OutputScreenCaptureFileName = path; videoRec.Start();
The above code will record video and save that video at your location which will be your path.
How to take dynamic path of the project to save recorded video.
string path1 = AppDomain.CurrentDomain.BaseDirectory.Replace("\\bin\\Debug", ""); string path = path1 + "Video\\" + scenarioTitle + "" + timestamp + ".wmv";
This path will save the recorded video in the “Video” folder of the project.
Stop and dispose of the recording
videoRec.Stop();
videoRec.Dispose();
Let’s have look of complete code
Create a class RecordVideo.cs using Microsoft.Expression.Encoder.ScreenCapture; using System; namespace SpecFlow { classRecordVideo { privatestaticreadonly ScreenCaptureJob vidRec = new ScreenCaptureJob(); publicstaticvoid StartRecordingVideo(string scenarioTitle) { string timestamp = DateTime.Now.ToString("dd-MM-yyyy-hh-mm-ss"); string path1 = AppDomain.CurrentDomain.BaseDirectory.Replace("\\bin\\Debug", ""); string path = path1 + "Report\\" + scenarioTitle + "" + timestamp + ".wmv"; vidRec.OutputScreenCaptureFileName = path; vidRec.Start(); } publicstaticvoid EndRecording() { vidRec.Stop(); vidRec.Dispose(); } } }
Call video recording methods in the Hooks
[AfterScenario] publicvoid AfterScenario() { Console.WriteLine("AfterScenario"); RecordVideo.EndRecording(); //implement logic that has to run after executing each scenario } [BeforeScenario] publicvoid BeforeScenario() { RecordVideo.StartRecordingVideo(ScenarioContext.Current.ScenarioInfo.Title); }