VLC

Recording Videos of your Selenium Tests

It is good practice to take a screen grab of test failures and review them later, particularly if you have some bizarre wait or timeout error that can be difficult to pin down but sometimes even these can lead you down the wrong path or not give enough context to what went wrong. This led me to explore being able to capture videos of running tests that can be scrutinised at a later time, much like you get when you run tests in SauceLabs or BrowserStack.

I created a helper class called RecordVideo and pulled down a NuGet package called Microsoft Expression Encoder. You will also need to install Microsoft Expression Encoder 4.

Having set up a start (and stop) recording method we can easily capture tests in and write them out to file with the help of our set up or specflow hooks.

Here’s how:

using System;
using Microsoft.Expression.Encoder.ScreenCapture;

public class RecordVideo
{
private static readonly ScreenCaptureJob vidRec = new ScreenCaptureJob();

public static void StartRecordingVideo(string scenarioTitle)
{
string timestamp = DateTime.Now.ToString("dd-MM-yyyy-hh-mm-ss");
vidRec.OutputScreenCaptureFileName = @"C:\Test Videos" + scenarioTitle + " " + timestamp + ".wmv";
vidRec.Start();
}

public static void EndRecording()
{
vidRec.Stop();
vidRec.Dispose();
}
}

We can now call these in our setup/ teardown (in my case I’m using Scenario Hooks) and use the test case name for the file title along with a timestamp.

[BeforeScenario]
public void BeforeScenario()
{
RecordVideo.StartRecordingVideo(ScenarioContext.Current.ScenarioInfo.Title);
objectContainer.RegisterInstanceAs(new PageContext(TestRunContext.Driver));
}
[AfterScenario]
public void AfterScenario()
{
if (ScenarioContext.Current.TestError != null)
{
TakeScreenShot.SaveScreenShot(TestRunContext.Driver, TestFailurePath + ScenarioContext.Current.ScenarioInfo.Title + ".jpeg");
}

RecordVideo.EndRecording();
}

Be sure to put something in place to clear the directory out of videos on each run or you might find yourself running out of disk space quite quickly.



  • Hey Justin,
    I know this is a bit of an older post, but I just found your blog not too long ago and I’m trying this one out. I’m curious if you experienced this issue that I am having. When I attempt to play the video that was created I get a message saying “This item was encoded in a format that’s not supported”. I am using the wmv file type like you have designated. I found that if I sued the xesc file format that Encoder has I am able to play the file, but only using there Encoder software. Any tips?

    By the way, your blog has some of the best content of any automation blog I have seen out there! You are a rock star, sir!

    • Hi Patrick,

      Thanks for the kind words 🙂

      To answer your question, I haven’t experienced that but I suspect perhaps the machine you are using to playback the video is lacking in some codecs. A quick search led me here: https://www.microsoft.com/en-us/download/details.aspx?id=10732 and would be worth a try.

      What media player are you using? I can play mine back with Windows Media Player but maybe VLC would do the job better for you… or maybe try to reinstall Encoder4 and see if any other codec option is in there somewhere (really can’t remember there!).

      Let me know if you find a solution 🙂


Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.