Skip to content

Instantly share code, notes, and snippets.

@GammaGames
Created November 27, 2024 20:35
Show Gist options
  • Save GammaGames/3d653d30818e7337be68bc6847f86970 to your computer and use it in GitHub Desktop.
Save GammaGames/3d653d30818e7337be68bc6847f86970 to your computer and use it in GitHub Desktop.
pytest-playwright-aria (equivelant to pytest-playwright-visual fixture)
import sys
from pathlib import Path
import pytest
from playwright.sync_api import Page, expect
@pytest.fixture
def assert_aria_snapshot(page: Page, pytestconfig, request, browser_patch):
test_base_dir = Path(request.node.fspath).stem
test_dir = str(Path(request.node.name)).split('[', 1)[0]
filename = f"{str(Path(request.node.name))}[{str(sys.platform)}]"
filepath = (
Path(request.node.fspath).parent.resolve()
/ 'snapshots'
/ test_base_dir
/ test_dir
)
filepath.mkdir(parents=True, exist_ok=True)
def _assert_aria_snapshot(selector="#main"):
browser_patch.screenshot()
if pytestconfig.getoption("--update-snapshots"):
with open(filepath / f"{filename}.yaml", "w") as f:
browser_patch.screenshot()
f.write(page.locator(selector).aria_snapshot())
pytest.fail("--> Aria snapshots updated. Please review yaml")
else:
with open(filepath / f"{filename}.yaml") as f:
expect(page.locator(selector)).to_match_aria_snapshot(f.read())
return _assert_aria_snapshot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment