如何在 Selenium webdriver 和 Java API 中录制视频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/48546783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 10:06:19  来源:igfitidea点击:

How to record a video in Selenium webdriver and Java API

javaseleniumselenium-webdriverwebdrivervideo-recording

提问by sgrillon

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)

我有每天晚上运行的自动化脚本(大约 50 个脚本或测试用例)。我想录制每个测试用例的视频(Selenium Java API + Cucumber)。有什么工具或方法可以用来分别控制每个测试用例的视频录制?我想在测试用例设置期间开始录制,在拆卸期间停止录制并使用指定的名称和日期在本地保存视频。所以我应该为每个测试用例准备 50 个视频(最好只保存失败的测试用例的视频)

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

有没有办法将此功能集成到我用于设置和拆卸的代码中?

采纳答案by sgrillon

I find a solution herebut 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:

SpecializedScreenRecorder 类扩展了 ScreenRecorder 类:

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:

Maven 导入:

    <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:

注意:如果您在没有 GUI 的 unix 上使用它,则会出现以下错误:

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

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

我寻找XvfbJenkins 和 Travis-ci 的插件,并重新编辑了这篇文章。

EDIT 1:

编辑 1:

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

如果你在没有 GUI 的 unix 上使用它,你可以使用 Jenkins +Xvfb插件。

回答by Oleksii

You could take a look at this libraryby Sergey Pirogov.

你可以看看Sergey Pirogov 的这个图书馆

回答by someusersomewhere

You could try to use either Selenium-Grid-Extrasor Zalenium.
Both are built on top of webdriver and do the recording automatically.

您可以尝试使用Selenium-Grid-ExtrasZalenium
两者都建立在 webdriver 之上,并自动进行录音。