Giter Site home page Giter Site logo

Comments (6)

paunovic avatar paunovic commented on May 22, 2024 1

Hi, here is minimal reproducible example:

conftest.py:

import os
import tempfile
from typing import Any, Callable, Dict, Generator, List

import pytest
from playwright.sync_api import (
    Browser,
    BrowserContext,
    Page,
    Playwright,
    sync_playwright,
)


artifacts_folder = tempfile.TemporaryDirectory(prefix="playwright-pytest-")
print(f"{artifacts_folder.name=}")


@pytest.fixture()
def browser_context_args(
    pytestconfig: Any,
    playwright: Playwright,
) -> Dict:
    context_args = {"record_video_dir": artifacts_folder}
    return context_args


@pytest.fixture(scope="session")
def playwright() -> Generator[Playwright, None, None]:
    pw = sync_playwright().start()
    yield pw
    pw.stop()


@pytest.fixture(scope="session")
def browser(launch_browser: Callable[[], Browser]) -> Generator[Browser, None, None]:
    browser = launch_browser()
    yield browser
    browser.close()


@pytest.fixture
def context(
    browser: Browser,
    browser_context_args: Dict,
    pytestconfig: Any,
) -> Generator[BrowserContext, None, None]:
    pages: List[Page] = []
    context = browser.new_context(**browser_context_args)
    context.on("page", lambda p: pages.append(p))

    yield context

    context.close()

    for page in pages:
        video = page.video
        video_path = str(video.path())
        print(f"{video_path=}")
        file_name = os.path.basename(video_path)
        video.save_as(f"/tmp/{file_name}")


@pytest.fixture
def page(context: BrowserContext, base_url: str) -> Generator[Page, None, None]:
    page = context.new_page()
    yield page

test_bug.py:

from playwright.sync_api import Page


def test_bug(page: Page):
    page.goto("https://playwright.dev/python/docs/intro/")

Running on Python 3.8.1, ubuntu/bionic64. If I change following line:

context_args = {"record_video_dir": artifacts_folder}

to:

context_args = {"record_video_dir": artifacts_folder.name}

It starts working.

from playwright-pytest.

paunovic avatar paunovic commented on May 22, 2024 1

Just for posterity, the bug that I was encountering few weeks ago when I initially opened this issue was different - TemporaryDirectory was returning underscore instead of dash at one place in the string for some reason, compared to video.path(). Looks like there are few buggy behaviours. Personally I refactored my fixtures away from TemporaryDirectory.

Oh and there's also a typo in temp directory name, playwight.

from playwright-pytest.

paunovic avatar paunovic commented on May 22, 2024

Looks like a bug with tempfile library where tempfile.Directory() returns slightly incorrect path, resulting videos not to be moved from artifacts dir to output dir.

from playwright-pytest.

JoelEinbinder avatar JoelEinbinder commented on May 22, 2024

Transferred to the playwright-pytest repo. Looks like a bug with our pytest fixtures. PRs welcome if you know how to fix it, otherwise @mxschmitt can you take a look?

from playwright-pytest.

mxschmitt avatar mxschmitt commented on May 22, 2024

@paunovic sorry for the late reply. Do you have a full example somewhere (e.g. GitHub repo)? I tried to confirm it and reproduce it locally, but for me it seems working as intended.

from playwright-pytest.

mxschmitt avatar mxschmitt commented on May 22, 2024

Ohhh good catch! Thats the fix for it: #83. Will release it shortly.

from playwright-pytest.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.