Recording Screencast of TestComplete Test Runs

In this post we’ll explore the possibility of recording a screencast of TestComplete test runs, the other test tool I use extensively at work.

TestComplete does not have features to record screencast of test execution. However we can use Microsoft Expression Encoder 4 SDK to record videos programmatically from test scripts.

A recoded screencast of test run can help you in investigation of unattended execution or even demo the software or create training videos.

Microsoft Expression Encoder 4 has basic recording features with limited codec support. You can download it free from http://www.microsoft.com/expression/products/encoder4_overview.aspx

While the commercial version Microsoft Expression Encoder 4 Pro provides number of additional features and codecs.

I will use the CLR Bridge & dotNET object available in TestComplete to call the Encoder SDK functions to create the screencast.

Using CLR Bridge in TestComplete we can use .NET assemblies in test scripts and extend the test script code to use features from standard .NET classes as well as custom libraries.

Setting up Encoder SDK in CLR Bridge

Let’s add the Encoder SDK library to the CLR Bridge in TestComplete Project Settings by following steps:

1. Go to TestComplete Project Setting by selecting Current Project Settings from Tools Menu
2. Select CLR Bridge option on left side of Project Properties Pane
3. Click Browse Files button to select the Encoder SDK DLL file. This will display Select Assembly dialog box
4. On Select Assembly dialog navigate to C:\Program Files\Microsoft Expression\Encoder 4\SDK folder and select the Microsoft.Expression.Encoder.dll file

This will add Encoder library to the TestComplete Project and provide access to the internal classes and functions using the dotNET object.

Using ScreenCaptureJob Class in TestComplete Script

Let’s create a instance of ScreenCaptureJob class from the Encoder SDK and call methods to start & stop video recording:

Sub NotePadEditCutTest()
	Dim wndNotepad, objScreenCaptureJob

	'Create a new ScreenCapture Job using it's Constructor
	Set objScreenCaptureJob = dotNET.Microsoft_Expression_Encoder_ScreenCapture.ScreenCaptureJob.zctor

	'Specify the path & name of the Output File
	objScreenCaptureJob.OutputScreenCaptureFileName = "C:\Data\NotePadEditCutTest.wmv"

	'Start the Video Recording using ScreenCaptureJob's Start Method
	Call objScreenCaptureJob.Start()

	'Test Code
	Call TestedApps.notepad.Run(1, True)
	Set wndNotepad = Aliases.notepad.wndNotepad
	Call wndNotepad.Edit.Keys("SOme Text")
	Call wndNotepad.MainMenu.Click("Edit|Select All")
	Call wndNotepad.MainMenu.Click("Edit|Cut")
	Call aqObject.CheckProperty(Aliases.notepad.wndNotepad.Edit, "wText", cmpEqual, "")

	'Stop the Video Recording using ScreenCaptureJob's Stop Method
	Call objScreenCaptureJob.Stop()
End Sub

The output screencast will be created in wmv format. This screencast can be edited in Expression Encoder UI.


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