视频
介绍
🌐 Introduction
使用 Playwright,你可以录制视频以供测试。
🌐 With Playwright you can record videos for your tests.
录视频
🌐 Record video
在测试结束时,视频会在关闭浏览器上下文时保存。如果你手动创建了浏览器上下文,请确保使用 await browser_context.close()。
🌐 Videos are saved upon browser context closure at the end of a test. If you create a browser context manually, make sure to await browser_context.close().
- Sync
- Async
context = browser.new_context(record_video_dir="videos/")
# Make sure to close, so that videos are saved.
context.close()
context = await browser.new_context(record_video_dir="videos/")
# Make sure to await close, so that videos are saved.
await context.close()
你也可以指定视频大小。视频大小默认是将视口大小缩放以适应 800x800。视口的视频会放置在输出视频的左上角,如有必要会缩放以适应。你可能需要将视口大小设置为与你想要的视频大小相匹配。
🌐 You can also specify video size. The video size defaults to the viewport size scaled down to fit 800x800. The video of the viewport is placed in the top-left corner of the output video, scaled down to fit if necessary. You may need to set the viewport size to match your desired video size.
- Sync
- Async
context = browser.new_context(
record_video_dir="videos/",
record_video_size={"width": 640, "height": 480}
)
context = await browser.new_context(
record_video_dir="videos/",
record_video_size={"width": 640, "height": 480}
)
保存的视频文件将出现在指定的文件夹中。它们都有生成的唯一名称。对于多页面的情况,你可以通过 page.video 访问与页面关联的视频文件。
🌐 Saved video files will appear in the specified folder. They all have generated unique names. For the multi-page scenarios, you can access the video file associated with the page via the page.video.
- Sync
- Async
path = page.video.path()
path = await page.video.path()
请注意,视频仅在页面或浏览器上下文关闭后可用。
🌐 Note that the video is only available after the page or browser context is closed.