Recording Screencast of Selenium Tests

I was looking for ways to record a video/screencast of Selenium Test Run in Java and came across this brilliant tool called Monte Media Library developed by Werner Randelshofer. This post describes using the ScreenRecorder class from Monte Media Library for recording screencast of Selenium Tests in Java.

Little about ScreenRecorder

ScreenRecoder supports “AVI” and “QuickTime” format for recording the video. For “AVI” format you need to install TSCC Codec (Techsmith Screen Capture Codec) while “QuickTime” format is supported by Apple’s QuickTime Player. ScreenRecorder provides multiple configurations for colors, mouse cursor, screen rate, mouse rate, audio etc. on GUI as well as programmatically.

You need to download ScreenRecorder.jar file from Monte’s Home Page. ScreenRecorder.jar can be launched as a standalone GUI for recording actions from Desktop window or you can add this to your project and import ScreenRecorder class for recording screen video programmatically.

Using ScreenRecorder Class

Following example is created in Eclipse and you need to add ScreenRecorder.jar to the build path of Project.

ScreenRecorder.jar contains ScreenRecorder class which can be called from a Selenium Script for recording the test session in following way:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.monte.media.math.Rational;
import org.monte.media.Format;
import org.monte.screenrecorder.ScreenRecorder;
import static org.monte.media.AudioFormatKeys.*;
import static org.monte.media.VideoFormatKeys.*;
import java.awt.*;

public class SeScreenCastDemo {
	
	public static void main(String[] args) throws Exception {

		//Create a instance of GraphicsConfiguration to get the Graphics configuration
		//of the Screen. This is needed for ScreenRecorder class.
		GraphicsConfiguration gc = GraphicsEnvironment//
		.getLocalGraphicsEnvironment()//
		.getDefaultScreenDevice()//
		.getDefaultConfiguration();

		//Create a instance of ScreenRecorder with the required configurations
		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);

	//Create a new instance of the Firefox driver
	WebDriver driver = new FirefoxDriver();

	//Call the start method of ScreenRecorder to begin recording
	screenRecorder.start();

	//And now use this to visit Google
	driver.get("http://www.google.com");

	//Find the text input element by its name
	WebElement element = driver.findElement(By.name("q"));

	//Enter something to search for
	element.sendKeys("Cheese!");

	//Now submit the form. WebDriver will find the form for us from the element
	element.submit();

	//Check the title of the page
	System.out.println("Page title is: " + driver.getTitle());

	//Google's search is rendered dynamically with JavaScript.
	//Wait for the page to load, timeout after 10 seconds
	(new WebDriverWait(driver, 10)).until(new ExpectedCondition<Boolean>() {
	public Boolean apply(WebDriver d) {
	return d.getTitle().toLowerCase().startsWith("cheese!");
	}});

	//Should see: "cheese! - Google Search"
	System.out.println("Page title is: " + driver.getTitle());

	//Close the browser
	driver.quit();

	//Call the stop method of ScreenRecorder to end the recording
	screenRecorder.stop();
	}
}

The ScreenRecorder class will create recordings in the home folder i.e. “Videos” on Windows, and “Movies” on Mac OS X by default. The ScreenRecorder class needs first argument as GraphicsConfiguration. You can get this by accessing GraphicsEnvironment member of AWT class by following way:

GraphicsConfiguration gc = GraphicsEnvironment//
 .getLocalGraphicsEnvironment()//
 .getDefaultScreenDevice()//
 .getDefaultConfiguration();

The ScreenRecorder captures screen interactions like a charm which can be very useful for analysing Selenium Tests.

Update:

Same can be achieved with Selenium .NET Binding (in C#/VB.NET) by using the Microsoft Expression Encoder 4 SDK. You can download the Encoder SDK from http://www.microsoft.com/expression/products/encoder4_overview.aspx. Use the ScreenCaptureJob class for recording video of Selenium Tests in C#/VB.NET.

Update:
This code has been tested on ScreenRecorder Version 0.6.6