Web Driver会话的视频录制与TestContainers
我正在寻找有关如何通过TestContainers在Docker内部录制WebDriver会话的一个很好的示例。这是我的解决方案:
lateinit var docker: BrowserWebDriverContainer<Nothing>
fun main() {
//1. Create webdriver and start Docker
val webDriver = startDocker()
//2. Open some page
webDriver.get("https://www.github.com")
//3. Stop the webdriver
webDriver.quit()
//4. Notify docker to save video
docker.afterTest(TestDi(), Optional.of(Throwable("")))
//5. Stop docker container
docker.stop()
}
fun create(): WebDriver {
WebDriverManager.getInstance(DriverManagerType.CHROME).driverVersion("latest").setup()
return ChromeDriver(getCapabilities())
}
private class RecordingFileFactoryImpl: RecordingFileFactory {
override fun recordingFileForTest(vncRecordingDirectory: File?, prefix: String?, succeeded: Boolean) =
Paths.get("video.mp4").toFile()
}
private class TestDi: TestDescription {
override fun getTestId() = ""
override fun getFilesystemFriendlyName() = "/build/"
}
private fun startDocker() : WebDriver {
docker = BrowserWebDriverContainer<Nothing>()
.withCapabilities(getCapabilities())
docker.withRecordingMode(
BrowserWebDriverContainer.VncRecordingMode.RECORD_ALL,
File("build"),
VncRecordingContainer.VncRecordingFormat.MP4
)
docker.withRecordingFileFactory(
RecordingFileFactoryImpl()
)
docker.start()
return docker.webDriver
}
private fun getCapabilities(): DesiredCapabilities {
val caps = DesiredCapabilities.chrome()
val options = ChromeOptions()
options.addArguments(
"--allow-insecure-localhost",
"--safebrowsing-disable-extension-blacklist",
"--safebrowsing-disable-download-protection",
)
caps.setCapability(ChromeOptions.CAPABILITY, options)
caps.setCapability("acceptInsecureCerts", true)
val chromePrefs = HashMap<String, Any>()
chromePrefs["profile.default_content_settings.popups"] = 0
chromePrefs["download.default_directory"] = "build"
chromePrefs["safebrowsing.enabled"] = "true"
options.setExperimentalOption("prefs", chromePrefs)
return caps
}
https://github.com/nachg/dockerdemo
并且它起作用。如果运行它,您将在project dir中获得 video.mp4
。但是我希望该视频将在第3步之后完成。实际上,该视频将在第4步之后完成,并且包含空白屏幕的几秒钟。我不要他们。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我自己解决了这个问题。 Soltion是 - 复制视频流文件并转换副本。请参阅我上次提交的详细信息:
I resolved this issue by myself. Soltion was - to copy the video stream file and convert the copy. Please see the details in my last commit: https://github.com/nachg/DockerDemo/commit/9664eb818fc95979c681b6ce806b92e7e647d413