Search code examples
python-3.xselenium-webdriverpytestscreenshotintegrated

How can we implement capturing of screenshots on failure in integrated framework(UI + API) using pytest framework?


Here, I have integrated framework like (API + UI) in single framework using pytest and I want to use below hook with pytest framework:

@pytest.mark.hookwrapper
def pytest_runtest_makereport(item, call)

but when I'm using above hook in conftest.py file, it's launching web browser and saving screenshots for ui and api both types of testcase but here my requirement is that I only want browser should be launch and save screenshots for ui and not for api testcases .

Requirement:

is there any way in pytest framework or generally if we can customized browser launching and failure screenshots capturing for UI testcases only and not for api testcases. ?


Solution

  • You can do it by explicitly marking your tests and saving screenshots only for tests with UI mark:

    pytest.ini

    [pytest]
    markers =
        api: API tests
        ui: UI tests
    

    conftest.py

    def pytest_runtest_makereport(item, call):
        if call.when == 'call' and call.excinfo is not None:
            if any(mark.name == 'ui' for mark in item.own_markers):
                # save screenshot here
                ...
    

    test_example.py

    import pytest
    
    
    @pytest.mark.api
    def test_api():
        assert False
    
    
    @pytest.mark.ui
    def test_ui():
        assert False