How to Record Selenium Test Execution Video with TestNG & Java?
Tags: java, selenium, testing, automation
Published: 2023-06-16 06:31:10

If you are working on a Selenium test automation project, it is essential to have a record of your test execution to review and analyze the results. Recording a video of your Selenium test execution can help you identify any issues or errors that occur during the test run. In this article, we will discuss how to record Selenium test execution video with Java.

I personally prefer **monte screen recorder** library! It works pretty well and thats what we are gonna learn today! Let’s start!


Step 1: **Add monte-screen-recorder dependency in your project**

If you are using maven, You can add following dependency to your project’s pom.xml file. This will download the required packages for screen recording and you can use them in your Selenium test automation project.

   

```
 <dependency>
    <groupId>org.monte.media</groupId>
    <artifactId>monte-screen-recorder</artifactId>
    <version>0.7.7.1</version>
    </dependency>
```

Step 2: **Import Required Packages**

To start with, we need to import the required packages for screen recording. Here are the packages that we need in the Java Class:

   

```
 import org.monte.media.math.Rational;
    import org.monte.screenrecorder.ScreenRecorder;
    import org.monte.media.Format;
    import org.monte.media.FormatKeys.MediaType;
    import org.monte.media.VideoFormatKeys;
    import org.monte.media.math.Rational;
```

Step 3: **Create a Method to Start the Screen Recording**

In this step, we will create a method that will start the screen recording. Here is the code for this method:

   

```
 public static ScreenRecorder startRecording(String fileName) throws Exception {
    GraphicsConfiguration gc = GraphicsEnvironment
    .getLocalGraphicsEnvironment()
    .getDefaultScreenDevice()
    .getDefaultConfiguration();

    ScreenRecorder screenRecorder = new ScreenRecorder(gc,
    new Format(MediaTypeKey, MediaType.FILE, MimeTypeKey, MIME_AVI),
    new Format(MediaTypeKey, MediaType.VIDEO, EncodingKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE,
    CompressorNameKey, ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE, DepthKey, (int) 24, FrameRateKey,
    Rational.valueOf(15), QualityKey, 1.0f, KeyFrameIntervalKey, (int) (15 * 60)),
    new Format(MediaTypeKey, MediaType.VIDEO, EncodingKey, “black”, FrameRateKey, Rational.valueOf(30)),
    null, new File(fileName));

    screenRecorder.start();

    return screenRecorder;
    }

```
In this method, we are creating a new instance of the ScreenRecorder class and setting up the required configurations for screen recording.


Step 4: **Create a Method to Stop the Screen Recording**

In this step, we will create a method that will stop the screen recording. Here is the code for this method:

    

```
public static void stopRecording(ScreenRecorder screenRecorder) throws Exception {
    screenRecorder.stop();
    }
```

In this method, we are simply calling the stop() method of the ScreenRecorder class to stop the screen recording.
Step 5: Start and Stop Screen Recording in Selenium Test

In this step, we will start and stop the screen recording in our Selenium test. Here is the code for this:

   

```
 @Test
    public void testRecording() throws Exception {
    // Start screen recording
    ScreenRecorder screenRecorder = startRecording(“testRecording.avi”);

    // Your Selenium test code goes here

    // Stop screen recording
    stopRecording(screenRecorder);
    }
```

In this code, we are starting the screen recording before our Selenium test code and stopping it after the test execution is completed.


Conclusion: In this article, we have discussed how to record Selenium test execution video with Java. By following the above steps, you can easily record a video of your Selenium test execution and analyze the results. This can help you identify any issues or errors that occur during the test run and improve the overall quality of your test automation project.

Don’t forget to Follow me on [LinkedIn](https://www.linkedin.com/in/bitwisebro) and [Github](https://github.com/bitwisebro)!!