playwright-pytest icon indicating copy to clipboard operation
playwright-pytest copied to clipboard

[BUG] Pytest.ini set --screenshot="on" it did not capture the screenshot after each test

Open old-eight800 opened this issue 2 years ago • 5 comments

Context:

  • Playwright Version: playwright-0.3.0
  • Operating System: Windows
  • Python version: Python 3.9.6
  • Browser: Chromium
  • Extra: pytest-7.1.3

Code Snippet

the content of conftest.py

import pytest
from playwright.sync_api import Page, expect
from loguru import logger

@pytest.fixture(scope="session", autouse=True)
def PCL_before_each_after_each(page: Page):
    logger.info("start PCL UI auto test")
    # Go to the starting url before each test.
    page.goto("#############")
    page.wait_for_timeout(1000)
    yield
    logger.info("end PCL UI auto test")

@pytest.fixture(scope="session")
def browser_context_args(browser_context_args):
    """  """
    return {
        **browser_context_args,
        "viewport": {
            "width": 375,
            "height": 667,
        }
    }

the content of pytest.ini """python [pytest] addopts= -vsx --screenshot="on" --video="on" -p no:warnings """

Describe the bug

when the test done, it should capture 5 screenshots as I have 5 test cases, but there was only one and filename is 'test-failed-1.png'

old-eight800 avatar Nov 16 '22 05:11 old-eight800

same as #139 ?

soprano8086 avatar Dec 02 '22 01:12 soprano8086

In my case I want to take screenshot when test fail. Not being able to take screen shot when tests fail is a show stopper for us. It would be nice to know whether this will be fixed so I can decide whether we continue with Playwright.

soprano8086 avatar Dec 02 '22 02:12 soprano8086

I verify that --screenshot option doesn't work as expected. No screenshot artifacts under test-results no matter what --output and/or --headed we provide. But its works under the only condition: there is no browser.new_context() in the conftest.py

pytest --video on --screenshot on
ls -R test-results
dd16957a85f2926cd1a13ff3c4965803.webm

storenth avatar Feb 15 '23 13:02 storenth

@mxschmitt looks like the related changes were here

storenth avatar Feb 15 '23 14:02 storenth

Sorry for the late reply! I tried the following which was working for me as expected:

import pytest
from playwright.sync_api import Page

@pytest.fixture(scope="function", autouse=True)
def PCL_before_each_after_each(page: Page):
    print("start PCL UI auto test")
    # Go to the starting url before each test.
    page.goto("http://example.com")
    yield
    print("end PCL UI auto test")

@pytest.fixture(scope="session")
def browser_context_args(browser_context_args):
    """  """
    return {
        **browser_context_args,
        "viewport": {
            "width": 375,
            "height": 667,
        }
    }

def test_1(page):
    pass

def test_2(page):
    pass

def test_3(page):
    pass

def test_4(page):
    pass

def test_5(page):
    pass

Command: pytest -vsx --screenshot="on" --video="on" -p no:warnings

Gives me:

@mxschmitt ➜ /workspaces/playwright-pytest-bug (main) $ tree test-results/
test-results/
├── tests-test-browser-py-test-1-chromium
│   ├── f238538e36ae458740b9f80d06285d26.webm
│   └── test-finished-1.png
├── tests-test-browser-py-test-2-chromium
│   ├── d9e51b7de27dbce4d083045048f7a450.webm
│   └── test-finished-1.png
├── tests-test-browser-py-test-3-chromium
│   ├── 8bfbd3d67bd848d1782a7067aa60d98a.webm
│   └── test-finished-1.png
├── tests-test-browser-py-test-4-chromium
│   ├── 583ebeaae4d539e2741dee79a587a76d.webm
│   └── test-finished-1.png
└── tests-test-browser-py-test-5-chromium
    ├── 784b2471f53e2fed12e21bbc4b358daa.webm
    └── test-finished-1.png

5 directories, 10 files

I see that the PCL_before_each_after_each is incorrectly scoped, so most likely you are not using pytest-playwright's page, hence its not recording the videos/screenshots, since thats not working for manual created context's.

mxschmitt avatar Oct 10 '23 14:10 mxschmitt

Closing as per above. Feel free to re-file for further bug reports. Thank you!

mxschmitt avatar Apr 25 '24 09:04 mxschmitt