Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test flakiness - trace playwright tests and upload traces of failing tests #4612

Merged
merged 3 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -283,8 +283,18 @@ jobs:
DB_USER: postgres
DB_PASSWORD: ''
E2E_DRIVER: ${{ matrix.browser }}
E2E_ENABLE_TRACING: '1'
E2E_TRACES_PATH: /tmp/playwright_traces
SDK_RELEASE: ${{ steps.sdk-tag.outputs.sdk_tag }}

- name: Upload playwright traces artifact on failure
if: ${{ failure() }}
uses: actions/upload-artifact@v4
with:
name: playwright-traces-${{ matrix.browser }}
path: /tmp/playwright_traces
retention-days: 1

docs:
name: Build and check documentation
runs-on: ubuntu-latest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,16 @@ const LegacyConfigFields = ({apiGroupChoices}) => (
<PaymentStatusUpdateJSON />
</Fieldset>

<DocumentTypesFieldet />
<ErrorBoundary
errorMessage={
<FormattedMessage
description="Objects API registrations options: document types selection error"
defaultMessage="Something went wrong while retrieving the available catalogues and/or document types."
/>
}
>
<DocumentTypesFieldet />
</ErrorBoundary>

<LegacyDocumentTypesFieldet />

Expand Down
63 changes: 61 additions & 2 deletions src/openforms/tests/e2e/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import inspect
import os
import re
from contextlib import asynccontextmanager
from pathlib import Path
from typing import Any, Literal, TypeAlias

from django.contrib.staticfiles.testing import StaticLiveServerTestCase
Expand All @@ -10,9 +12,17 @@
from asgiref.sync import sync_to_async
from furl import furl
from maykin_2fa.test import disable_admin_mfa
from playwright.async_api import BrowserType, Locator, Page, async_playwright, expect
from playwright.async_api import (
BrowserContext,
BrowserType,
Locator,
Page,
async_playwright,
expect,
)

from openforms.accounts.tests.factories import SuperUserFactory
from openforms.conf.utils import config

SupportedBrowser: TypeAlias = Literal["chromium", "firefox", "webkit"]

Expand All @@ -21,6 +31,9 @@
SLOW_MO = int(os.environ.get("SLOW_MO", "100"))
PLAYWRIGHT_BROWSERS_PATH = os.getenv("PLAYWRIGHT_BROWSERS_PATH", default=None)

ENABLE_TRACING: bool = config("E2E_ENABLE_TRACING", default=False)
TRACES_ROOT_PATH = Path(config("E2E_TRACES_PATH", default="/tmp/playwright")) # type: ignore

LAUNCH_KWARGS = {
"headless": HEADLESS,
"slow_mo": SLOW_MO,
Expand All @@ -39,6 +52,51 @@ def create_superuser(**kwargs):
SuperUserFactory.create(**kwargs)


@asynccontextmanager
async def maybe_trace(context: BrowserContext):
"""
Use playwright tracing if opted in through the envvar.

Usage:
"""
trace_file_path: Path | None = None

if ENABLE_TRACING:
# Thanks ChatGPT.
stack = inspect.stack()
test_method_name = ""
test_case_name = ""
for frame in stack:
# The frame's `function` attribute will give the name of the method
# The `frame` object contains a `frame` attribute that provides access to the locals
if "self" not in frame.frame.f_locals:
continue

# Check if `self` is an instance of a `unittest.TestCase` subclass
self_instance = frame.frame.f_locals["self"]
if not isinstance(self_instance, E2ETestCase):
continue

test_cls = self_instance.__class__
dir_path = TRACES_ROOT_PATH / Path(test_cls.__module__.replace(".", "/"))
test_case_name = test_cls.__name__
test_method_name = frame.function
trace_file_path = dir_path / f"{test_case_name}.{test_method_name}.zip"
break

if trace_file_path:
await context.tracing.start(screenshots=True, snapshots=True, sources=True)

save_trace = True
try:
yield
save_trace = False
finally:
if ENABLE_TRACING and save_trace and trace_file_path:
trace_file_path.parent.mkdir(parents=True, exist_ok=True)
await context.tracing.stop(path=trace_file_path)


@asynccontextmanager
async def browser_page():
context_kwargs: dict[str, Any] = {
Expand All @@ -54,7 +112,8 @@ async def browser_page():
browser = await _browser.launch(**LAUNCH_KWARGS)
context = await browser.new_context(**context_kwargs)
page = await context.new_page()
yield page
async with maybe_trace(context):
yield page
finally:
await browser.close()

Expand Down
Loading