25
Oct

Video recording Selenium tests

Maven Dependency:

<dependencies>
    <!-- junit testing -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.11</version>
    </dependency>
    <!-- selenium -->
    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-java</artifactId>
        <version>2.44.0</version>
    </dependency>
    <!-- screen recorder -->
    <dependency>
        <groupId>com.pojosontheweb</groupId>
        <artifactId>monte-repack</artifactId>
        <version>1.0</version>
    </dependency>
</dependencies>

Junit program


import org.junit.*;
import org.monte.screenrecorder.ScreenRecorder;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.monte.media.math.Rational;
import org.monte.media.Format;
import static org.monte.media.AudioFormatKeys.*;
import static org.monte.media.VideoFormatKeys.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.List;

public class ScreenRecorderTest {

	private static WebDriver driver;
	private static ScreenRecorder screenRecorder;

	@BeforeClass
	public static void setUp() throws IOException, AWTException {

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

		// create driver
		System.setProperty("webdriver.chrome.driver", "C:\\Browserdrivers_Selenium\\chromedriver.exe");

		driver = new ChromeDriver();

		// maximize screen
		driver.manage().window().maximize();
	}

	@Before
	public void beforeTest() throws IOException {
		screenRecorder.start();
	}

	@Test
	public void testScreenRecorder() {
		driver.get("https://memorynotfound.com/");
	}

	@After
	public void afterTest() throws IOException {
		screenRecorder.stop();
		List<File> createdMovieFiles = screenRecorder.getCreatedMovieFiles();
		for (File movie : createdMovieFiles) {
			System.out.println("New movie created: " + movie.getAbsolutePath());
		}
	}

	@AfterClass
	public static void cleanUp() {
		if (driver != null) {
			driver.close();
			driver.quit();
		}
	}

}

Output:

New movie created: C:\Users\srasz\AppData\Local\Temp\2\screenrecorder\ScreenRecording 2019-10-25 at 11.13.58.avi