|
| 1 | +import os |
| 2 | + |
| 3 | +import pytest |
| 4 | +from django.contrib.auth.models import User |
| 5 | + |
| 6 | + |
| 7 | +# Set DJANGO_ALLOW_ASYNC_UNSAFE |
| 8 | +os.environ["DJANGO_ALLOW_ASYNC_UNSAFE"] = "true" |
| 9 | + |
| 10 | +# Default browser to run tests with |
| 11 | +os.environ.setdefault("PLAYWRIGHT_BROWSER_NAME", "chromium") |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def user(): |
| 16 | + """Create a test superuser.""" |
| 17 | + u = User.objects.create( |
| 18 | + username="test", is_active=True, is_staff=True, is_superuser=True |
| 19 | + ) |
| 20 | + u.set_password("test") |
| 21 | + u.save() |
| 22 | + return u |
| 23 | + |
| 24 | + |
| 25 | +@pytest.fixture |
| 26 | +def client(client, user): |
| 27 | + """Login the test client.""" |
| 28 | + client.login(username="test", password="test") |
| 29 | + return client |
| 30 | + |
| 31 | + |
| 32 | +@pytest.fixture(scope="session") |
| 33 | +def browser_type_launch_args(): |
| 34 | + """Configure browser launch arguments.""" |
| 35 | + return { |
| 36 | + "headless": True, |
| 37 | + "args": [ |
| 38 | + "--no-sandbox", |
| 39 | + "--disable-gpu", |
| 40 | + "--disable-dev-shm-usage", |
| 41 | + "--disable-setuid-sandbox", |
| 42 | + ], |
| 43 | + } |
| 44 | + |
| 45 | + |
| 46 | +@pytest.fixture(scope="function") |
| 47 | +def browser_context_args(browser_context_args): |
| 48 | + """Modify browser context arguments for tracing.""" |
| 49 | + return { |
| 50 | + **browser_context_args, |
| 51 | + "record_video_dir": os.path.join(os.getcwd(), "test-results/videos/"), |
| 52 | + "record_har_path": os.path.join(os.getcwd(), "test-results/har/", "test.har"), |
| 53 | + "ignore_https_errors": True, |
| 54 | + "java_script_enabled": True, |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | +@pytest.fixture |
| 59 | +def page(page): |
| 60 | + """Add console and network error logging to the page.""" |
| 61 | + # Capture console logs |
| 62 | + page.on("console", lambda msg: print(f"BROWSER CONSOLE {msg.type}: {msg.text}")) |
| 63 | + |
| 64 | + # Capture JavaScript errors |
| 65 | + page.on("pageerror", lambda err: print(f"BROWSER JS ERROR: {err}")) |
| 66 | + |
| 67 | + # Capture request failures |
| 68 | + page.on( |
| 69 | + "requestfailed", |
| 70 | + lambda request: print(f"NETWORK ERROR: {request.url} {request.failure}"), |
| 71 | + ) |
| 72 | + |
| 73 | + return page |
| 74 | + |
| 75 | + |
| 76 | +@pytest.hookimpl(hookwrapper=True) |
| 77 | +def pytest_runtest_makereport(item, call): |
| 78 | + """Handle reporting and artifact generation.""" |
| 79 | + outcome = yield |
| 80 | + report = outcome.get_result() |
| 81 | + |
| 82 | + # Take screenshot of failed tests |
| 83 | + if report.when == "call" and report.failed: |
| 84 | + try: |
| 85 | + page = item.funcargs["page"] |
| 86 | + # Take screenshot and save it with test name |
| 87 | + screenshot_dir = os.path.join(os.getcwd(), "test-results/screenshots/") |
| 88 | + os.makedirs(screenshot_dir, exist_ok=True) |
| 89 | + screenshot_path = os.path.join(screenshot_dir, f"{item.name}_failed.png") |
| 90 | + page.screenshot(path=screenshot_path) |
| 91 | + # Save page HTML |
| 92 | + html_path = os.path.join(screenshot_dir, f"{item.name}_failed.html") |
| 93 | + with open(html_path, "w", encoding="utf-8") as f: |
| 94 | + f.write(page.content()) |
| 95 | + |
| 96 | + # Add to report |
| 97 | + report.extra = [ |
| 98 | + { |
| 99 | + "name": "Screenshot", |
| 100 | + "content": screenshot_path, |
| 101 | + "mime_type": "image/png", |
| 102 | + }, |
| 103 | + {"name": "HTML", "content": html_path, "mime_type": "text/html"}, |
| 104 | + ] |
| 105 | + except Exception as e: |
| 106 | + print(f"Failed to capture artifacts: {e}") |
0 commit comments