In Java, we can extend Selenium WebDriver test scripts to record videos by using an open source tool named Monte Media Library.
In this blog we will look at how to configure and use the Monte Media Library’s ScreenRecorder class with Selenium, to record movies of tests.
ScreenRecoder supports the AVI and QuickTime formats for recording movies. The program provides multiple configurations for colors, mouse cursor, screen refresh rate, mouse rate, audio, and so on.
First step is to download the ScreenRecorder.jar file from the http://www.randelshofer.ch/monte/index.html site or search  the net for it.
Second step is to add the ScreenRecorder.jar file to the project’s build path.
Here is a sample code on how to integrate video recording to Selenium WebDriver test:

import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.By;
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 org.junit.*;

import java.awt.*;

public class videoRecording {

private WebDriver driver;
private ScreenRecorder screenRecorder;

@Before
public void setUp() throws Exception {
// Create an 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_QUICKTIME),
new Format(MediaTypeKey, MediaType.VIDEO, EncodingKey,ENCODING_QUICKTIME_JPEG, CompressorNameKey,
ENCODING_QUICKTIME_JPEG, 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
driver = new FirefoxDriver();
// Call the start method of ScreenRecorder to begin recording
screenRecorder.start();
}

@Test
public void testGoogleSearch() throws Exception {

driver.get("http://www.google.com");
WebElement element = driver.findElement(By.name("q"));
// Enter something to search for
element.sendKeys("Selenium");
element.submit();

Thread.sleep(2000);
Assert.assertTrue(driver.getTitle().contains("Selenium"));
}

@After
public void tearDown() throws Exception {
// Close the browser
driver.quit();
// Call the stop method of ScreenRecorder to end the recording
screenRecorder.stop();
}
}

During the video recording of desktop, the ScreenRecorder class needs the graphics configuration of the display screen on which tests are executed.
Java’s AWT package has a class named GraphicsConfiguration through which we can pass this information to the ScreenRecorder class.

The graphics configuration details from the GraphicsEnvironment class of AWT are collected in the GraphicsConfiguration gc.
This will get configuration of the default system display screen for an instance of GraphicsConfiguration.

The ScreenRecorder constructor is supplied with the following parameters:
1, GraphicsConfiguration (gc) – Provides display screen information such as size, resolution, and so on.
2, Video and compression format (video/avi, tscc) – The output format of the movie, encoding mechanism, color depth, frames per second, keyframe interval.
ScreenRecorder supports AVI and QuickTime movie formats.
(In our case we use QuickTime movie format).
3, Color of the mouse cursor and refresh rate (black, 30) – The color of the mouse cursor and refresh rate.
4, Audio format (null) – No audio will be recorded.

Video recording is invoked by calling the ScreenRecorder’s start() method and completed by calling the ScreenRecorder’s stop() method.
When a recording is finished, ScreenRecorder stores the output file in the user’s Home directory. On Windows, it will save the recorded file in the C:\Users\<username>\Videos folder.

Posted By István Lackó

    17 Responses to “Recording videos of tests in Java”

  1. a says:

    Have you ever thought about creating an e-book or guest authoring on other sites?

    I have a blog centered on the same subjects you discuss and would really like to have
    you share some stories/information. I know my viewers would appreciate your work.
    If you’re even remotely interested, feel free to shoot me an e-mail.

  2. Aldo says:

    I tried your code and it worked like a charm (thanks!). However, when I tried to run it on a Virtual Machine, the video was completely black. Do you have any idea why this happened? Any workaround?

    Thanks

    • Hi!
      It seems that this is some driver/codec issue. Be sure that every necessary file is installed in the virtual machine, or try to update them.

      • S says:

        I’ve had a similar problem , wanted to record the tests while they are run against Jenkins or even Virtual Machine.

        Any hints would be a great help as you how to make it work rather than a black screen.

  3. harender says:

    Hello sir,
    Can you please tell how to record audio with it also.

  4. Vishnu says:

    Dear Folks,

    How to specify the video file to store instead of storing in the c:// driver.

    • Hi,

      There is a constructor which can be used for your situation:
      ScreenRecorder(java.awt.GraphicsConfiguration cfg, java.awt.Rectangle captureArea, Format fileFormat, Format screenFormat, Format mouseFormat, Format audioFormat, java.io.File movieFolder)

      Check this URL for: (java.awt.GraphicsConfiguration, java.awt.Rectangle, org.monte.media.Format, org.monte.media.Format, org.monte.media.Format, org.monte.media.Format, java.io.File)

      According to javadoc, you have two additional parameters here:
      captureArea
      movieFolder

      Here is an example:

      ScreenRecorder screenRecorder = new ScreenRecorder(gc, 
      				new Rectangle(new Dimension(1920, 1080)),
      				new Format(MediaTypeKey, MediaType.FILE, MimeTypeKey, MIME_QUICKTIME),
      				new Format(MediaTypeKey, MediaType.VIDEO, EncodingKey,ENCODING_QUICKTIME_JPEG, CompressorNameKey,
      ENCODING_QUICKTIME_JPEG, DepthKey, (int) 24,FrameRateKey, Rational.valueOf(15), QualityKey, 1.0f,KeyFrameIntervalKey, (int) (15 * 60)),
      				new Format(MediaTypeKey, MediaType.VIDEO, EncodingKey, &quot;black&quot;, FrameRateKey,Rational.valueOf(30)),
      				null,
      				new File(&quot;&lt;Your file path&gt;&quot;));
  5. Rajashekar says:

    i am able to record the test by using your code.
    but how we can record the test for mobile automation ?

  6. Raju says:

    Hi,

    In my case, even browser also not opening and i’m not getting any errors also. Please provide your comments.

    • Hi!
      Thanks for your comment on our blog. Here is some suggestions in this situation:

      make sure that you have the latest Selenium framework (2.46.0) (http://www.seleniumhq.org/download/)
      if you have multiple instances of Java Virtual Machine, make sure that you are use the same for your eclipse project and for your Selenium Webdriver.
      if you are using Firefox to start your test, make sure that you have version 37.0.2 or higher
      if you are using Chrome to start your test, make sure that you have your ChromeDriver executable on your hard drive, and that you added it to path variable. (https://sites.google.com/a/chromium.org/chromedriver/downloads)

  7. Uday says:

    Hi,

    Thanks for the nice article.

    Is there a way to improve the quality of the video? What parameters/values i need to update in the above code?

    Another point is, is there a way i can provide the video file name rather than ScreenRecorder generated timpstamp name?

    Thanks,
    Uday

  8. Amar Sorathiya says:

    I am getting blank video while running script on Jenkins.

    Any help would be appreciated.

  9. Amar Sorathiya says:

    I am getting blank video while running script on Jenkins.
    Please help if anyone has any idea….

  10. Umesh says:

    Try below solution for Screen Recording during Mobile Automation (Appium)

    http://easytestautomation.blogspot.in/2016/06/easy-to-record-screen-in-appium-for.html

  11. vedant says:

    Thanks,this code is working but the file is recorded in .mov ,so i can not play the video.Suggest me so that i can play the recorded video,or can i record it in some different format.

Leave a Reply




XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>