我在 Windows 7 中使用 Selenium 3 webdriver 和 Python 3。
我想录制我的 selenium 测试中发生的事情的视频。
为此,我使用了 FFmpeg和 screen-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) 如果测试 last less 文件被创建但它不起作用(读者无法读取它,它只是一些字节)。这是主要错误!我不确定电影从哪里开始以及在哪里(以及如何)停止。
3) 如果我有多个测试,我希望所有测试都只有一部电影(所以我想在同一部电影中记录所有测试)。
4) 如果可能的话,我更愿意记录 webdriver 窗口(我的测试正在运行的窗口)而不是我的屏幕,所以在测试进行的同时我可以做其他事情(它们很慢)。
谢谢你的帮助。
请您参考如下方法:
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.
