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
ScreenRecorder screenRecorder = new ScreenRecorder(gc, “AVI”, 24, ScreenRecorder.CursorEnum.BLACK,15,30,0);
there is no constructor available for the above line!!!
@Ambika, I have updated the example with latest JARs from Monte Media Library.
Hi i cant import screenRecorder library from that ScreenRecorder.jar after adding in to javaproject in eclipse with selenium,
Using: selenium Java 2.20.0, Win 7 32-bit, installed (Techsmith Screen Capture Codec).
and (‘import ch.randelshofer.screenrecorder.ScreenRecorder;’) whre this come from?
i import all below listed libraryes..
import org.openqa.selenium.*;
import org.monte.*;
import org.monte.screenrecorder.*;
please help me
Hi Chandra,
Somehow the sample code was broken, please copy the updated code and try once again
Best,
Unmesh
Hi Umesh,
i used the above updated code but still unable to import ” screenRecorder screenRecorder = new ScreenRecorder” ?? i also added the ScreenRecorder.jar to the project library..! if i continue to run throwing Error
((Exception in thread “main” java.lang.Error: Unresolved compilation problem:
screenRecorder cannot be resolved to a type
at test.VideoRecord.main(VideoRecord.java:48) ))
please suggest me …Umesh
Thank you for reply.
Hi Chandra,
The code as been fixed and tested with ScreenRecorder Version 0.6.6.
Best,
Unmesh
wow thats fantastic, but where can i find older version of ScreenRecorder Version 0.6.6.
i searched many sites but coudnt found ? can u pleast priovide any link…or mail to me?
or github link…
Thank you very much Unmesh …
In fact version 0.6.6 is released yesterday, you can get it from Monte’s Home Page.
Please, How can I specify location where to write created video file on HDD?
Thanks, Vlado.
Vlado,
You need to modify the source of Monte Media Library to store the output file at desired location.
Best,
Unmesh
Nice tutorial, Thanks
Hi,
I am able to record video for my test run on my desktop using screen recorder, but when i run my test on remote machine using selenium grid, it throws null pointer error.
Could you please advise, how can i use it for remote machine recording
regards
manish
Hi Manish,
ScreenRecorder does not work on remote machine. You can record video from remote machine if you’re using Python with Castro.
Best,
Unmesh
Thanks Unmesh.
I am using JAVA. May you advise any other tool or Castro equivalent for video recording in remote machine for JAVA.
regards
manish
Hi,
Please advise me on a video recording tool on a remote machine in selenium for JAVA, as i have to implement it in my testing process.
Regards
And using JUNIT, how it can be work? Can you help me to configure? I can’t put the screencast stop in tear down method.
Example:
package com.eliasnogueira.seleniumpageonjects.tests;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import com.eliasnogueira.seleniumpageonjects.pages.BookDetailPage;
import com.eliasnogueira.seleniumpageonjects.pages.IndexPage;
import com.eliasnogueira.seleniumpageonjects.pages.LoginPage;
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.*;
import java.io.IOException;
public class ExecutaTeste {
private WebDriver driver;
// 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
@Before
public void setUp() throws Exception {
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);
driver = new FirefoxDriver();
screenRecorder.start();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(“https://lojaexemplod.lojablindada.com/customer/account/login/”);
screenRecorder.stop();
}
@Test
public void executaLogin() throws IOException, AWTException {
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);
screenRecorder.start();
LoginPage login = new LoginPage(driver);
login.efetuaLogin(“elias.nogueira@live.com”, “selenium”);
Assert.assertEquals(login.verificaLogin(“Painel da Minha Conta”), true);
IndexPage index = new IndexPage(driver);
index.efetuaBusca(“Ajax”);
Assert.assertEquals(index.verificaLivro(“Ajax”), true);
index.clicaLivro(“Ajax”);
BookDetailPage detail = new BookDetailPage(driver);
detail.adicionarLivroAoCarrinho(“1”);
Assert.assertEquals(detail.verificaValorLivro(“R$444,50”), true);
index.logoff();
screenRecorder.stop();
}
@After
public void tearDown() throws Exception {
driver.close();
}
}
Hi,
Following should work for you
public class ExecutaTeste {
private WebDriver driver;
private ScreenRecorder screenRecorder;
@Before
public void setUp() 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 = 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);
driver = new FirefoxDriver();
screenRecorder.start();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get(“https://lojaexemplod.lojablindada.com/customer/account/login/”);
}
@Test
public void executaLogin() throws IOException, AWTException {
LoginPage login = new LoginPage(driver);
login.efetuaLogin(“elias.nogueira@live.com”, “selenium”);
Assert.assertEquals(login.verificaLogin(“Painel da Minha Conta”), true);
IndexPage index = new IndexPage(driver);
index.efetuaBusca(“Ajax”);
Assert.assertEquals(index.verificaLivro(“Ajax”), true);
index.clicaLivro(“Ajax”);
BookDetailPage detail = new BookDetailPage(driver);
detail.adicionarLivroAoCarrinho(“1?);
Assert.assertEquals(detail.verificaValorLivro(“R$444,50?), true);
index.logoff();
}
@After
public void tearDown() throws Exception {
driver.close();
screenRecorder.stop();
}
}
Hi Unmesh,
i have copied above code in my eclipse, i have downloaded screen recorder java from the website http://www.randelshofer.ch/monte/ (not sure about the version), i am getting below error please help me.
“The constructor ScreenRecorder(GraphicsConfiguration, Format, Format, Format, null) is undefined”
After launching sample code i get exception:
java.io.IOException: Unable to encode video frames in this output format:Format{mediaType:,quality:,frameRate:,keyFrameInterval:,compressorName:tscc,dimY:,mimeType:video/avi,encoding:tscc,dimZ:,dimX:}
at org.monte.screenrecorder.ScreenRecorder.createMovieWriter(ScreenRecorder.java:345)
at org.monte.screenrecorder.ScreenRecorder.start(ScreenRecorder.java:425)
Please help
Will it work with RemoteWebDriver? Can I use it on Mobile devices where I am using RemoteWebDriver? Mobile devices are connect to grid, FYI.
Thank you.
Hi Shah,
Monte Media Library can not record session on a RemoteWebDriver or Grid. You can use Castro from python for recording sessions on a remote machine or device which supports VNC.
Best,
Unmesh
Thank you Unmesh for your reply. I tried castro, but it looks like its too out dated and moreover I am looking forward to record session on mobile device. Let me know if you have any suggesions.
Thank you,
Shah
Hi unmesh,
Can you please provide the same code in c# as i want to make sure i am coding anything wrong. Also if you can mention what else i have to include in visual studio in order to run the automate test that would be helpful .
Thanks,
G
@G
You need to use Microsoft Expression Encoder SDK for capturing video in C#. Please visit http://www.microsoft.com/expression/products/encoder4_overview.aspx for more details and sample projects that you can use in Visual Studio.
Best,
Unmesh
Thanks Unmesh .
I managed to record video with Encoder SDK. Can you please throw some light on how we can add description in the video like what user is doing ?
Hi Umesh, i have tried your code in my simple project under it worked fine.
When i am trying to integrate the code with my project code base, it shows me error
The constructor ScreenRecorder(GraphicsConfiguration, Format, Format, Format, null) is undefined
Please help me.
Hi Unmesh,
Can you provide the Maven Dependency for Monte Screen Recorder.
Does any one know maven dependency for org.monte.media. I am really stuck coz of it.. Tried adding below but had no luck :
org.monte.screenrecorder
MonteScreenRecorder
1.0.0
org.monte
monte-screen-recorder
0.7.5
test
mvn install:install-file -Dfile=C:\QAAutomationInterface\lib\MonteScreenRecorder.jar -DgroupId=org.monte -DartifactId=monte-screen-recorder -Dversion=0.7.5 -Dpackaging=jar
Hi Unmesh,
We have many recording tools which will record the complete screen actions. So what is the specific use of this Monte Media ScreenRecorder class?.
Following program throwing a NPE while running by using the JUnit. Kindly look into this.
package prac1;
import static org.monte.media.FormatKeys.EncodingKey;
import static org.monte.media.FormatKeys.FrameRateKey;
import static org.monte.media.FormatKeys.KeyFrameIntervalKey;
import static org.monte.media.FormatKeys.MIME_AVI;
import static org.monte.media.FormatKeys.MediaTypeKey;
import static org.monte.media.FormatKeys.MimeTypeKey;
import static org.monte.media.VideoFormatKeys.CompressorNameKey;
import static org.monte.media.VideoFormatKeys.DepthKey;
import static org.monte.media.VideoFormatKeys.ENCODING_AVI_TECHSMITH_SCREEN_CAPTURE;
import static org.monte.media.VideoFormatKeys.QualityKey;
import java.awt.AWTException;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Test;
import org.junit.Before;
import org.monte.media.Format;
import org.monte.media.FormatKeys.MediaType;
import org.monte.media.math.Rational;
import org.monte.screenrecorder.ScreenRecorder;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Practices2Test {
ScreenRecorder screenRecorder;
private WebDriver driver;
@Before
public void setUp() 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);
File obj1 = new File(
“D:\\IE Drivers\\IEDriverServer_Win32_2.39.0\\IEDriverServer.exe”);
System.setProperty(“webdriver.ie.driver”, obj1.getAbsolutePath());
driver = new InternetExplorerDriver();
screenRecorder.start();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
}
@Test
public void login() throws IOException,AWTException
{
driver.get(“www.google.co.in”);
driver.findElement(By.name(“q”)).sendKeys(“Tavant”);
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.id(“gbqfb”)).click();
WebDriverWait obj = new WebDriverWait(driver,10);
obj.until(ExpectedConditions.titleContains(“Tavant”));
System.out.println(driver.getTitle());
}
@Test
public void test2()throws Exception
{
driver.get(“http://www.healthkart.com/”);
WebDriverWait obj = new WebDriverWait(driver,10);
WebElement obj2 = obj.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(“.//*[@id=’globalSearch’]/input”)));
System.out.println(obj2);
}
@After
public void done() throws Exception {
driver.close();
screenRecorder.stop();
}
}
Hi please also tell how to record audio in this as well.
Hi,
I have this library working nice on my local machine, but when the tests run on my CI engine, a virtual machine, the videos are created but they are totally black. Is there a way to fix this? Thanks
Dear All,
I would like to capture only application screen activities rather then system capture. In addition it should work in both local and remote server.
And also need to mention the video saving file path as required.
File file = new File(ReportXL.strResultsFolder+”/VideoCapture”);
GraphicsConfiguration gc = GraphicsEnvironment
.getLocalGraphicsEnvironment()
.getDefaultScreenDevice()
.getDefaultConfiguration();
// ScreenRecorder screenRecorder = new ScreenRecorder(gc,
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, file, “Video”);
Thank u
i am able to screen cast web browser by using your code,
but how we can screen cast the mobile automation ?
This worked great!!! Thank you!
hi there, first thanks a lot for your awesome blog 🙂
How can we save the file in the same folder that our java program is in?
Could you help me, how to use this, if I would like to record the sound with the screen recording?
Great – Saved my day. Thank you
Hi Unmesh
can you pls tell how can i save the recording? maybe i m overlooking something, but whr to define the folder path
Hi there, I have a question:
Is it possible save the video if the script fails before the screenRecorder.stop(); method?
Thanks in advance.
You can put your stop method in onTestFailure as well, hope that works.
Hi unmesh,
I have tried same thing in my framework but i was facing error while doing this
Framework was build on (Cucumber + Maveen + SpringBootApplication)
I hope issue with Spring boot because i was able to create sample Testng project and record the video but my framework build in such a way
Message: java.awt.HeadlessException
at sun.java2d.HeadlessGraphicsEnvironment.getDefaultScreenDevice(HeadlessGraphicsEnvironment.java:77)
can you please help me on this