How to Record Selenium Test Execution Video using Monte Screen Recorder API


Normally we take screenshots and attach them to Reports(HTML Reports or extent reports)
to help you in debugging tests and identifying the cause of test failures.

However, in certain cases, a video showing exact screen states, screen transitions and input as generated by your tests might come in even more handy. Such a video could also help presenting / demonstrating your work to your peers and other stakeholders.

Monte Screen Recorder, a Java library that can assist you in creating videos of your Selenium tests.

In this post, I will show you how to create videos for your test cases.

Installation and configuration of the Monte Screen Recorder is easy.

Simply download the .jar file from here and add it as a dependency to your project.

Reference Link: http://www.randelshofer.ch/monte/

You can download JAR file from Here



Utility Class:

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();
}
}

Selenium Web Driver Test Case:


import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class TestCase {
WebDriver driver;

@BeforeClass
void setup() {
System.setProperty("webdriver.chrome.driver", "C:/Drivers/chromedriver_win32/chromedriver.exe");
driver = new ChromeDriver();

driver.get("http://demo.nopcommerce.com");

driver.manage().window().maximize();
}

@Test
void verifyLinks() throws Exception {
ScreenRecorderUtil.startRecord("CheckingLinks");
driver.findElement(By.xpath("/html/body/div[6]/div[2]/ul[1]/li[5]/a")).click(); // Books

// Computers
driver.findElement(By.xpath("/html/body/div[6]/div[3]/div[2]/div[1]/div[1]/div[2]/ul/li[1]/a")).click();
System.out.println(driver.getTitle());

// Electronics
driver.findElement(By.xpath("/html/body/div[6]/div[3]/div[2]/div[1]/div[1]/div[2]/ul/li[2]/a")).click();
System.out.println(driver.getTitle());

// Apparel
driver.findElement(By.xpath("/html/body/div[6]/div[3]/div[2]/div[1]/div[1]/div[2]/ul/li[3]/a")).click();
System.out.println(driver.getTitle());

// Digital Downloads
driver.findElement(By.xpath("/html/body/div[6]/div[3]/div[2]/div[1]/div[1]/div[2]/ul/li[4]/a")).click();
System.out.println(driver.getTitle());

// Jewelary
driver.findElement(By.xpath("/html/body/div[6]/div[3]/div[2]/div[1]/div[1]/div[2]/ul/li[6]/a")).click();
System.out.println(driver.getTitle());

// Giftcards
driver.findElement(By.xpath("/html/body/div[6]/div[3]/div[2]/div[1]/div[1]/div[2]/ul/li[7]/a")).click();
System.out.println(driver.getTitle());

ScreenRecorderUtil.stopRecord();
}

@AfterClass
void tearDown()
{
driver.close();
}

}

Followers