|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from pathlib import Path |
| 16 | +from typing import List |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | +from playwright._impl._api_structures import ScreencastFrame |
| 21 | +from playwright.async_api import Page |
| 22 | +from tests.server import Server |
| 23 | + |
| 24 | + |
| 25 | +def _noop_frame(f: ScreencastFrame) -> None: |
| 26 | + pass |
| 27 | + |
| 28 | + |
| 29 | +async def test_screencast_start_should_deliver_frames_via_on_frame( |
| 30 | + page: Page, server: Server |
| 31 | +) -> None: |
| 32 | + frames: List[ScreencastFrame] = [] |
| 33 | + |
| 34 | + def collect_frame(f: ScreencastFrame) -> None: |
| 35 | + frames.append(f) |
| 36 | + |
| 37 | + await page.screencast.start(on_frame=collect_frame) |
| 38 | + await page.goto(server.EMPTY_PAGE) |
| 39 | + await page.evaluate("() => document.body.style.backgroundColor = 'red'") |
| 40 | + await page.wait_for_timeout(500) |
| 41 | + await page.screencast.stop() |
| 42 | + assert len(frames) > 0, "expected at least one frame" |
| 43 | + for frame in frames: |
| 44 | + assert frame["data"][:2] == b"\xff\xd8", "expected JPEG frame" |
| 45 | + |
| 46 | + |
| 47 | +async def test_screencast_start_should_throw_if_already_started( |
| 48 | + page: Page, |
| 49 | +) -> None: |
| 50 | + await page.screencast.start(on_frame=_noop_frame) |
| 51 | + with pytest.raises(Exception, match="already started"): |
| 52 | + await page.screencast.start(on_frame=_noop_frame) |
| 53 | + await page.screencast.stop() |
| 54 | + |
| 55 | + |
| 56 | +async def test_screencast_start_should_record_video_to_path( |
| 57 | + page: Page, server: Server, tmp_path: Path |
| 58 | +) -> None: |
| 59 | + video_path = tmp_path / "video.webm" |
| 60 | + await page.screencast.start(path=video_path) |
| 61 | + await page.goto(server.EMPTY_PAGE) |
| 62 | + await page.evaluate("() => document.body.style.backgroundColor = 'red'") |
| 63 | + await page.wait_for_timeout(500) |
| 64 | + await page.screencast.stop() |
| 65 | + assert video_path.exists(), f"video file should exist: {video_path}" |
| 66 | + assert video_path.stat().st_size > 0 |
| 67 | + |
| 68 | + |
| 69 | +async def test_screencast_start_returns_disposable(page: Page) -> None: |
| 70 | + disposable = await page.screencast.start(on_frame=_noop_frame) |
| 71 | + async with disposable: |
| 72 | + pass |
| 73 | + # After dispose, starting again should succeed. |
| 74 | + await page.screencast.start(on_frame=_noop_frame) |
| 75 | + await page.screencast.stop() |
| 76 | + |
| 77 | + |
| 78 | +async def test_screencast_show_overlay(page: Page, server: Server) -> None: |
| 79 | + await page.goto(server.EMPTY_PAGE) |
| 80 | + disposable = await page.screencast.show_overlay("<div>Hello Overlay</div>") |
| 81 | + assert disposable |
| 82 | + async with disposable: |
| 83 | + pass |
| 84 | + |
| 85 | + |
| 86 | +async def test_screencast_show_chapter(page: Page, server: Server) -> None: |
| 87 | + await page.goto(server.EMPTY_PAGE) |
| 88 | + await page.screencast.show_chapter("Chapter Title") |
| 89 | + await page.screencast.show_chapter( |
| 90 | + "With Description", description="Some details", duration=100 |
| 91 | + ) |
| 92 | + |
| 93 | + |
| 94 | +async def test_screencast_hide_show_overlays(page: Page, server: Server) -> None: |
| 95 | + await page.goto(server.EMPTY_PAGE) |
| 96 | + await page.screencast.show_overlay("<div>visible</div>") |
| 97 | + await page.screencast.hide_overlays() |
| 98 | + await page.screencast.show_overlays() |
| 99 | + |
| 100 | + |
| 101 | +async def test_screencast_show_and_hide_actions(page: Page, server: Server) -> None: |
| 102 | + await page.goto(server.EMPTY_PAGE) |
| 103 | + async with await page.screencast.show_actions(): |
| 104 | + pass |
| 105 | + await page.screencast.hide_actions() |
0 commit comments