How to use test_should_support_a_buffer_without_a_path method in Playwright Python

Best Python code snippet using playwright-python

Run Playwright Python automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

test_chromium_tracing.py

Source: test_chromium_tracing.py Github

copy
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
15import json
16import os
17from pathlib import Path
18
19import pytest
20
21from playwright.async_api import Browser, Page
22
23
24@pytest.mark.only_browser("chromium")
25async def test_should_output_a_trace(
26    browser: Browser, page: Page, server, tmpdir: Path
27):
28    output_file = tmpdir / "trace.json"
29    await browser.start_tracing(page=page, screenshots=True, path=output_file)
30    await page.goto(server.PREFIX + "/grid.html")
31    await browser.stop_tracing()
32    assert os.path.getsize(output_file) > 0
33
34
35@pytest.mark.only_browser("chromium")
36async def test_should_create_directories_as_needed(
37    browser: Browser, page: Page, server, tmpdir
38):
39    output_file = tmpdir / "these" / "are" / "directories" / "trace.json"
40    await browser.start_tracing(page=page, screenshots=True, path=output_file)
41    await page.goto(server.PREFIX + "/grid.html")
42    await browser.stop_tracing()
43    assert os.path.getsize(output_file) > 0
44
45
46@pytest.mark.only_browser("chromium")
47async def test_should_run_with_custom_categories_if_provided(
48    browser: Browser, page: Page, tmpdir: Path
49):
50    output_file = tmpdir / "trace.json"
51    await browser.start_tracing(
52        page=page,
53        screenshots=True,
54        path=output_file,
55        categories=["disabled-by-default-v8.cpu_profiler.hires"],
56    )
57    await browser.stop_tracing()
58    with open(output_file, mode="r") as of:
59        trace_json = json.load(of)
60        assert (
61            "disabled-by-default-v8.cpu_profiler.hires"
62            in trace_json["metadata"]["trace-config"]
63        )
64
65
66@pytest.mark.only_browser("chromium")
67async def test_should_throw_if_tracing_on_two_pages(
68    browser: Browser, page: Page, tmpdir: Path
69):
70    output_file_1 = tmpdir / "trace1.json"
71    await browser.start_tracing(page=page, screenshots=True, path=output_file_1)
72    output_file_2 = tmpdir / "trace2.json"
73    with pytest.raises(Exception):
74        await browser.start_tracing(page=page, screenshots=True, path=output_file_2)
75    await browser.stop_tracing()
76
77
78@pytest.mark.only_browser("chromium")
79async def test_should_return_a_buffer(
80    browser: Browser, page: Page, server, tmpdir: Path
81):
82    output_file = tmpdir / "trace.json"
83    await browser.start_tracing(page=page, path=output_file, screenshots=True)
84    await page.goto(server.PREFIX + "/grid.html")
85    value = await browser.stop_tracing()
86    with open(output_file, mode="r") as trace_file:
87        assert trace_file.read() == value.decode()
88
89
90@pytest.mark.only_browser("chromium")
91async def test_should_work_without_options(browser: Browser, page: Page, server):
92    await browser.start_tracing()
93    await page.goto(server.PREFIX + "/grid.html")
94    trace = await browser.stop_tracing()
95    assert trace
96
97
98@pytest.mark.only_browser("chromium")
99async def test_should_support_a_buffer_without_a_path(
100    browser: Browser, page: Page, server
101):
102    await browser.start_tracing(page=page, screenshots=True)
103    await page.goto(server.PREFIX + "/grid.html")
104    trace = await browser.stop_tracing()
105    assert "screenshot" in trace.decode()
106
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run Python Tests on LambdaTest Cloud Grid

Execute automation tests with Playwright Python on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)