Java整合Selenium录制视频

 

捕捉视频
有时候我们未必能够分析故障只需用日志文件或截图的帮助。有时捕获完整的执行视频帮助。让我们了解如何捕捉视频。

我们将利用Monte媒体库的执行相同。

配置

第1步:导航到URL下载屏幕记录JAR,如下图所示。
http://www.randelshofer.ch/monte/index.html

第2步:下载后,添加JAR文件添加到当前项目的库。

第3步:我们会利用Java的AWT包来初始化显卡配置。

GraphicsConfiguration gc = GraphicsEnvironment
  .getLocalGraphicsEnvironment()
  .getDefaultScreenDevice()
  .getDefaultConfiguration();
  
第4步:它采用下列参数创建ScreenRecorder的一个实例。


 
示例

我们将捕获简单的测试执行视频 - 百分比计算。

package com.demo.test;

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.AWTException;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.io.File;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.apache.commons.io.FileUtils;
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.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

public class webdriverdemo {
    private static ScreenRecorder screenRecorder;

    public static void main(String[] args) throws IOException, AWTException {
        GraphicsConfiguration gconfig = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();
        screenRecorder = new ScreenRecorder(gconfig, 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);

        WebDriver driver = new ChromeDriver();

        // 开始捕获视频
        screenRecorder.start();

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        driver.navigate().to("https://www.baidu.com/");

        driver.manage().window().maximize();

        for (int i = 0; i < 3; i++) {
            driver.findElement(By.id("kw")).sendKeys("selenium", Keys.ENTER);
            driver.navigate().forward();
            driver.navigate().back();
            try {
                Thread.sleep(3000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);

        FileUtils.copyFile(screenshot, new File("D:screenshotsscreenshots1.jpg"));

        // 停止捕获视频
        screenRecorder.stop();

    }
}

输出

录制的视频保存在“C:users<<UserName>>Videos”文件夹,如下图所示。

 自定义视频位置代码

package webui;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.AWTException;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsEnvironment;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.List;
import java.util.concurrent.TimeUnit;import org.apache.commons.io.FileUtils;
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.Keys;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;public class webdriverdemo {private static ScreenRecorder screenRecorder;//ScreenRecorder 官方地址,可以下载源码//http://www.randelshofer.ch/monte/index.htmlpublic static void main(String[] args) throws IOException, AWTException {GraphicsConfiguration gconfig = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration();screenRecorder = new ScreenRecorder(gconfig, 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);WebDriver driver = new ChromeDriver();// 开始捕获视频screenRecorder.start();driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);driver.navigate().to("https://www.baidu.com/");driver.manage().window().maximize();for (int i = 0; i < 3; i++) {driver.findElement(By.id("kw")).sendKeys("selenium", Keys.ENTER);driver.navigate().forward();driver.navigate().back();try {Thread.sleep(3000);} catch (InterruptedException e) {// TODO Auto-generated catch blocke.printStackTrace();}}File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);FileUtils.copyFile(screenshot, new File("D:/screenshotsscreenshots1.jpg"));System.out.println(screenRecorder.getStartTime());String vedioPath = "";//默认录制的视频保存在“C:users<<UserName>>Videos”文件夹,如下图所示。//获取视频所在位置,这里只有一个视频List<File> ll= screenRecorder.getCreatedMovieFiles();for (int i = 0; i < ll.size(); i++) {System.out.println(ll.get(i).getAbsolutePath());vedioPath = ll.get(i).getAbsolutePath();}// 停止捕获视频screenRecorder.stop();//复制视频到固定位置FileUtils.copyFile(new File(vedioPath), new File("D:/111.mp4"));System.out.println("Finish!!!!!");}public static void copyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException {if (srcFile.isDirectory()) {throw new IOException("Source '" + srcFile + "' exists but is a directory");} else if (srcFile.getCanonicalPath().equals(destFile.getCanonicalPath())) {throw new IOException("Source '" + srcFile + "' and destination '" + destFile + "' are the same");} else {File parentFile = destFile.getParentFile();if (parentFile != null && !parentFile.mkdirs() && !parentFile.isDirectory()) {throw new IOException("Destination '" + parentFile + "' directory cannot be created");} else if (destFile.exists() && !destFile.canWrite()) {throw new IOException("Destination '" + destFile + "' exists but is read-only");} else {doCopyFile(srcFile, destFile, preserveFileDate);}}}private static void doCopyFile(File srcFile, File destFile, boolean preserveFileDate) throws IOException {if (destFile.exists() && destFile.isDirectory()) {throw new IOException("Destination '" + destFile + "' exists but is a directory");} else {Path srcPath = srcFile.toPath();Path destPath = destFile.toPath();long newLastModifed = preserveFileDate ? srcFile.lastModified() : destFile.lastModified();Files.copy(srcPath, destPath, StandardCopyOption.REPLACE_EXISTING);checkEqualSizes(srcFile, destFile, Files.size(srcPath), Files.size(destPath));checkEqualSizes(srcFile, destFile, srcFile.length(), destFile.length());destFile.setLastModified(newLastModifed);}}private static void checkEqualSizes(File srcFile, File destFile, long srcLen, long dstLen) throws IOException {if (srcLen != dstLen) {throw new IOException("Failed to copy full contents from '" + srcFile + "' to '" + destFile + "' Expected length: " + srcLen + " Actual: " + dstLen);}}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/30630.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

linux环形缓冲区kfifo实践3:IO多路复用poll和select

基础知识 poll和select方法在Linux用户空间的API接口函数定义如下。 int poll(struct pollfd *fds, nfds_t nfds, int timeout); poll()函数的第一个参数fds是要监听的文件描述符集合&#xff0c;类型为指向struct pollfd的指针。struct pollfd数据结构定义如下。 struct poll…

django 优化方式

前言 对于网站和Web APP来说&#xff0c;相同的类型的产品&#xff0c;响应速度越好&#xff0c;那么用户量就越高。不可否认的是&#xff0c;响应速度是用户黏粘性最好的方式之一&#xff0c;但往往不知道如何下手解决&#xff0c;希望这篇文章可以给予你一些思路 对于网站和…

LC-杨辉三角-记忆版

LC-杨辉三角-记忆版 上一篇&#xff1a;LC-杨辉三角 上一篇讲了杨辉三角的算法&#xff0c;不过之前的算法存在一个问题&#xff0c;比如&#xff1a; a[5][3] a[4][2] a[4][3] a[5][4] a[4][3] a[4][4]我们可以看到计算a[5][3]和a[5][4]时都需要a[4][3]的值&#xff0c…

如何让PPT看起来规整统一

一、字体 常见问题&#xff1a;字体风格太多、文字可读性差、页面风格不匹配 1.使用文字的几个原则 &#xff08;1&#xff09;一份PPT最多使用两种中文字体 比如首页大标题宋体、正文黑体、其他页标题黑体加粗。通过粗细、字号、不同颜色背景等区分不同层级。注意 使用粗体…

【React学习】—jsx语法规则(三)

【React学习】—jsx语法规则&#xff08;三&#xff09; 一、jsx语法规则&#xff1a; 1、定义虚拟DOM&#xff0c;不要写引号&#xff0c; 2、标签中混入JS表达式要用{} 3、样式的类名指定不要用class&#xff0c;要用className 4、内联样式&#xff0c;要用style{{key:value}…

AWS中Lambda集成SNS

1.创建Lambda 在Lambda中&#xff0c;创建名为AWSSNSDemo的函数 use strict console.log(loading function); var aws require(aws-sdk); var docClient new aws.DynamoDB.DocumentClient(); aws.config.regionap-southeast-1;exports.handler function(event,context,cal…

一台电脑B用网线共享另外一台电脑A的WiFi网络,局域网其它电脑C怎么访问电脑B服务

环境: 电脑A:联想E14笔记本 系统:WIN10 专业版 局域网IP:192.168.14.111 共享IP:192.168.137.1 电脑B:HP 288pro 台式机 Ubuntu20.04 系统:共享IP:192.168.137.180 电脑A正常连接WIFI,电脑B没有WIFI只有,有线网口,共享电脑A的无线网 (还有一种桥接网络不在本…

pdf怎么转换成jpg图片?这几个转换方法了解一下

pdf怎么转换成jpg图片&#xff1f;转换PDF文件为JPG图片格式在现代工作中是非常常见的需求&#xff0c;比如将PDF文件中的图表、表格或者图片转换为JPG格式后使用在PPT演示、网页设计等场景中。 【迅捷PDF转换器】是一款非常实用的工具&#xff0c;可以将PDF文件转换成多种不同…

I/O 函数/缓存和字节流、占位符、getchar(),putchar()

I/O 函数 C 语言提供了一些函数&#xff0c;用于与外部设备通信&#xff0c;称为输入输出函数&#xff0c;简称 I/O 函数。输入&#xff08;import&#xff09;指的是获取外部数据&#xff0c;输出&#xff08;export&#xff09;指的是向外部传递数据。 缓存和字节流 严格地…

创建CREATE_STAT_TABLE 统计信息表在达梦和oracle中的使用

达梦 创建CREATE_STAT_TABLE 统计信息表 PROCEDURE CREATE_STAT_TABLE ( STATOWN VARCHAR(128), STATTAB VARCHAR(128), TABLESPACE VARCHAR(128) DEFAULT NULL, GLOBAL_TEMPORARY BOOLEAN DEFAULT FALSE ); 创建普通表的对应系统表的列名字段包括以下&#xff1a; OWNER TABL…

C 语言的逻辑运算符

C 语言的逻辑运算符包括三种&#xff1a; 逻辑运算符可以将两个关系表达式连接起来. Suppose exp1 and exp2 are two simple relational expressions, such as cat > rat and debt 1000 . Then you can state the following: ■ exp1 && exp2 is true only if bo…

MySQL 8.0 事务定义和基本操作

MySQL 事务&#xff08;Transaction&#xff09;的四大特性&#xff1a;A、C、I、D A、原子性&#xff1a;&#xff08;Atomicity&#xff09; 一个事务是不可分割的最小工作单位。 执行的事务&#xff0c;要么全部成功&#xff0c;要么回滚到执行事务之前的状态。 C、一致…

通讯协议035——全网独有的OPC HDA知识一之聚合(四)平均值

本文简单介绍OPC HDA规范的基本概念&#xff0c;更多通信资源请登录网信智汇(wangxinzhihui.com)。 本节旨在详细说明HDA聚合的要求和性能。其目的是使HDA聚合标准化&#xff0c;以便HDA客户端能够可靠地预测聚合计算的结果并理解其含义。如果用户需要聚合中的自定义功能&…

计算机基础知识一

1、计算机系统组成 1.1 硬件 CPU&#xff1a;中央处理器、计算机核心部件、负责计算任务 内存&#xff1a;记忆功能、存储二进制数&#xff0c;内存是一个字节一个地址。 内存大小换算&#xff1a; 8 bits 1 Byte 1024 Bytes Bytes 1 KB &#xff0c; 1024 KB KB 1 …

putty如何保存session

标准版的putty是不支持保存session的&#xff0c;这是处于安全性的考虑。 不过用起来比较麻烦&#xff0c;可以通过快捷方式的办法来实现保存session。 新建一个快捷方式&#xff0c;指向哪里都可以。 右键 | 属性 | 目标 这里修改下&#xff1a; "C:\Program Files\PuTTY…

java 企业工程管理系统软件源码 自主研发 工程行业适用 em

​ 工程项目管理软件&#xff08;工程项目管理系统&#xff09;对建设工程项目管理组织建设、项目策划决策、规划设计、施工建设到竣工交付、总结评估、运维运营&#xff0c;全过程、全方位的对项目进行综合管理 工程项目各模块及其功能点清单 一、系统管理 1、数据字典&#…

文盘 Rust -- tokio 绑定 cpu 实践

tokio 是 rust 生态中流行的异步运行时框架。在实际生产中我们如果希望 tokio 应用程序与特定的 cpu core 绑定该怎么处理呢&#xff1f;这次我们来聊聊这个话题。 首先我们先写一段简单的多任务程序。 use tokio::runtime; pub fn main() {let rt runtime::Builder::new_mu…

【Github】Uptime Kuma:自托管监控工具的完美选择

简介&#xff1a; Uptime Kuma 是一款强大的自托管监控工具&#xff0c;通过简单的部署和配置&#xff0c;可以帮助你监控服务器、VPS 和其他网络服务的在线状态。相比于其他类似工具&#xff0c;Uptime Kuma 提供更多的灵活性和自由度。本文将介绍 Uptime Kuma 的功能、如何使…

Redux中reducer 中为什么每次都要返回新的state!!!

Redux中reducer 中为什么每次都要返回新的state&#xff01;&#xff01;&#xff01; 最近在学习react相关的知识&#xff0c;学习redux的时候遇到看到一个面试题&#xff1a; 如果Redux没返回新的数据会怎样&#xff1f; 这就是要去纠结为什么编写reducer得时候为什么不允许直…

Unity悬浮显示提示内容

在编写unity时&#xff0c;需要实现鼠标在某一个按钮上时&#xff0c;就显示其子物体中对应的下拉菜单&#xff0c;为此编写了一个公共类&#xff0c;对于需要悬浮显示的控件均可挂载此类。代码如下&#xff1a; using UnityEngine; using UnityEngine.EventSystems; using Un…