tiny-match-snapshot


Nametiny-match-snapshot JSON
Version 0.0.2 PyPI version JSON
download
home_pageNone
SummaryTiny snapshot assertion for tests
upload_time2024-04-10 22:51:09
maintainerNone
docs_urlNone
authorNone
requires_python>=3.8
licenseMIT License Copyright (c) 2024 Eduardo Oliveira Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
keywords snapshot test image match assertion playwright selenium
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            <h1 align="center">tiny-match-snapshot</h1>

<p align="center">
    <img alt="Supported Python Versions" src="https://img.shields.io/badge/Python-3.8+-blue" />
    <img alt="PyPI - Version" src="https://img.shields.io/pypi/v/tiny-match-snapshot" />
    <img alt="GitHub License" src="https://img.shields.io/github/license/inventare/tiny-match-snapshot" />
    <img alt="PyPI - Downloads" src="https://img.shields.io/pypi/dm/tiny-match-snapshot" />
    <img alt="GitHub Actions Workflow Status" src="https://img.shields.io/github/actions/workflow/status/inventare/tiny-match-snapshot/tests.yml?label=tests" />
</p>

This package provides a tiny match snaptshot utility for usages with **Playwright** or with **Selenium** to capture screenshot of some web element and store it as snapshot and in next runs of tests, compare the taken screenshot with the stored as snapshot.

## Requirements

- Python 3.8+
- Selenium or Playwright (or any other, see "Extending the behaviour" section)

## Instalation

The package can be installed with the `pip` package manager:

```sh
pip install tiny-match-snapshot
```

## Usage

Some examples of usage is disponible at `examples` folder on the root of this repository. For more complex usages, this package is used to run UI regression tests on the [inventare/django-image-uploader-widget](https://github.com/inventare/django-image-uploader-widget/tree/main/tests/tests_ui_regression) package.

### With unittest and playwright

A first way, using `unittest` and `playwright` is:

```python
import unittest
from unittest.case import TestCase
from match_snapshot import MatchSnapshot
from playwright.sync_api import sync_playwright

class MyTests(TestCase, MatchSnapshot):
    snapshot_path = "./__snapshots__"
    failed_path = "./__errors__"

    def test_selenium(self):
        playwright = sync_playwright().start()
        browser = playwright.chromium.launch()
        page = browser.new_page()
        page.goto("https://playwright.dev/")

        banner = page.query_selector(".hero.hero--primary")

        self.assert_match_snapshot(banner, "test_unittest_playwright")

        browser.close()

if __name__ == '__main__':
    unittest.main()
```

### With unittest and selenium

An simple example of using `unittest` and `selenium` is:

```python
import unittest
from unittest.case import TestCase
from match_snapshot import MatchSnapshot
from selenium import webdriver
from selenium.webdriver.common.by import By

class MyTests(TestCase, MatchSnapshot):
    snapshot_path = "./__snapshots__"
    failed_path = "./__errors__"

    def test_selenium(self):
        driver = webdriver.Chrome()
        driver.get("http://www.python.org")

        banner = driver.find_element(By.CLASS_NAME, "main-header")

        self.assert_match_snapshot(banner, "test_unittest_selenium")

if __name__ == '__main__':
    unittest.main()
```

### With pytest and playwright

To use with `pytest` and `playwright` we can encapsulate our tests inside a class to inherits the `MatchSnapshot` class. To run the tests, we recommend to use the plugin [pytest test runner](https://playwright.dev/python/docs/test-runners) and run the tests using the command: `pytest --browser webkit --headed`.

```python
from playwright.sync_api import Page
from match_snapshot import MatchSnapshot

class TestsPlaywright(MatchSnapshot):
    snapshot_path = "./__snapshots__"
    failed_path = "./__errors__"

    def test_playwright(self, page: Page):
        page.goto("https://playwright.dev/")

        element = page.query_selector('.hero.hero--primary')
        self.assert_match_snapshot(element, 'test_pytest_playwright')
```

### With pytest and selenium

A, not beautifull way to use with `pytest` and `selenium` is to use:

```python
from match_snapshot import MatchSnapshot
from selenium import webdriver
from selenium.webdriver.common.by import By

class TestsSelenium(MatchSnapshot):
    snapshot_path = "./__snapshots__"
    failed_path = "./__errors__"

    def test_selenium(self):
        driver = webdriver.Chrome()
        driver.get("http://www.python.org")

        banner = driver.find_element(By.CLASS_NAME, "main-header")

        self.assert_match_snapshot(banner, "test_pytest_selenium")
```

## Extending the behaviour

The basic behaviour of this library is the usage of the `.screenshot()` method of a web element. To extends the behaviour of the package to be used with other tool is basically write a wrapper class to compose with the element:

```python
class MyCustomElement:
    def __init__(self, element):
        self.element = element

    def screenshot(self, path: str):
        # TODO: TAKE the screenshot and save it to the path
        pass
```

Now, instead of use the API element to the `assert_match_snapshot()` we use the composed `MyCustomElement` class:

```python
class MyTestCase(MatchSnapshot, ...)
    def test_any_thing(self):
        element = ...

        match_element = MyCustomElement(element)
        self.assert_match_snapshot(match_element, 'my_test')
```

            

Raw data

            {
    "_id": null,
    "home_page": null,
    "name": "tiny-match-snapshot",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": "Eduardo Oliveira <eduardo_y05@outlook.com>",
    "keywords": "snapshot, test, image, match, assertion, playwright, selenium",
    "author": null,
    "author_email": "Eduardo Oliveira <eduardo_y05@outlook.com>",
    "download_url": "https://files.pythonhosted.org/packages/d1/55/7ecf3d0707cfc50c3a0dff4d4076ad76399e1de0259bcaad645edc76c21f/tiny-match-snapshot-0.0.2.tar.gz",
    "platform": null,
    "description": "<h1 align=\"center\">tiny-match-snapshot</h1>\n\n<p align=\"center\">\n    <img alt=\"Supported Python Versions\" src=\"https://img.shields.io/badge/Python-3.8+-blue\" />\n    <img alt=\"PyPI - Version\" src=\"https://img.shields.io/pypi/v/tiny-match-snapshot\" />\n    <img alt=\"GitHub License\" src=\"https://img.shields.io/github/license/inventare/tiny-match-snapshot\" />\n    <img alt=\"PyPI - Downloads\" src=\"https://img.shields.io/pypi/dm/tiny-match-snapshot\" />\n    <img alt=\"GitHub Actions Workflow Status\" src=\"https://img.shields.io/github/actions/workflow/status/inventare/tiny-match-snapshot/tests.yml?label=tests\" />\n</p>\n\nThis package provides a tiny match snaptshot utility for usages with **Playwright** or with **Selenium** to capture screenshot of some web element and store it as snapshot and in next runs of tests, compare the taken screenshot with the stored as snapshot.\n\n## Requirements\n\n- Python 3.8+\n- Selenium or Playwright (or any other, see \"Extending the behaviour\" section)\n\n## Instalation\n\nThe package can be installed with the `pip` package manager:\n\n```sh\npip install tiny-match-snapshot\n```\n\n## Usage\n\nSome examples of usage is disponible at `examples` folder on the root of this repository. For more complex usages, this package is used to run UI regression tests on the [inventare/django-image-uploader-widget](https://github.com/inventare/django-image-uploader-widget/tree/main/tests/tests_ui_regression) package.\n\n### With unittest and playwright\n\nA first way, using `unittest` and `playwright` is:\n\n```python\nimport unittest\nfrom unittest.case import TestCase\nfrom match_snapshot import MatchSnapshot\nfrom playwright.sync_api import sync_playwright\n\nclass MyTests(TestCase, MatchSnapshot):\n    snapshot_path = \"./__snapshots__\"\n    failed_path = \"./__errors__\"\n\n    def test_selenium(self):\n        playwright = sync_playwright().start()\n        browser = playwright.chromium.launch()\n        page = browser.new_page()\n        page.goto(\"https://playwright.dev/\")\n\n        banner = page.query_selector(\".hero.hero--primary\")\n\n        self.assert_match_snapshot(banner, \"test_unittest_playwright\")\n\n        browser.close()\n\nif __name__ == '__main__':\n    unittest.main()\n```\n\n### With unittest and selenium\n\nAn simple example of using `unittest` and `selenium` is:\n\n```python\nimport unittest\nfrom unittest.case import TestCase\nfrom match_snapshot import MatchSnapshot\nfrom selenium import webdriver\nfrom selenium.webdriver.common.by import By\n\nclass MyTests(TestCase, MatchSnapshot):\n    snapshot_path = \"./__snapshots__\"\n    failed_path = \"./__errors__\"\n\n    def test_selenium(self):\n        driver = webdriver.Chrome()\n        driver.get(\"http://www.python.org\")\n\n        banner = driver.find_element(By.CLASS_NAME, \"main-header\")\n\n        self.assert_match_snapshot(banner, \"test_unittest_selenium\")\n\nif __name__ == '__main__':\n    unittest.main()\n```\n\n### With pytest and playwright\n\nTo use with `pytest` and `playwright` we can encapsulate our tests inside a class to inherits the `MatchSnapshot` class. To run the tests, we recommend to use the plugin [pytest test runner](https://playwright.dev/python/docs/test-runners) and run the tests using the command: `pytest --browser webkit --headed`.\n\n```python\nfrom playwright.sync_api import Page\nfrom match_snapshot import MatchSnapshot\n\nclass TestsPlaywright(MatchSnapshot):\n    snapshot_path = \"./__snapshots__\"\n    failed_path = \"./__errors__\"\n\n    def test_playwright(self, page: Page):\n        page.goto(\"https://playwright.dev/\")\n\n        element = page.query_selector('.hero.hero--primary')\n        self.assert_match_snapshot(element, 'test_pytest_playwright')\n```\n\n### With pytest and selenium\n\nA, not beautifull way to use with `pytest` and `selenium` is to use:\n\n```python\nfrom match_snapshot import MatchSnapshot\nfrom selenium import webdriver\nfrom selenium.webdriver.common.by import By\n\nclass TestsSelenium(MatchSnapshot):\n    snapshot_path = \"./__snapshots__\"\n    failed_path = \"./__errors__\"\n\n    def test_selenium(self):\n        driver = webdriver.Chrome()\n        driver.get(\"http://www.python.org\")\n\n        banner = driver.find_element(By.CLASS_NAME, \"main-header\")\n\n        self.assert_match_snapshot(banner, \"test_pytest_selenium\")\n```\n\n## Extending the behaviour\n\nThe basic behaviour of this library is the usage of the `.screenshot()` method of a web element. To extends the behaviour of the package to be used with other tool is basically write a wrapper class to compose with the element:\n\n```python\nclass MyCustomElement:\n    def __init__(self, element):\n        self.element = element\n\n    def screenshot(self, path: str):\n        # TODO: TAKE the screenshot and save it to the path\n        pass\n```\n\nNow, instead of use the API element to the `assert_match_snapshot()` we use the composed `MyCustomElement` class:\n\n```python\nclass MyTestCase(MatchSnapshot, ...)\n    def test_any_thing(self):\n        element = ...\n\n        match_element = MyCustomElement(element)\n        self.assert_match_snapshot(match_element, 'my_test')\n```\n",
    "bugtrack_url": null,
    "license": "MIT License  Copyright (c) 2024 Eduardo Oliveira  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ",
    "summary": "Tiny snapshot assertion for tests",
    "version": "0.0.2",
    "project_urls": {
        "documentation": "https://github.com/inventare/tiny-match-snapshot/",
        "homepage": "https://github.com/inventare/tiny-match-snapshot/"
    },
    "split_keywords": [
        "snapshot",
        " test",
        " image",
        " match",
        " assertion",
        " playwright",
        " selenium"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e811022f40f8f3a125334451ac2efcb5909cd573e399c9d05833a99f706e34b3",
                "md5": "4eadca7236988b3dc12be8db8df8aac3",
                "sha256": "70d4b09d1064986aaaf26a71c27e3a1caca521964772ad30406401d4141a2e20"
            },
            "downloads": -1,
            "filename": "tiny_match_snapshot-0.0.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "4eadca7236988b3dc12be8db8df8aac3",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 6422,
            "upload_time": "2024-04-10T22:51:07",
            "upload_time_iso_8601": "2024-04-10T22:51:07.994276Z",
            "url": "https://files.pythonhosted.org/packages/e8/11/022f40f8f3a125334451ac2efcb5909cd573e399c9d05833a99f706e34b3/tiny_match_snapshot-0.0.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d1557ecf3d0707cfc50c3a0dff4d4076ad76399e1de0259bcaad645edc76c21f",
                "md5": "b14e5ac4ca20ecad638dd3906016d9ec",
                "sha256": "88139d1e639f2381d2d7e643eaa5dde60a8885ad58a12304e294d9e5893ee58d"
            },
            "downloads": -1,
            "filename": "tiny-match-snapshot-0.0.2.tar.gz",
            "has_sig": false,
            "md5_digest": "b14e5ac4ca20ecad638dd3906016d9ec",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 6696,
            "upload_time": "2024-04-10T22:51:09",
            "upload_time_iso_8601": "2024-04-10T22:51:09.600411Z",
            "url": "https://files.pythonhosted.org/packages/d1/55/7ecf3d0707cfc50c3a0dff4d4076ad76399e1de0259bcaad645edc76c21f/tiny-match-snapshot-0.0.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-10 22:51:09",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "inventare",
    "github_project": "tiny-match-snapshot",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "lcname": "tiny-match-snapshot"
}
        
Elapsed time: 0.22110s