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

fix: migrate artifacts_folder to session fixture #223

Merged
merged 2 commits into from
Apr 24, 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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
os: [ubuntu-latest, windows-latest, macos-13]
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

macos-latest is now macos-14-arm64. setup-python does not support macos-arm64 yet.

python-version: [3.8, 3.9, '3.10', 3.11]
runs-on: ${{ matrix.os }}
steps:
Expand Down
39 changes: 27 additions & 12 deletions pytest_playwright/pytest_playwright.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,17 @@
from slugify import slugify
import tempfile

artifacts_folder = tempfile.TemporaryDirectory(prefix="playwright-pytest-")

@pytest.fixture(scope="session")
def _pw_artifacts_folder() -> Generator[tempfile.TemporaryDirectory, None, None]:
artifacts_folder = tempfile.TemporaryDirectory(prefix="playwright-pytest-")
yield artifacts_folder
try:
# On Windows, files can be still in use.
# https://github.com/microsoft/playwright-pytest/issues/163
artifacts_folder.cleanup()
except (PermissionError, NotADirectoryError):
pass


@pytest.fixture(scope="session", autouse=True)
Expand Down Expand Up @@ -193,6 +203,7 @@ def browser_context_args(
playwright: Playwright,
device: Optional[str],
base_url: Optional[str],
_pw_artifacts_folder: tempfile.TemporaryDirectory,
) -> Dict:
context_args = {}
if device:
Expand All @@ -203,7 +214,7 @@ def browser_context_args(
video_option = pytestconfig.getoption("--video")
capture_video = video_option in ["on", "retain-on-failure"]
if capture_video:
context_args["record_video_dir"] = artifacts_folder.name
context_args["record_video_dir"] = _pw_artifacts_folder.name

return context_args

Expand All @@ -213,8 +224,11 @@ def _artifacts_recorder(
request: pytest.FixtureRequest,
playwright: Playwright,
pytestconfig: Any,
_pw_artifacts_folder: tempfile.TemporaryDirectory,
) -> Generator["ArtifactsRecorder", None, None]:
artifacts_recorder = ArtifactsRecorder(pytestconfig, request, playwright)
artifacts_recorder = ArtifactsRecorder(
pytestconfig, request, playwright, _pw_artifacts_folder
)
yield artifacts_recorder
# If request.node is missing rep_call, then some error happened during execution
# that prevented teardown, but should still be counted as a failure
Expand Down Expand Up @@ -252,12 +266,6 @@ def browser(launch_browser: Callable[[], Browser]) -> Generator[Browser, None, N
browser = launch_browser()
yield browser
browser.close()
try:
# On Windows, files can be still in use.
# https://github.com/microsoft/playwright-pytest/issues/163
artifacts_folder.cleanup()
except (PermissionError, NotADirectoryError):
pass


class CreateContextCallback(Protocol):
Expand Down Expand Up @@ -451,11 +459,16 @@ def pytest_addoption(parser: Any) -> None:

class ArtifactsRecorder:
def __init__(
self, pytestconfig: Any, request: pytest.FixtureRequest, playwright: Playwright
self,
pytestconfig: Any,
request: pytest.FixtureRequest,
playwright: Playwright,
pw_artifacts_folder: tempfile.TemporaryDirectory,
) -> None:
self._request = request
self._pytestconfig = pytestconfig
self._playwright = playwright
self._pw_artifacts_folder = pw_artifacts_folder

self._all_pages: List[Page] = []
self._screenshots: List[str] = []
Expand Down Expand Up @@ -542,7 +555,7 @@ def on_did_create_browser_context(self, context: BrowserContext) -> None:

def on_will_close_browser_context(self, context: BrowserContext) -> None:
if self._capture_trace:
trace_path = Path(artifacts_folder.name) / create_guid()
trace_path = Path(self._pw_artifacts_folder.name) / create_guid()
context.tracing.stop(path=trace_path)
self._traces.append(str(trace_path))
else:
Expand All @@ -551,7 +564,9 @@ def on_will_close_browser_context(self, context: BrowserContext) -> None:
if self._pytestconfig.getoption("--screenshot") in ["on", "only-on-failure"]:
for page in context.pages:
try:
screenshot_path = Path(artifacts_folder.name) / create_guid()
screenshot_path = (
Path(self._pw_artifacts_folder.name) / create_guid()
)
page.screenshot(
timeout=5000,
path=screenshot_path,
Expand Down
25 changes: 25 additions & 0 deletions tests/test_playwright.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,31 @@ def test_d(page):
assert "gw1" in "\n".join(result.outlines)


def test_xdist_should_not_print_any_warnings(testdir: pytest.Testdir) -> None:
original = os.environ.get("PYTHONWARNINGS")
os.environ["PYTHONWARNINGS"] = "always"
try:
testdir.makepyfile(
"""
import pytest

def test_default(page):
pass
"""
)
result = testdir.runpytest(
"--numprocesses",
"2",
)
result.assert_outcomes(passed=1)
assert "ResourceWarning" not in "".join(result.stderr.lines)
finally:
if original is not None:
os.environ["PYTHONWARNINGS"] = original
else:
del os.environ["PYTHONWARNINGS"]


def test_headed(testdir: pytest.Testdir) -> None:
testdir.makepyfile(
"""
Expand Down
Loading