Web Driver会话的视频录制与TestContainers

发布于 2025-01-20 22:24:19 字数 2454 浏览 0 评论 0 原文

我正在寻找有关如何通过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步之后完成,并且包含空白屏幕的几秒钟。我不要他们。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小兔几 2025-01-27 22:24:19

我自己解决了这个问题。 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文