【问题标题】:How to take a video of what's happening in selenium如何拍摄硒中发生的事情的视频
【发布时间】:2019-02-01 16:21:51
【问题描述】:

我在 Windows 7 中使用 Selenium 3 webdriver 和 Python 3。

我想录制一段关于我的硒测试中发生的事情的视频。

为此,我使用FFmpegscreen-capture-recorder,但我可以更改程序。

这是我的代码:

import unittest
from selenium import webdriver
from subprocess import Popen
#from subprocess import call


cmd = 'ffmpeg -y -rtbufsize 2000M -f dshow -i video="screen-capture-recorder" -r 10 -t 20 screen-capture.mp4'

class SearchProductTest(unittest.TestCase):
    def setUp(self):

        # start the recording of movie
        self.videoRecording = Popen(cmd)

        # create a new Firefox session
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.driver.maximize_window()

        # navigate to the application home page
        self.driver.get("http://demo-store.seleniumacademy.com/")

    def test_search_by_category(self):
        # get the search textbox
        search_field = self.driver.find_element_by_name("q")
        search_field.clear()

        # enter search keyword and submit
        search_field.send_keys("phones")
        search_field.submit()

        # get all the anchor elements which have product names displayed
        # currently on result page using find_elements_by_xpath method
        products = self.driver.find_elements_by_xpath(
            "//h2[@class='product-name']/a")

        # check count of products shown in results
        self.assertEqual(3, len(products))
        #self.videoRecording.terminate()

    def test_something_else(self):
        pass

    def tearDown(self):
        # close the browser window
        self.driver.quit()

        # Stop the recording
        self.videoRecording.terminate()

    #def terminate(process):
        #if process.poll() is None:
        #    call('taskkill /F /T /PID ' + str(process.pid))

if __name__ == '__main__':
    unittest.main(verbosity=2)

问题是:

1) cmd 给出每部电影的最大时间(示例中为 20")。如果测试持续时间更长,则创建电影并且它可以工作(但不完整,只有 20")。

2) 如果测试持续时间较短,则创建文件但它不起作用(阅读器无法读取它,它只是一些字节)。这是主要错误!我不确定从哪里开始播放电影以及在哪里(以及如何)停止。

3) 如果我有多个测试,我希望所有测试都只有一部电影(所以我想在同一部电影中记录所有测试)。

4) 如果可能的话,我更愿意记录 webdriver 窗口(我的测试正在运行的那个)而不是我的屏幕,所以在测试进行的同时我可以做其他事情(它们很慢)。

感谢您的帮助。

【问题讨论】:

    标签: python selenium selenium-webdriver ffmpeg video-capture


    【解决方案1】:

    WebDriver 有 3 种可能对您有用的方法,get_screenshot_as_png、get_screenshot_as_base64 和 get_screenshot_as_file。这样你就可以在后台线程中截图并使用OpenCV and PIL to generate a video file from the results.

    如果您不想引入新的依赖项,您也可以将屏幕截图转储到文件中,最后use ffmpeg to generate a video as well.

    【讨论】:

    • 截屏拍电影你觉得效率高吗?
    • 只要您在内存中处理它们,效率不会比屏幕捕获记录器低多少。在实践中,可能这两种方法对性能的影响都可以忽略不计。现代计算机速度很快。
    • 这不会很好,因为屏幕截图是从 geckodriver 生成的并且包含一个阻塞的 http 请求。所以性能将是一个问题。
    【解决方案2】:

    我没有尝试过,但无论如何它应该可以工作。您的代码终止了 ffmpeg 进程。该软件无法完成视频文件。下面的代码应该优雅地结束这个过程:

    self.videoRecording.send_signal(subprocess.CTRL_C_EVENT)
    self.videoRecording.wait()
    

    您的 popen 语句需要额外的参数。请参考https://docs.python.org/3.7/library/subprocess.html

    import subprocess
    ...
    Popen(cmd, creationflags = subprocess.CREATE_NEW_PROCESS_GROUP)
    

    您还可以使用 ffmpeg 修复损坏的视频文件:

    ffmpeg -err_detect ignore_err -i screen-capture.mp4 -c copy video_fixed-screen-capture.mp4
    

    【讨论】:

    • 它不起作用。该软件不会停止。在我的公开声明中我还需要什么?做什么?让它发挥作用?谢谢
    猜你喜欢
    • 1970-01-01
    • 2020-06-23
    • 1970-01-01
    • 2014-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多