Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to record a video in Selenium webdriver and Java API [duplicate]

I have automated scripts that run every night (around 50 scripts or testcases). I want to record a video of each test case (Selenium Java API + Cucumber). Is there any tool or way that i can use to control video recording for each test case separately? I want during a test case setup start recording and during teardown stop recording and save the video locally with a specified name and date. So I should have 50 videos for each testcase (more preferably save only videos for the failed test cases)

Is there a way of integrating this functionality in the code I use for my setups and teardowns?

like image 634
Stéphane GRILLON Avatar asked Jan 31 '18 16:01

Stéphane GRILLON


People also ask

How can the WebDriver imitate the double click?

We can perform double click on elements in Selenium with the help of Actions class. In order to perform the double click action we will use moveToElement() method, then use doubleClick() method. Finally use build(). perform() to execute all the steps.

Is recording supported by Selenium WebDriver?

Selenium IDE is the ultimate tool for using Record and Playback.

Does Selenium WebDriver support record and playback facility?

Hi Gurminder, Selenium Webdriver doesn't support Record and Playback Facility. Though you can achieve the same by using Selenium IDE and then you can export the recorded tests in Webdriver compatible format as per your preferred language.


1 Answers

I find a solution here but if you find better, you can post an other response:

org.monte.screenrecorder.ScreenRecorder  screenRecorder = new ScreenRecorder...

Full main code:

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.Dimension;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.awt.Rectangle;
import java.awt.Toolkit;
import java.io.File;

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.chrome.ChromeDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

public class VideoReord {

    public static final String USER_DIR = "user.dir";
    public static final String DOWNLOADED_FILES_FOLDER = "downloadFiles";

    private ScreenRecorder screenRecorder;

    public static void main(String[] args) throws Exception {

        VideoReord videoReord = new VideoReord();
        videoReord.startRecording();

        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        WebDriver driver = new ChromeDriver(capabilities);
        driver.get("http://www.google.com");

        WebElement element = driver.findElement(By.name("q"));
        element.sendKeys("BreizhCamp 2018");
        element.submit();
        System.out.println("Page title is: " + driver.getTitle());
        driver.quit();
        videoReord.stopRecording();
    }

    public void startRecording() throws Exception {
        File file = new File(System.getProperty(USER_DIR) + File.separator + DOWNLOADED_FILES_FOLDER);

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

        this.screenRecorder = new SpecializedScreenRecorder(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, "MyVideo");
        this.screenRecorder.start();

    }

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

SpecializedScreenRecorder class extends ScreenRecorder class:

import java.awt.AWTException;
import java.awt.GraphicsConfiguration;
import java.awt.Rectangle;
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.Registry;
import org.monte.screenrecorder.ScreenRecorder;

public class SpecializedScreenRecorder extends ScreenRecorder {

    private String name;

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

Maven import:

    <dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-server</artifactId>
        <version>3.5.3</version>
        <exclusions>
            <exclusion>
                <groupId>com.codeborne</groupId>
                <artifactId>phantomjsdriver</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.google.guava</groupId>
                <artifactId>guava</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <!-- screen-recorder-->
    <dependency>
        <groupId>org.monte</groupId>
        <artifactId>screen-recorder</artifactId>
        <version>0.7.7</version>
    </dependency>

    <!-- guava -->
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>${guava.version}</version>
    </dependency>

CAUTION: If you use this on unix without GUI, you have this error:

No X11 DISPLAY variable was set, but this program performed an operation which requires it.

I look Xvfb plugin for Jenkins and Travis-ci and I re-edit this post.

EDIT 1:

If you use this on unix without GUI, you can use Jenkins + Xvfb plugin.

like image 71
Stéphane GRILLON Avatar answered Nov 10 '22 01:11

Stéphane GRILLON