'Video Recording of Webdriver session with TestContainers
I am looking for the good example about how to record a webdriver session inside docker via testcontainers. Here is my solution:
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
And it works. If you run it you will get an video.mp4 inside project dir. But I am expecting that the video will be completed after the step #3. Actually the video is completing after step #4, and it contains some seconds of the blank screen. I don't want them.
Solution 1:[1]
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
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Nikita Chegodaev |
