File: test_chromium_tracing.py

package info (click to toggle)
python-playwright 1.55.0%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,728 kB
  • sloc: python: 53,655; javascript: 383; sh: 216; makefile: 6
file content (119 lines) | stat: -rw-r--r-- 4,239 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# Copyright (c) Microsoft Corporation.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import json
import os
from pathlib import Path

import pytest

from playwright.async_api import Browser, Page
from tests.server import Server


@pytest.mark.only_browser("chromium")
async def test_should_output_a_trace(
    browser: Browser, page: Page, server: Server, tmp_path: Path
) -> None:
    output_file = tmp_path / "trace.json"
    await browser.start_tracing(page=page, screenshots=True, path=output_file)
    await page.goto(server.PREFIX + "/grid.html")
    await browser.stop_tracing()
    assert os.path.getsize(output_file) > 0


@pytest.mark.only_browser("chromium")
async def test_should_create_directories_as_needed(
    browser: Browser, page: Page, server: Server, tmp_path: Path
) -> None:
    output_file = tmp_path / "these" / "are" / "directories" / "trace.json"
    await browser.start_tracing(page=page, screenshots=True, path=output_file)
    await page.goto(server.PREFIX + "/grid.html")
    await browser.stop_tracing()
    assert os.path.getsize(output_file) > 0


async def rafraf(target: Page, count: int = 1) -> None:
    for _ in range(count):
        await target.evaluate(
            "async () => await new Promise(f => window.requestAnimationFrame(() => window.requestAnimationFrame(f)));"
        )


@pytest.mark.only_browser("chromium")
async def test_should_run_with_custom_categories_if_provided(
    browser: Browser, page: Page, tmp_path: Path
) -> None:
    output_file = tmp_path / "trace.json"
    await browser.start_tracing(
        page=page,
        path=output_file,
        categories=["disabled-by-default-cc.debug"],
    )
    await rafraf(page)
    await browser.stop_tracing()
    with open(output_file, mode="r") as of:
        trace_json = json.load(of)
        trace_config = trace_json["metadata"].get("trace-config")
        trace_events = trace_json["traceEvents"]
        assert (
            trace_config is not None and "disabled-by-default-cc.debug" in trace_config
        ) or any(
            event.get("cat") == "disabled-by-default-cc.debug" for event in trace_events
        )


@pytest.mark.only_browser("chromium")
async def test_should_throw_if_tracing_on_two_pages(
    browser: Browser, page: Page, tmp_path: Path
) -> None:
    output_file_1 = tmp_path / "trace1.json"
    await browser.start_tracing(page=page, screenshots=True, path=output_file_1)
    output_file_2 = tmp_path / "trace2.json"
    with pytest.raises(Exception):
        await browser.start_tracing(page=page, screenshots=True, path=output_file_2)
    await browser.stop_tracing()


@pytest.mark.only_browser("chromium")
async def test_should_return_a_buffer(
    browser: Browser, page: Page, server: Server, tmp_path: Path
) -> None:
    output_file = tmp_path / "trace.json"
    await browser.start_tracing(page=page, path=output_file, screenshots=True)
    await page.goto(server.PREFIX + "/grid.html")
    value = await browser.stop_tracing()
    with open(output_file, mode="r") as trace_file:
        assert trace_file.read() == value.decode()


@pytest.mark.only_browser("chromium")
async def test_should_work_without_options(
    browser: Browser, page: Page, server: Server
) -> None:
    await browser.start_tracing()
    await page.goto(server.PREFIX + "/grid.html")
    trace = await browser.stop_tracing()
    assert trace


@pytest.mark.only_browser("chromium")
async def test_should_support_a_buffer_without_a_path(
    browser: Browser, page: Page, server: Server
) -> None:
    await browser.start_tracing(page=page, screenshots=True)
    await page.goto(server.PREFIX + "/grid.html")
    await rafraf(page)
    trace = await browser.stop_tracing()
    assert "screenshot" in trace.decode()