Skip to content

Commit 87cdda6

Browse files
committed
feat: make a screenshot if a test is failing
1 parent 61ad69e commit 87cdda6

File tree

4 files changed

+44
-1
lines changed

4 files changed

+44
-1
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ Possible values: `chromium`, `firefox`, `webkit`
7171

7272
By default, the tests run in headless mode. You can pass the `--headful` CLI flag to run the browser in headful mode.
7373

74+
### `--screenshot-on-error`
75+
76+
By using the `--screenshot-on-error` argument for each failing test the screenshots will be stored in the `pytest_playwright` directory to upload them e.g. on your CI provider as an asset for debugging purposes.
77+
7478
## Examples
7579

7680
### Skipping by browser type

pytest_playwright/pytest_playwright.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import asyncio
1818

1919
import pytest
20+
from pathlib import Path
21+
from slugify import slugify
2022

2123
from playwright import sync_playwright
2224
from playwright.sync_api import Browser, BrowserContext, Page
@@ -42,6 +44,17 @@ def pytest_configure(config: Any) -> None:
4244
)
4345

4446

47+
def pytest_runtest_makereport(item: Any, call: Any) -> None:
48+
if call.when == "call":
49+
if call.excinfo is not None:
50+
config = item.funcargs["pytestconfig"]
51+
if config.getoption("--screenshot-on-error"):
52+
page: Page = item.funcargs["page"]
53+
screenshot_dir = Path(".playwright-screenshots")
54+
screenshot_dir.mkdir(exist_ok=True)
55+
page.screenshot(path=screenshot_dir / f"{slugify(item.nodeid)}.png")
56+
57+
4558
def _get_skiplist(request: Any, values: List[str], value_name: str) -> List[str]:
4659
skipped_values: List[str] = []
4760
# Allowlist
@@ -174,3 +187,9 @@ def pytest_addoption(parser: Any) -> None:
174187
default=False,
175188
help="Run tests in headful mode.",
176189
)
190+
parser.addoption(
191+
"--screenshot-on-error",
192+
action="store_true",
193+
default=False,
194+
help="Saves a screenshot in the '.playwright-screenshots' directory if a test is failing",
195+
)

setup.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313
url="https://github.com/microsoft/playwright-pytest",
1414
packages=["pytest_playwright"],
1515
include_package_data=True,
16-
install_requires=["playwright>=0.0.4", "pytest", "pytest-base-url"],
16+
install_requires=[
17+
"playwright>=0.0.4",
18+
"pytest",
19+
"pytest-base-url",
20+
"python-slugify==4.*",
21+
],
1722
entry_points={"pytest11": ["playwright = pytest_playwright.pytest_playwright"]},
1823
classifiers=[
1924
"Programming Language :: Python :: 3",

tests/test_playwright.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
# limitations under the License.
1414

1515
from typing import Any
16+
import os
17+
from pathlib import Path
1618

1719

1820
def test_default(testdir: Any) -> None:
@@ -177,3 +179,16 @@ def test_base_url(page):
177179
result = testdir.runpytest("--browser", "test123")
178180
result.assert_outcomes(errors=1)
179181
assert "'test123' is not allowed" in "\n".join(result.outlines)
182+
183+
184+
def test_make_screenshot(testdir: Any) -> None:
185+
testdir.makepyfile(
186+
"""
187+
def test_my_test(page):
188+
raise ValueError("test123")
189+
"""
190+
)
191+
result = testdir.runpytest("--screenshot-on-error")
192+
result.assert_outcomes(failed=1)
193+
screenshot_files = os.listdir(Path(str(testdir)) / ".playwright-screenshots")
194+
assert screenshot_files == ["test-make-screenshot-py-test-my-test-chromium.png"]

0 commit comments

Comments
 (0)