我有自动脚本每晚运行(大约50个脚本或测试用例)。我想录制每个测试用例的视频(Selenium Java API + Cucumber)。是否有任何工具或方法可用于分别控制每个测试用例的视频录制?我希望在测试用例设置开始录制和拆解停止录制期间,并在本地保存具有指定名称和日期的视频。所以每个测试用例应该有50个视频(更好的是只保存失败的测试用例的视频)
有没有办法在我用于设置和拆卸的代码中集成此功能?
答案 0 :(得分:5)
我找到了解决方案here,但如果您找到更好的解决方案,则可以发布其他回复:
org.monte.screenrecorder.ScreenRecorder screenRecorder = new ScreenRecorder...
完整的主要代码:
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类扩展了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导入:
<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>
注意:如果在没有GUI的unix上使用此功能,则会出现此错误:
No X11 DISPLAY variable was set, but this program performed an operation which requires it.
我看Jenkins和Travis-ci的Xvfb
插件,我重新编辑了这篇文章。
编辑1:
如果你在没有GUI的unix上使用它,你可以使用Jenkins + Xvfb
插件。
答案 1 :(得分:3)
你可以看看Sergey Pirogov的this library。
答案 2 :(得分:2)
您可以尝试使用Selenium-Grid-Extras或Zalenium 两者都建立在webdriver之上,并自动进行录制。