---
title: Integrating screenshots in Allure Report with Pytest and Playwright
description: The guide describes how you can integrate the screenshots in Allure Report with Pytest and Playwright
---

# Integrating screenshots in Allure Report with Pytest and Playwright

Interpreting test results should be fast and straightforward, allowing the root cause of a failure to be immediately clear without requiring code inspection, debugging, or external assistance.

To achieve this, test reports must present detailed information in a concise and accessible format. Incorporating screenshots is an effective way to provide visual context for test failures. This guide explains how to capture screenshots in Pytest with Playwright and integrate them into Allure Report to enhance readability and analysis.

## 1. Preparation

### Prerequisites

Make sure the following prerequisites are met:

- [Python installed](https://www.python.org/downloads/)
- [Allure Report installed](/docs/v2/install/)

### Dependency list

This guide uses the following packages:

- [Pytest 8.3.4](https://pypi.org/project/pytest/8.3.4/)
- [allure-pytest 2.13.5](https://pypi.org/project/allure-pytest/2.13.5/)
- [Pytest-playwright 0.7.0](https://pypi.org/project/pytest-playwright/0.7.0/)

### Code sample

The complete source code used in this guide is available at [https://github.com/allure-examples/guide-pytest-playwright-screenshots](https://github.com/allure-examples/guide-pytest-playwright-screenshots).

### Setup

To run the examples in this guide:

1. Install [Python](https://www.python.org/downloads/)
1. Install [Allure Report](/docs/v2/install/)
1. Download a fresh project with Pytest from [Allure Start](/start/)
1. Add [the Playwright Pytest plugin](https://playwright.dev/python/docs/intro#installing-playwright-pytest) to your project. For example, if you use `pip`:

**Install Playwright Pytest:**
```shell
pip install pytest-playwright
```

## 2. Screenshots with Playwright Pytest

To take a screenshot with Playwright Pytest at an arbitrary time, use the [`screenshot`](https://playwright.dev/python/docs/api/class-page#page-screenshot) method provided by the `Page` object. To get the `Page` object, you can use the `page` fixture:

```python
def test_example(page):
    page.goto("https://playwright.dev/")
    try:
        assert "Playwright" in page.title()
    except AssertionError:
        page.screenshot(path="path/to/screenshot.png")
        raise
```

To take a screenshot of a particular element, use the [`screenshot`](https://playwright.dev/python/docs/api/class-locator#locator-screenshot) method of the `Locator` object, for example:

```python
element = page.get_by_role("search") # or however you find the element
# take the screenshot
element.screenshot(path="path/to/screenshot.png")
```

## 3. Automatic screenshots on failure

To take screenshots automatically of every test failure, set the [`--screenshot`](https://playwright.dev/python/docs/test-runners#cli-arguments) option to `only-on-failure` in your project's [Pytest configuration](https://pytest.org/en/latest/reference/customize.html#command-line-options-and-configuration-file-settings). For example, if you use `pyproject.toml` as a configuration file, add the following:

```toml
[tool.pytest.ini_options]
...
addopts = [
    ...
    "--screenshot", "only-on-failure"
]
```

Now, when a test fails, a screenshot file will be saved automatically in the test results directory of your project. Playwright Pytest deletes and recreates the directory with each test run.

## 4. Integrating with Allure Report

While capturing screenshots is beneficial for diagnosing test failures, viewing them separately can be inefficient. When test data is dispersed across multiple locations, the process of identifying and resolving failures becomes more time-consuming.

To streamline this workflow, screenshots should be directly attached to Allure Reports, ensuring all relevant test information is consolidated in a single location for efficient analysis and troubleshooting.

Allure Report shows you screenshots inside the test in which they were taken. That is also where the error messages and other attachments are presented to you, so you have everything you need to analyze test results in one place:

![Screenshot Attachment in Allure Report](/images/guides/pytest-screenshots/screesnshot_in_scenario.png "Screenshot Attachment in Allure Report")

To attach screenshots to Allure, use either the [`allure.attach()`](/docs/pytest-reference/#attaching-content-from-variables) or the [`allure.attach.file()`](/docs/pytest-reference/#reading-attachments-from-files) functions.

Use `allure.attach.file()` to attach an existing file:

```python
import allure
from pathlib import Path

# Attach a screenshot of a page or an element
def attach_screenshot(pageOrElement, path):
    name = Path(path).name
    pageOrElement.screenshot(path=path)
    allure.attach.file(
        path,
        name=name,
        attachment_type=allure.attachment_type.PNG,
    )
```

Use `allure.attach()` if you don’t need to save the screenshot on disk:

```python
import allure

# Attach a screenshot of a page or an element
def attach_screenshot(pageOrElement, name):
    allure.attach(
        pageOrElement.screenshot(),
        name=name,
        attachment_type=allure.attachment_type.PNG,
    )
```

Tip:

When you need to attach a screenshot you just took, prefer using the `allure.attach` function if possible. The reason is that when you save a screenshot on disk, it may not always be immediately available for reading because of OS caching. If that happens, you may end up with an empty file attached to Allure.

If you also set the `--screenshot` configuration option as described in [**Automatic screenshots on failure**](#_3-automatic-screenshots-on-failure) section, Playwright Pytest will save screenshots locally.

To automatically attach screenshots to Allure on each test failure, you can add a hook in the `conftest.py` file in your project's test root directory. The hook may look up the screenshots Playwright Pytest saved after the failed tests and attach these screenshots to Allure Report. For example:

```python
import re
import allure
import pytest

from pathlib import Path

# A regular expression to match the file names of the screenshots taken on test failures
SCREENSHOT_NAME_PATTERN = re.compile(r"^test-failed-\d+\.png$")

@pytest.hookimpl(hookwrapper=True)
def pytest_runtest_teardown(item, nextitem):
    yield

    try:
        artifacts_dir = item.funcargs.get("output_path")
        if artifacts_dir:
            artifacts_dir_path = Path(artifacts_dir)
            if artifacts_dir_path.is_dir():
                for file in artifacts_dir_path.iterdir():
                    if file.is_file() and SCREENSHOT_NAME_PATTERN.match(file.name):
                        allure.attach.file(
                            str(file),
                            name=file.name,
                            attachment_type=allure.attachment_type.PNG,
                        )
    except Exception as e:
        print(f"Error taking screenthot: {e}")
```

## 5. Other attachment types

Allure Report can also handle other types of attachments - text, tables, URI lists, or documents (XML, JSON, YAML). For example:

```python
allure.attach(
    response_content,
    name="response content",
    attachment_type=allure.attachment_type.XML
)
```

Allure Pytest makes some attachments automatically:

1. everything passed to `sys.stdout` - e.g. `print(...)`
2. everything passed to `sys.stderr` - e.g. `print(..., file=sys.stderr)`
3. logging, e.g. `logging.warning(...)`

These outputs are attached as pseudo-files to the report:

![Automatic Attachments In Allure Report With Pytest](/images/guides/pytest-screenshots/attachment_pytest_auto.png "Automatic Attachments In Allure Report With Pytest")

If you don’t want all of this to appear in the report, use the [`--allure-no-capture`](/docs/pytest-configuration/#allure-no-capture) option.

Allure also allows you to [attach videos](/docs/attachments/#videos). You can consult the official documentation for more on [attachments with Pytest](/docs/pytest/#attach-screenshots-and-other-files) and [attachment types](/docs/attachments/).

## 6. Conclusion

With Pytest and Playwright, you can capture screenshots at any point during your tests or configure Playwright to take them automatically upon test failures. Allure Report combines these screenshots along with other attachments—such as text, documents, tables, and videos—into a single view, providing comprehensive data to help diagnose test failures efficiently.
