Selenium Record Video

Profile picture for user arilio666

In this article, we will see how we can record our test performance in selenium using a simple tool called  monte screen recorder.

1.) Setup

<!-- https://mvnrepository.com/artifact/com.github.stephenc.monte/monte-screen-recorder -->
<dependency>
   <groupId>com.GitHub.Stephens.monte</groupId>
   <artifactId>monte-screen-recorder</artifactId>
   <version>0.7.7.0</version>
</dependency>
  • Paste this dependency in the POM.xml file.
  • This will install the necessary jars related to the screen recorder in our project.

2.) Utility code

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.monte.media.Format;
import org.monte.media.FormatKeys.MediaType;
import org.monte.media.Registry;
import org.monte.media.math.Rational;
import org.monte.screenrecorder.ScreenRecorder;
import static org.monte.media.AudioFormatKeys.*;
import static org.monte.media.VideoFormatKeys.*;
public class ScreenRecorderUtil extends ScreenRecorder {
public static ScreenRecorder screenRecorder;
public String name;
public ScreenRecorderUtil(GraphicsConfiguration cfg, Rectangle captureArea, Format fileFormat,
  Format screenFormat, Format mouseFormat, Format audioFormat, File movieFolder, String name)
    throws IOException, AWTException {
 super(cfg, captureArea, fileFormat, screenFormat, mouseFormat, audioFormat, movieFolder);
 this.name = name;
}
@Override
protected File createMovieFile(Format fileFormat) throws IOException {
 if (!movieFolder.exists()) {
  movieFolder.mkdirs();
 } else if (!movieFolder.isDirectory()) {
  throw new IOException("\"" + movieFolder + "\" is not a directory.");
 }
 SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss");
 return new File(movieFolder,
   name + "-" + dateFormat.format(new Date()) + "." + Registry.getInstance().getExtension(fileFormat));
}
public static void startRecord(String methodName) throws Exception {
 File file = new File("./test-recordings/");
 Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
 int width = screenSize.width;
 int height = screenSize.height;
 Rectangle captureSize = new Rectangle(0, 0, width, height);
 GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().
   getDefaultScreenDevice()
   .getDefaultConfiguration();
 screenRecorder = new ScreenRecorderUtil(GC, captureSize,
   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, 24, FrameRateKey,
     Rational.valueOf(15), QualityKey, 1.0f, KeyFrameIntervalKey, 15 * 60),
   new Format(MediaTypeKey, MediaType.VIDEO, EncodingKey, "black", FrameRateKey, Rational.valueOf(30)),
   null, file, methodName);
 screenRecorder.start();
}
public static void stopRecord() throws Exception {
 screenRecorder.stop();
}
}
  • Save this as ScreenRecorderUtil.java and save it into the package folder where the tests are located.

3.) Test

ScreenRecorderUtil.startRecord("main");
ScreenRecorderUtil.stopRecord();
  • Use the start record and pass in the method we run the test within it.
  • Make sure to stop the record at the end.
  • After running the test, refresh the project.
  • We can see here the video files in avi format are generated after a successful test run.