|
1 | | -# Pytest plugin for visual testing |
| 1 | +# pytest-assert-screenshot |
2 | 2 |
|
3 | | -Based on [pixelmatch-py](https://github.com/whtsky/pixelmatch-py) image comparison library. |
| 3 | +Этот плагин - форк плагина [pytest-playwright-visual](https://github.com/symon-storozhenko/pytest-playwright-visual). |
4 | 4 |
|
5 | | -## Main Features: |
6 | | -- snapshots creation on the first run |
7 | | -- visual review of mismatches |
8 | | -- failing on `--update-screenshots` to make users manually review images |
9 | | -- snapshot name is optional, `test_name.png` is auto-generated by default |
10 | | -- updated folder structure: `snapshots/file_name/test_name/test_name.png` |
| 5 | +Плагин предназначен для сравнения скриншотов при UI-тестировании веб-приложений. Плагин может быть исопльзован с любым UI-фреймворком. |
11 | 6 |
|
12 | | -## Installation |
| 7 | +В плагине добавлена возможность задавать допустимое количество отличающихся пикселей на скриншоте. Это помогает сделать тесты более стабильными. |
13 | 8 |
|
14 | | -```bash |
15 | | -$ pip install pytest-playwright-visual |
16 | | -``` |
17 | | - |
18 | | -## Usage |
19 | | - |
20 | | -This plugin provides a `assert_screenshot` fixture which is used to create snapshots and compare it. |
21 | | - |
22 | | -Example: |
23 | | - |
24 | | -```python |
25 | | -def test_myapp(page, assert_snapshot): |
26 | | - page.goto("https://example.com") |
27 | | - assert_snapshot(page.screenshot()) |
28 | | -``` |
29 | | -Then, run pytest: |
30 | | -```bash |
31 | | -$ pytest |
32 | | -``` |
33 | | -The first time you run pytest, snapshots will be created, and you will get the error: |
34 | | - |
35 | | -```console |
36 | | -Failed: --> New snapshot(s) created. Please review images |
37 | | -``` |
38 | | - |
39 | | -The next run, the snapshots comparison will take place. |
40 | | - |
41 | | -To update snapshots, run: |
42 | | - |
43 | | -```bash |
44 | | -$ pytest --update-screenshots |
45 | | -``` |
46 | | - |
47 | | -After updating, tests will fail and you will need to review images. |
48 | | - |
49 | | -In case of a mismatch, `screenshot_tests_failures` folder will be created with `Actual_..`, `Expected_..` and `Diff_..` images generated. |
50 | | - |
51 | | -## Folder Structure Example |
52 | | - |
53 | | - |
54 | | - |
55 | | -## API |
56 | | -**assert_snapshot(page.screenshot(), threshold: float = 0.1, name='test_name[browser][os].png', fail_fast=False)** |
57 | | -- `threshold` - sets the threshold for the comparison of the screenshots:`0` to `1`. Default is `0.1` |
58 | | -- `name` - `.png` extensions only. Default is `test_name.png` (recommended) |
59 | | -- `fail_fast` - If `True`, will fail after first different pixel. `False` by default |
60 | | - |
61 | | -## Change snapshots path |
62 | | - |
63 | | -You can change the default path where snapshots are stored by setting `pytest.snapshots_path` and/or |
64 | | -`pytest.screenshot_failures_path` value in `pytest_configure()` hook in your root `conftest.py`: |
| 9 | +После установки пакета в тестах с использованием библиотеки pytest становится доступна фикстура assert-screenshot: |
65 | 10 | ```python |
66 | | -def pytest_configure(): |
67 | | - pytest.snapshots_path = Path.cwd() / "screenshots" |
68 | | - pytest.snapshot_failures_path = Path.cwd() / "screenshots_failures" |
69 | | -``` |
70 | | - |
71 | | -## License |
72 | | - |
73 | | -Apache 2.0 LICENSE |
| 11 | +class TestExamplesVisual: |
| 12 | + def test_visual_auth_page(self, ui_client: UIClient, assert_screenshot): |
| 13 | + """Визуальный тест страницы авторизации""" |
| 14 | + # ARRANGE |
| 15 | + ui_client.page_auth.navigate() |
| 16 | + # ACT |
| 17 | + screenshot_page_auth = ui_client.page_auth.make_screenshot() |
| 18 | + # ASSERT |
| 19 | + screenshot_name = OperationsFile.generate_screenshot_name() |
| 20 | + logger.debug('Имя базового скриншота = ' + screenshot_name) |
| 21 | + assert_screenshot( |
| 22 | + screenshot_page_auth, |
| 23 | + threshold=0.12, |
| 24 | + name=screenshot_name, |
| 25 | + fail_fast=False, |
| 26 | + ) |
| 27 | +``` |
0 commit comments