Skip to content

[BUG] Playwright produces zero-bytes .webm videos #81

Closed
@paunovic

Description

@paunovic

Context:

  • Playwright Version: Version 1.15.0-1631797286000
  • Operating System: ubuntu/bionic64
  • Node.js version: v12.16.1
  • Browser: All
  • Extra: running inside virtualbox vm, host OS is Windows 10.

Code Snippet

@pytest.fixture(autouse=True)
def browser_context_args(browser_context_args: dict) -> dict:

    browser_context_args = {
        **browser_context_args,
        "viewport": {
            "width": 1920,
            "height": 1080,
        },
        "device_scale_factor": 2,
        "record_video_size": {
            "width": 800,
            "height": 600,
        },
    }

    return browser_context_args

Describe the bug

I am running Playwright + Python + pytest-playwright with following arguments:

pytest --tracing retain-on-failure --screenshot only-on-failure --video retain-on-failure

On error, tracing and screenshots are saved fine, however videos come out as zero bytes .webm files.

Activity

paunovic

paunovic commented on Oct 3, 2021

@paunovic
Author

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.

JoelEinbinder

JoelEinbinder commented on Oct 8, 2021

@JoelEinbinder

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?

mxschmitt

mxschmitt commented on Oct 8, 2021

@mxschmitt
Member

@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.

paunovic

paunovic commented on Oct 11, 2021

@paunovic
Author

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.

mxschmitt

mxschmitt commented on Oct 11, 2021

@mxschmitt
Member

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

paunovic

paunovic commented on Oct 11, 2021

@paunovic
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      Participants

      @paunovic@JoelEinbinder@mxschmitt

      Issue actions

        [BUG] Playwright produces zero-bytes .webm videos · Issue #81 · microsoft/playwright-pytest