Selenium Record Video with JUnit in Java
In this example we show you how you can record a video of selenium test cases in java. This way you can watch how your selenium tests are performing. If you sun your selenium tests on a selenium grid like sauce labs or browser stack, they provide taking screenshots or making video’s. This tutorial focuses on recording video of selenium tests locally.
Maven Dependencies
<!-- junit testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<!-- selenium -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.44.0</version>
</dependency>
<!-- screen recorder -->
<dependency>
<groupId>com.pojosontheweb</groupId>
<artifactId>monte-repack</artifactId>
<version>1.0</version>
</dependency>
Selenium Record Video Example
package com.memorynotfound.test;
import org.junit.*;
import org.monte.screenrecorder.ScreenRecorder;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.monte.media.math.Rational;
import org.monte.media.Format;
import static org.monte.media.AudioFormatKeys.*;
import static org.monte.media.VideoFormatKeys.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.List;
public class ScreenRecorderTest {
private static WebDriver driver;
private static ScreenRecorder screenRecorder;
@BeforeClass
public static void setUp() throws IOException, AWTException {
//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);
// create driver
driver = new FirefoxDriver();
// maximize screen
driver.manage().window().maximize();
}
@Before
public void beforeTest() throws IOException {
screenRecorder.start();
}
@Test
public void testScreenRecorder() {
driver.get("https://memorynotfound.com/");
}
@After
public void afterTest() throws IOException {
screenRecorder.stop();
List<File> createdMovieFiles = screenRecorder.getCreatedMovieFiles();
for(File movie : createdMovieFiles){
System.out.println("New movie created: " + movie.getAbsolutePath());
}
}
@AfterClass
public static void cleanUp(){
if (driver != null) {
driver.close();
driver.quit();
}
}
}
Example output
New movie created: /var/folders/ts/tv2s4k357c79jmw4_rkbt7b40000gn/T/screenrecorder/ScreenRecording 2015-02-07 at 16.55.53.avi
Example Video
Here is an example of a recorded video: Selenium Record Video Example. It is a small video of 3 seconds that opens a web page.
Hi there, I have a question:
Is it possible save the video if the script fails before the screenRecorder.stop(); method?
Thanks in advance.
Dear Rodrigo, I think you need the
screenRecorder.stop();
method before you can save the video.