About these ads

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

About these ads

33 Comments on “Recording Screencast of Selenium Tests”

  1. ambika verma says:

    ScreenRecorder screenRecorder = new ScreenRecorder(gc, “AVI”, 24, ScreenRecorder.CursorEnum.BLACK,15,30,0);

    there is no constructor available for the above line!!!

  2. chandra says:

    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

    • unmesh says:

      Hi Chandra,

      Somehow the sample code was broken, please copy the updated code and try once again

      Best,
      Unmesh

      • chandra says:

        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.

    • unmesh says:

      Hi Chandra,

      The code as been fixed and tested with ScreenRecorder Version 0.6.6.

      Best,
      Unmesh

      • chandra says:

        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 …

      • unmesh says:

        In fact version 0.6.6 is released yesterday, you can get it from Monte’s Home Page.

  3. Vlado says:

    Please, How can I specify location where to write created video file on HDD?

    Thanks, Vlado.

  4. Manish says:

    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

    • unmesh says:

      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

      • Manish says:

        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

      • Manish says:

        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

  5. Rodrigues QA says:

    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();
    }
    }

    • unmesh says:

      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();
      }
      }

      • lakshmi says:

        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”

  6. Marcin says:

    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

  7. Shah says:

    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.

    • unmesh says:

      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

      • Shah says:

        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

  8. G says:

    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

  9. lakshmi says:

    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.

  10. Rahul Pratap Singh says:

    Hi Unmesh,
    Can you provide the Maven Dependency for Monte Screen Recorder.

  11. […] worked on a new tool for Selenium JUnit testing: video recording of test execution. There are some good articles on how to utilize Monte Media Library for Java to add recording to your code. However, I did not […]

  12. Ankita says:

    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

  13. Param says:

    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?.

  14. Param says:

    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();

    }

    }

  15. harender says:

    Hi please also tell how to record audio in this as well.

  16. cattox says:

    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


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 43 other followers