Skip to content

Commit bf5f0c2

Browse files
authored
chore: prevent save_as from hanging in sync mode (#1474)
Resolves #1462. Docs: microsoft/playwright#16120
1 parent b5273b8 commit bf5f0c2

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

playwright/_impl/_video.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ async def path(self) -> pathlib.Path:
5555
return artifact.absolute_path
5656

5757
async def save_as(self, path: Union[str, pathlib.Path]) -> None:
58+
if self._page._connection._is_sync and not self._page._is_closed:
59+
raise Error(
60+
"Page is not yet closed. Close the page prior to calling save_as"
61+
)
5862
artifact = await self._artifact_future
5963
if not artifact:
6064
raise Error("Page did not produce any video frames")

tests/async/test_video.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,16 @@ async def test_short_video_should_throw_persistent_context(
4949

5050
path = await page.video.path()
5151
assert str(tmpdir) in str(path)
52+
53+
54+
async def test_should_not_error_if_page_not_closed_before_save_as(
55+
browser, tmpdir, server
56+
):
57+
page = await browser.new_page(record_video_dir=tmpdir)
58+
await page.goto(server.PREFIX + "/grid.html")
59+
out_path = tmpdir / "some-video.webm"
60+
saved = page.video.save_as(out_path)
61+
await page.close()
62+
await saved
63+
await page.context.close()
64+
assert os.path.exists(out_path)

tests/sync/test_video.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
from pathlib import Path
1717
from typing import Dict
1818

19-
from playwright.sync_api import Browser, BrowserType
19+
import pytest
20+
21+
from playwright.sync_api import Browser, BrowserType, Error
2022
from tests.server import Server
2123

2224

@@ -91,3 +93,21 @@ def test_record_video_can_get_video_path_immediately(
9193
page.wait_for_timeout(1000)
9294
context.close()
9395
assert os.path.exists(path)
96+
97+
98+
def test_should_error_if_page_not_closed_before_save_as(
99+
browser: Browser, tmpdir: Path, server: Server
100+
) -> None:
101+
page = browser.new_page(record_video_dir=tmpdir)
102+
page.goto(server.PREFIX + "/grid.html")
103+
out_path = tmpdir / "some-video.webm"
104+
with pytest.raises(Error) as err:
105+
video = page.video
106+
assert video
107+
video.save_as(out_path)
108+
assert "Page is not yet closed. Close the page prior to calling save_as" in str(err)
109+
assert not os.path.exists(out_path)
110+
page.context.close()
111+
112+
video.save_as(out_path)
113+
assert os.path.exists(out_path)

0 commit comments

Comments
 (0)