Skip to content

Commit 3349e4d

Browse files
committed
added artifacts/video remote handling
1 parent ae908a3 commit 3349e4d

File tree

4 files changed

+59
-6
lines changed

4 files changed

+59
-6
lines changed

playwright/_impl/_page.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
ViewportSize,
3030
)
3131
from playwright._impl._api_types import Error
32+
from playwright._impl._artifact import Artifact
3233
from playwright._impl._connection import (
3334
ChannelOwner,
3435
from_channel,
@@ -285,7 +286,9 @@ def _on_dialog(self, params: Any) -> None:
285286
def _on_download(self, params: Any) -> None:
286287
url = params["url"]
287288
suggested_filename = params["suggestedFilename"]
288-
artifact = from_channel(params["artifact"])
289+
artifact = cast(Artifact, from_channel(params["artifact"]))
290+
if self._browser_context._browser:
291+
artifact._is_remote = self._browser_context._browser._is_remote
289292
self.emit(
290293
Page.Events.Download, Download(self, url, suggested_filename, artifact)
291294
)

playwright/_impl/_video.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ def __init__(self, page: "Page") -> None:
2828
self._dispatcher_fiber = page._dispatcher_fiber
2929
self._page = page
3030
self._artifact_future = page._loop.create_future()
31+
if page.context.browser:
32+
self._is_remote = page.context.browser._is_remote
33+
else:
34+
self._is_remote = False
3135
if page.is_closed():
3236
self._page_closed()
3337
else:
@@ -42,9 +46,14 @@ def _page_closed(self) -> None:
4246

4347
def _artifact_ready(self, artifact: Artifact) -> None:
4448
if not self._artifact_future.done():
49+
artifact._is_remote = self._is_remote
4550
self._artifact_future.set_result(artifact)
4651

4752
async def path(self) -> pathlib.Path:
53+
if self._is_remote:
54+
raise Error(
55+
"Path is not available when using browserType.connect(). Use save_as() to save a local copy."
56+
)
4857
artifact = await self._artifact_future
4958
if not artifact:
5059
raise Error("Page did not produce any video frames")

tests/async/test_browsertype_connect.py

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,47 @@ async def test_browser_type_connect_should_reject_navigation_when_browser_closes
138138
await page.goto(server.PREFIX + "/one-style.html")
139139
assert "Playwright connection closed" in exc_info.value.message
140140

141-
with pytest.raises(Error) as exc_info:
142-
await page.goto(server.PREFIX + "/one-style.html")
143-
assert "Playwright connection closed" in exc_info.value.message
141+
142+
async def test_should_not_allow_getting_the_path(
143+
browser_type: BrowserType, launch_server, server: Server
144+
):
145+
def handle_download(request):
146+
request.setHeader("Content-Type", "application/octet-stream")
147+
request.setHeader("Content-Disposition", "attachment")
148+
request.write(b"Hello world")
149+
request.finish()
150+
151+
server.set_route("/download", handle_download)
152+
153+
remote_server = launch_server()
154+
browser = await browser_type.connect(remote_server.ws_endpoint)
155+
page = await browser.new_page(accept_downloads=True)
156+
await page.set_content(f'<a href="{server.PREFIX}/download">download</a>')
157+
async with page.expect_download() as download_info:
158+
await page.click("a")
159+
download = await download_info.value
160+
with pytest.raises(Error) as exc:
161+
await download.path()
162+
assert (
163+
exc.value.message
164+
== "Path is not available when using browser_type.connect(). Use save_as() to save a local copy."
165+
)
166+
remote_server.kill()
167+
168+
169+
async def test_prevent_getting_video_path(
170+
browser_type: BrowserType, launch_server, tmpdir, server
171+
):
172+
remote_server = launch_server()
173+
browser = await browser_type.connect(remote_server.ws_endpoint)
174+
page = await browser.new_page(record_video_dir=tmpdir)
175+
await page.goto(server.PREFIX + "/grid.html")
176+
await browser.close()
177+
assert page.video
178+
with pytest.raises(Error) as exc:
179+
await page.video.path()
180+
assert (
181+
exc.value.message
182+
== "Path is not available when using browserType.connect(). Use save_as() to save a local copy."
183+
)
184+
remote_server.kill()

tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,12 +249,12 @@ def kill(self):
249249
def launch_server(browser_name: str, launch_arguments: Dict, tmp_path: Path):
250250
remotes: List[RemoteServer] = []
251251

252-
def _launch_server(override_launch_options: Dict = {}):
252+
def _launch_server(**kwargs: Dict):
253253
remote = RemoteServer(
254254
browser_name,
255255
{
256256
**launch_arguments,
257-
**override_launch_options,
257+
**kwargs,
258258
},
259259
tmp_path / f"settings-{len(remotes)}.json",
260260
)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy