Video for Your Selenium Tests Using FFmpeg

Using screenshots for your Selenium tests when they fail is cool, but there are some limitations. For example, it’s hard to know exactly what happened before a failure occurred using a snapshot. In my case, the biggest issue is that our applications sometimes have random modal popups appear that cause the Selenium image to be just a black screen — at least that’s my theory.


The problem is that the image only shows the browser’s viewport, not the full desktop. Rather than a screenshot, I feel that having a video recording would better help me to debug what is actually happening.


BlankScreen

My solution is to have our CI system start the recording before our test run, then stop it after the entire test scenario is completed.


Since our test log files are time stamped, we can navigate to that time in the video recording to see what was going on. This method is kind of crude, since it will record nonstop, but it’s a good first step. 

To do this we choose FFmpeg. (You could, of course, bypass all this by simply using Sauce Labs, which has a much more advanced video recording option.)

FFmpeg is an open-source, command line video/encoding software utility that allows you to record video and audio of your desktop. The first issue I found with it is that it’s not very clear how to go about installing FFmpeg, and what commands you need to use to create a recording. So, I decided to document what I had to do to get it working on my machine and share it with you.

What you’ll need for recording Selenium test runs:

First, install FFmpeg

 ffmpeg –list_devices true –f dshow –i dummy

BadDevices

Notice that when I do this I get the error message Could not enumerate video devices (or none found).

This means that my machine doesn’t currently have any DirectShow video devices installed. FFmpeg needs a video device to use in order to record.

Install Screen_Capture_Recorder


The screen capture tool I chose was screen_capture_recorder, because it’s a free, open-source “screen capture” device. If you got the same video device error I did, you can easily install a valid video device by following these steps:

     ffmpeg –list_devices true –f dshow –i dummy


You should now see a valid video device listed:

GoodDevices

Cool. Now it’s time to record!

Command to record a screen capture using ffmeg

 ffmpeg -f dshow -i video="screen-capture-recorder" -r 30 -vcodec mpeg4 -vtag xvid -qscale:v 0 bddrun.mkv

How to view the recording?


Notice how ffmpeg saves the recording as a .mkv file. In order to view it, you’ll need to first convert to an mp4 file.

     ffmpeg –I name_of_yourFile.mkv –codec copy bddTestRun.mp4

Tips

Make sure your cmd.exe screen buffer size has a wide enough width.

cmd

Make a recording batch file


Instead of always having to type this command, whenever you want to record, you can create a batch file that contains the ffmpeg record command by following these steps:

	@ECHO OFF
        ffmpeg -f dshow -i video="screen-capture-recorder" -r 30 -threads 0 bddrun.mkv