Tuesday, June 14, 2016

A Video Recorder for your Selenium FW using Monte

Screen recorder for web tests on webdriver by means of java.

Usage:
1) initialize web driver
     WebDriver driver = new FirefoxDriver();
     driver.get("http://automated-testing.info");
2) capture video
     initScreen(Path to save video);
     startVideoCapturing();
3) Stop Video recording
     stopVideoCapturing();
4) Quit web driver
     driver.quit();
Jars files to add to buildpath : monte-cc.jar, jai_core.jar, jmf.jar

Note: Monte java library is used to implement it http://www.randelshofer.ch/monte/


package common;
import org.monte.media.Format;
import org.monte.media.FormatKeys.*;
import org.monte.media.math.Rational;
import org.monte.screenrecorder.ScreenRecorder;
import java.awt.AWTException;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.io.File;
import java.io.IOException;
import static org.monte.media.FormatKeys.*;
import static org.monte.media.VideoFormatKeys.*;

public class VideoCapture {
    public static ScreenRecorder screenLogger = null;

    /**
     * initializes a graphic configurations
     */
    public static void initScreen(String pathToFile) {
// get the graphics configuration of the current screen
GraphicsConfiguration gc = GraphicsEnvironment
.getLocalGraphicsEnvironment().getDefaultScreenDevice()
.getDefaultConfiguration();

// set screen recorder configuration
try {
   screenLogger = new ScreenRecorder(gc, null, 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,
   24, FrameRateKey, Rational.valueOf(15), QualityKey,
   1.0f, KeyFrameIntervalKey, 15 * 60), new Format(
   MediaTypeKey, MediaType.VIDEO, EncodingKey,
   "black", FrameRateKey, Rational.valueOf(30)), null,
   new File(pathToFile));
} catch (IOException ioe) {
   ioe.printStackTrace();
} catch (AWTException awte) {
   awte.printStackTrace();
}
    }

    /**
     * starts the screen recorder
     */
    public static void startVideoCapturing() {
try {
   screenLogger.start();
} catch (IOException ioe) {
   ioe.printStackTrace();
}
    }

    /**
     * stops the screen recorder
     */
    public static void stopVideoCapturing() {
try {
   screenLogger.stop();
} catch (Exception e) {
   e.printStackTrace();
}
    }

}