Thursday, 17 March 2016

Capturing video of a Running Test In Selenium-Java


Agenda of this blog: To discuss about how to capture the video of a running automation (here Selenium as an example) test-script using Java.

Why it is required:


While coming to running automation test script it is very useful to capture the video of it, because sometimes you have many test scripts (some times more than 1000 test scripts),there its sometimes very tedious to continuously monitor running scripts(which is getting failed) to get the failure reason for the same and merging these video in reports will provide better reporting.
So in that case video recording is very useful as it will show the full execution of the same and from there you can easily identify the place of failure for test-script where and reason for the same.
How to achieve the video recording feature:
We need to follow below approach to achieve the same:

1.  We need to download a jar called monte-screenrecorder and use the same in the project build path .
      2. Write a class which will implement the logic for the recording(Find the below code snippet for the same)and use it in the Test classes.

package recordingVideo;

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.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.io.File;
import java.io.IOException;

import org.monte.media.Format;
import org.monte.media.FormatKeys.MediaType;
import org.monte.media.math.Rational;
import org.monte.screenrecorder.ScreenRecorder;



public class Record {

                private static ScreenRecorder screenRecorder;
                /**
                 * Method to start the video capturing using monte screen recorder.
                 */
                public static void startVideoCapture()
                {
                                try
                                {
                                                                GraphicsDevice gd = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
                                                                int width = gd.getDisplayMode().getWidth();
                                                                int height = gd.getDisplayMode().getHeight();
                                                                java.awt.Rectangle captureArea = new java.awt.Rectangle(width,height);
                                                                GraphicsConfiguration gc = GraphicsEnvironment
                                            .getLocalGraphicsEnvironment()
                                            .getDefaultScreenDevice()
                                            .getDefaultConfiguration();
                                                                File mediaFolder = new File("E:\\tESTvIDEO");     
                                                                screenRecorder = new ScreenRecorder(gc,captureArea,
                                                                       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,mediaFolder);
               
                                                                screenRecorder.start();
                                               
                                }
                               
                                catch(Exception ex){
                                ex.printStackTrace();
                                }
                               
                }             
               




                /**
                 * Method to stop the video recording.
                 */
                public static void stopVideoCapture()
                {
                                try
                                {
                                               
                                                {
                                                                screenRecorder.stop();
                                               
                                                }
                                }
               
                                catch(IOException e)
                                {
                                                e.printStackTrace();
                                }
                                catch (Exception e) {
                                                e.printStackTrace();
                                }
                }
}
  

We need to call to methods, One for starting the recording and other for stopping the recording,we can call these methods in the @BeforeMethod and @AfterMethod(TestNg annotations) respectively.
Find the below code snippet for the Demo test Class which will record the execution of opening chrome browser and navigating to google.com.

package recordingVideo;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class Demo {

                Record record=new Record();
                WebDriver driver;
                @BeforeMethod
                public void startRecording(){
                                System.setProperty("webdriver.chrome.driver", "..\\AdvanceConcepts\\src\\Drivers\\chromedriver.exe");
                                driver=new ChromeDriver();
                                record.startVideoCapture();
                }
               
                @AfterMethod
                public void stopRecording(){
                                record.stopVideoCapture();
                                driver.close();
                }
               
               
                @Test
                public void openGoogleTest(){
                                driver.get("https://www.google.co.in/?gfe_rd=cr&ei=FprqVs3OG-3I8Ae587ioAQ");
                               
                               
                }
}




Visit for More Automation Related Discussion:




https://www.youtube.com/channel/UCKSk4gkmO3LXcW17hFUkmcQ/videos






3 comments:

  1. Pretty significant blogs!!! I should read for a lot of knowledge for this sites.Experiencing pleasant knowledge.Thanks for sharing this helpful information to our idea. You have supported a trustworthy blog keep sharing.If you want to apply converted studying for Hadoop Real-Time Training and Live High-Level Knowledge instructor Trained from Hadoop Training Programming Course to reach us
    Hadoop Training in Chennai
    Big Data Hadoop Training Center in Velachery

    ReplyDelete
  2. Existing without the answers to the difficulties you’ve sorted out through this guide is a critical case, as well as the kind which could have badly affected my entire career if I had not discovered your website.
    Best PHP Training Institute in Chennai|PHP Course in chennai

    Best .Net Training Institute in Chennai
    Big Data Hadoop Training in Chennai
    Linux Training in Chennai
    Cloud Computing Training in Chennai

    ReplyDelete