Recording Selenium Test Execution

After long time, got few min to think about new blog. There are many blog available for each and every solution but I was thinking to have few which is either not available or very limited information available over internet. I have free version of sauce lab and I am always fascinated by this kind of tool and I would love to build such tool sometime in future. There is one feature that is recording the session is always attracted me. I tried to find some resources which can help me to achieve so in my framework.

I found Microsoft expression encoder to do recording your screen. You can use their dlls to add into your C# based framework to execute and record the session. In you framework you can write a method which starts recording since beginning and later during cleanup task, you can have some login to determine if you want to save recording or not. For example, I my framework, I start recording based on my configuration file and in teardown methods, I check if there is an error or not. if yes then I encode into wmv format else I discard recording.




Follow following steps to have code and settings into framework:

Step 1:

Install Microsoft Encoder Expression: Refer CerebrumLabs.com for that (http://www.cerebrumlabs.com/free-screen-recording-software/). Once you install, you may get following folder in installation location. C:\Program Files (x86)Microsoft Expression. If you try to explore few folders inside this folder you will find SDK folder @: C:\Program Files (x86)\Microsoft Expression\Encoder 4\SDK, where you can see Microsoft.Expression.Encoder.dlls which you can use in your framework code.

Step 2:

Include dlls given in SDK folders into your framework. You can write a method and call that. I have a key value pair in my framework config file and I can decide if I want to do recording or not.  In your test’s [setup] method, you can call this method which will start recording at the beginning of the test.

using System;

.

.

using OpenQA.Selenium;

.

.

using Microsoft.Expression.Encoder.ScreenCapture;

using System.Drawing;

using Microsoft.Expression.Encoder.Profiles;

using Microsoft.Expression.Encoder;

namespace FRAMEWORK

{

    //Call this method in setup method.   
    public static void StartRecordingVideo()

    {

        //Provide setting in config file if you want to do recording or not.
        if (testEInfo.isRecording)

        {

            job = new ScreenCaptureJob();

            job.CaptureRectangle = Screen.PrimaryScreen.Bounds;

            job.ShowFlashingBoundary = true;

            //provide the location where you want to save the recording.
            job.OutputPath = AutomationLogging.newLocationInResultFolder;

            job.Start();

        }

    }

}

Step 3:

Now your test started and in the background your screen recording going on. once you reach to the [teardown] method. you can decide if you want to keep the recording or not. In my case I want to keep recording only if there is test failure so my developers can review else there is no point of having recording if tests are passing. To do so I have following method in above code which I call in code at the very end.

public static void StopRecordingVideo()
        {
            if (testEInfo.isRecording)
            {
                string filename = job.ScreenCaptureFileName;
                job.Stop();
                if (AutomationLogging.countOfError > 0)
                {
                    MediaItem src = new MediaItem(filename);
                    Job jb = new Job();
                    jb.MediaItems.Add(src);
                    jb.ApplyPreset(Presets.VC1HD720pVBR);
                    jb.OutputDirectory = AutomationLogging.newLocationInResultFolder;
                    string output = ((Microsoft.Expression.Encoder.JobBase)(jb)).ActualOutputDirectory;
                    jb.Encode();
                }

                File.Delete(filename);
            }
        }

Conclusion:

during encoding, you may notice that encoder is eating little more memory and you system may little slow. Try at your end and let me know if you have any question.

11 Comments

  • zack

    September 9, 2015

    if i use java or python coding the test, how can i use this method?

    Reply
  • Ram

    September 29, 2015

    Aditya you are doing a great Job I really appreciate your work.

    Reply
    • Aditya

      October 8, 2015

      Thanks for your kind words, Ram!

      Reply
  • shweta

    July 20, 2016

    Hi Aditya,
    This is interesting but its not working for me when i follow your blog step by step please let me know if you can help me setting this up for my framework

    Reply
    • aditya

      July 21, 2016

      Can you please share your code which is not working? And please share the error too if you are getting. I am sure you have installed ms expression on the computer you are running your test.

      Reply
      • Reddy

        June 27, 2017

        Aditya,

        I like your blog and your blog posts amazing. i would like to know can you do a post/give me best references on chakram framework..this is also becoming popular for rest api testing..
        thanks
        Reddy

        Reply
        • Aditya

          June 30, 2017

          Reddy,
          Thanks for Chakram framework. It looks interesting and pretty slick to use. I would like to prepare a post on that.

          Reply

Leave a Reply