How to use skip_by_browser 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_shadow_root.py

Source: test_shadow_root.py Github

copy
1import pytest
2
3
4@pytest.fixture(autouse=True)
5def skip_by_browser(request, browser_name):
6    marker = request.node.get_closest_marker('skip_if_browser')
7
8    if marker.args[0] == browser_name:
9        pytest.skip(marker.args[1])
10
11
12@pytest.mark.skip_if_browser('firefox', "Can't get shadowRoot in firefox")
13def test_shadow_root_find_all(test_page):
14    """When I find the shadow root of an element
15    Then the elements in the shadow root can be found.
16    """
17    test_page.navigate()
18    assert test_page.shadow_root_area.data.value == 'Inside a shadow root'
19
Full Screen

conftest.py

Source: conftest.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 asyncio
16import io
17import sys
18
19import pytest
20from PIL import Image
21from pixelmatch import pixelmatch
22from pixelmatch.contrib.PIL import from_PIL_to_raw_data
23
24from playwright._impl._path_utils import get_file_dirname
25
26from .server import test_server
27
28_dirname = get_file_dirname()
29
30
31def pytest_generate_tests(metafunc):
32    if "browser_name" in metafunc.fixturenames:
33        browsers = metafunc.config.option.browser or ["chromium", "firefox", "webkit"]
34        metafunc.parametrize("browser_name", browsers, scope="session")
35
36
37@pytest.fixture(scope="session")
38def event_loop():
39    loop = asyncio.get_event_loop()
40    yield loop
41    loop.close()
42
43
44@pytest.fixture(scope="session")
45def assetdir():
46    return _dirname / "assets"
47
48
49@pytest.fixture(scope="session")
50def launch_arguments(pytestconfig):
51    return {
52        "headless": not pytestconfig.getoption("--headful"),
53    }
54
55
56@pytest.fixture
57def server():
58    yield test_server.server
59
60
61@pytest.fixture
62def https_server():
63    yield test_server.https_server
64
65
66@pytest.fixture
67def ws_server():
68    yield test_server.ws_server
69
70
71@pytest.fixture(autouse=True, scope="session")
72async def start_server():
73    test_server.start()
74    yield
75    test_server.stop()
76
77
78@pytest.fixture(autouse=True)
79def after_each_hook():
80    yield
81    test_server.reset()
82
83
84@pytest.fixture(scope="session")
85def browser_name(pytestconfig):
86    return pytestconfig.getoption("browser")
87
88
89@pytest.fixture(scope="session")
90def is_webkit(browser_name):
91    return browser_name == "webkit"
92
93
94@pytest.fixture(scope="session")
95def is_firefox(browser_name):
96    return browser_name == "firefox"
97
98
99@pytest.fixture(scope="session")
100def is_chromium(browser_name):
101    return browser_name == "chromium"
102
103
104@pytest.fixture(scope="session")
105def is_win():
106    return sys.platform == "win32"
107
108
109@pytest.fixture(scope="session")
110def is_linux():
111    return sys.platform == "linux"
112
113
114@pytest.fixture(scope="session")
115def is_mac():
116    return sys.platform == "darwin"
117
118
119def _get_skiplist(request, values, value_name):
120    skipped_values = []
121    # Allowlist
122    only_marker = request.node.get_closest_marker(f"only_{value_name}")
123    if only_marker:
124        skipped_values = values
125        skipped_values.remove(only_marker.args[0])
126
127    # Denylist
128    skip_marker = request.node.get_closest_marker(f"skip_{value_name}")
129    if skip_marker:
130        skipped_values.append(skip_marker.args[0])
131
132    return skipped_values
133
134
135@pytest.fixture(autouse=True)
136def skip_by_browser(request, browser_name):
137    skip_browsers_names = _get_skiplist(
138        request, ["chromium", "firefox", "webkit"], "browser"
139    )
140
141    if browser_name in skip_browsers_names:
142        pytest.skip(f"skipped for this browser: {browser_name}")
143
144
145@pytest.fixture(autouse=True)
146def skip_by_platform(request):
147    skip_platform_names = _get_skiplist(
148        request, ["win32", "linux", "darwin"], "platform"
149    )
150
151    if sys.platform in skip_platform_names:
152        pytest.skip(f"skipped on this platform: {sys.platform}")
153
154
155def pytest_addoption(parser):
156    group = parser.getgroup("playwright", "Playwright")
157    group.addoption(
158        "--browser",
159        action="append",
160        default=[],
161        help="Browsers which should be used. By default on all the browsers.",
162    )
163    parser.addoption(
164        "--headful",
165        action="store_true",
166        default=False,
167        help="Run tests in headful mode.",
168    )
169
170
171@pytest.fixture(scope="session")
172def assert_to_be_golden(browser_name: str):
173    def compare(received_raw: bytes, golden_name: str):
174        golden_file = (_dirname / f"golden-{browser_name}" / golden_name).read_bytes()
175        received_image = Image.open(io.BytesIO(received_raw))
176        golden_image = Image.open(io.BytesIO(golden_file))
177
178        if golden_image.size != received_image.size:
179            pytest.fail("Image size differs to golden image")
180            return
181        diff_pixels = pixelmatch(
182            from_PIL_to_raw_data(received_image),
183            from_PIL_to_raw_data(golden_image),
184            golden_image.size[0],
185            golden_image.size[1],
186            threshold=0.2,
187        )
188        assert diff_pixels == 0
189
190    return compare
191
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)