Automation: Selenium Script to record Automation Test and Execution Video


Here we go, Selenium Screen Recorder: How To Record Selenium Test Execution Video

Techsouljours.blogspot.com - a blog for QA
Video Recording using selenium script


By using  Monte Screen Recorder API we record of our test cases as video format.

Monte Screen Recorder API available as Maven Dependencies where we can add in our POM.xml file as dependency tag.

How to Implement in our project :

We have one Java File of Recording screen functionality written and have attached as java file name as 'MyScreenRecorder.java' file

This 'MyScreenRecorder.java' file is the base function and we can use the method as 'MyScreenRecorder.startRecording("yourtestcase_recording")' in our own test case file.

For Example : Your Java file has the navigation function and if you want to record some scenarios, then we can use the method as 'MyscreenRecorder.startRecording' to start record and 'MyScreenRecorder.stopRecording' to stop the record.

@Test
public void navigation() throws Exception{

MyScreenRecorder.startRecording("Navigation_recording");

/*
                       your code goes here
                */
               MyScreenRecorder.stopRecording();

}

That's simple, your test case got recorded and saved in separate folder.



Results:

Here I have attached the screen recorder java file and ran for navigation from google to contus and from amazon to flipkart.


MyScreenRecorder.java

import java.awt.AWTException;
import java.awt.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

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

import static org.monte.media.AudioFormatKeys.*;
import static org.monte.media.VideoFormatKeys.*;

public class MyScreenRecorder extends ScreenRecorder {
public static ScreenRecorder screenRecorder;
public String name;

public MyScreenRecorder(GraphicsConfiguration cfg, Rectangle captureArea, Format fileFormat,Format screenFormat, Format mouseFormat, Format audioFormat, File movieFolder, String name) throws IOException, AWTException
{
super(cfg, captureArea, fileFormat, screenFormat, mouseFormat, audioFormat,movieFolder);
this.name = name;

}

@Override
protected File createMovieFile(Format fileFormat) throws IOException {

if (!movieFolder.exists()) {
movieFolder.mkdirs();
} else if (!movieFolder.isDirectory()) {
throw new IOException("\"" + movieFolder + "\" is not a directory.");
}
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss");
return new File(movieFolder,
name + "-" + dateFormat.format(new Date()) + "." + Registry.getInstance().getExtension(fileFormat));

}

public static void startRecording(String methodName) throws Exception {
File file = new File("./recordings/");

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
int width = screenSize.width;
int height = screenSize.height;

Rectangle captureSize = new Rectangle(0, 0, width, height);

GraphicsConfiguration gc = GraphicsEnvironment.getLocalGraphicsEnvironment().
getDefaultScreenDevice()
.getDefaultConfiguration();

screenRecorder = new MyScreenRecorder(gc, captureSize,

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, file, methodName);

screenRecorder.start();

}

public static void stopRecording() throws Exception {
screenRecorder.stop();
}

}


MyTestDemo

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

public class MyTestDemo {

WebDriver driver;

@BeforeMethod
public void setup(){
System.setProperty("webdriver.chrome.driver", "E:\\Automation- setup files\\drivers\\chromedriver.exe");
driver = new ChromeDriver();
}

@Test
public void navigationTest() throws Exception{
MyScreenRecorder.startRecording("NavigationTest_from_google_to_contus_viceversa");

driver.get("http://www.google.com");
driver.navigate().to("http://www.contus.com");
driver.navigate().back();
driver.navigate().forward();
driver.navigate().back();
driver.navigate().forward();
driver.navigate().back();
driver.navigate().forward();
driver.navigate().back();
driver.navigate().forward();

MyScreenRecorder.stopRecording();
}

@Test
public void navigation_FeatureTest() throws Exception{
MyScreenRecorder.startRecording("NavigationTest_From_amazon_to_flipkart_viceversa");

driver.get("http://www.amazon.com");
driver.navigate().to("http://www.flipkart.com");
driver.navigate().back();
driver.navigate().forward();
driver.navigate().back();
driver.navigate().forward();
driver.navigate().back();
driver.navigate().forward();
driver.navigate().back();
driver.navigate().forward();

MyScreenRecorder.stopRecording();
}


@AfterMethod
public void tearDown(){
driver.quit();
}

Test Execution got recorded and video got saved. 


Video Recording in selenium script1


                                         
Video Recording in selenium script2

Full Script from the scrap and want to import in your project. 

Here is the Github URL 

By using this feature we can create AVI/Mp4 recording video files for your test case execution, which can be shared to developers while logging the bugs.

Post author by Siddarth

10 comments:

  1. Pretty article! I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing. Need to learn
    Security Testing Services
    Test Automation Services
    Software Testing Services
    Compatibility Testing Services
    Regression Testing Services

    ReplyDelete
  2. I have learned so much from your post. I would definitely bookmark your site to be updated with your upcoming articles. Great job! So much information.
    DevOps Course in Hyderabad
    DevOps Training institute in Hyderabad
    DevOps Online Training insitute

    ReplyDelete
  3. This is very interesting, but it is necessary to click on this link: record web session

    ReplyDelete
  4. There is so much in this article that I would never have thought of on my own. Your content gives readers things to think about in an interesting way. https://www.pageview.com/

    ReplyDelete
  5. Como faço para gerar video em .mp4?

    ReplyDelete
  6. We're offering additional discount on early enrollment on full module Automation Training. Get Job Support with Unlimited Placement Opportunities.
    Call us at +91-9953489987, 9711287737
    For more details Visit www.diac.co.in

    ReplyDelete
  7. This really is my first time i visit here. I discovered so many entertaining stuff in your blog, especially its discussion. From a great deal of comments in your articles, I guess I am not alone having all of the leisure here! Maintain the superb work. It is very useful who is looking for top software testing companies

    ReplyDelete