Video recording in selenium using specflow C#

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 Nunit or TDD approch we just need to user the tag “[WatchDog(SaveInClass.AllTests)]” before the class and its done. It will record your execution and store them into your debug folder of the project. But when it comes to apply the same thing in BDD. We need to create a custom method. So here 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 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 form 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();

Above code will record video and save that video at your location which will be your path.

How to take dynamic path of 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 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);

        }

 

Add a Comment

Your email address will not be published. Required fields are marked *

sixteen + 20 =