tests: functional: Implement a workaround for issue with screenshots

pytest_splinter has an issue with creating screenshots when using
session_browser instead of a browser fixture. Implement a workaround.

See: https://github.com/pytest-dev/pytest-splinter/pull/157

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2021-12-22 19:01:46 -08:00 committed by James Valleroy
parent 668da20a02
commit f9c1e09932
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808

View File

@ -166,3 +166,64 @@ def fixture_call_action(request, capsys, actions_module):
return captured.out
return _call_action
@pytest.fixture(autouse=True)
def fixture_fix_session_browser_screenshots(request):
"""Fix a bug in pytest-splinter for screenshots.
When using session_browser, pytest-splinter does not take a screenshot when
a test has failed. It is uses internal pytest API on the FixtureRequest
object. This API was removed in later versions of pytest causing the
failure. Re-implement the fixture that has the problem fixing this issue.
Drop this fixture after a fix is merged and released in pytest-splinter.
See: https://github.com/pytest-dev/pytest-splinter/pull/157
"""
yield
if not request.config.pluginmanager.has_plugin('pytest-splinter'):
return
session_tmpdir = request.getfixturevalue('session_tmpdir')
splinter_session_scoped_browser = request.getfixturevalue(
'splinter_session_scoped_browser')
splinter_make_screenshot_on_failure = request.getfixturevalue(
'splinter_make_screenshot_on_failure')
splinter_screenshot_dir = request.getfixturevalue(
'splinter_screenshot_dir')
splinter_screenshot_getter_html = request.getfixturevalue(
'splinter_screenshot_getter_html')
splinter_screenshot_getter_png = request.getfixturevalue(
'splinter_screenshot_getter_png')
splinter_screenshot_encoding = request.getfixturevalue(
'splinter_screenshot_encoding')
# Screenshot for function scoped browsers is handled in
# browser_instance_getter
if not splinter_session_scoped_browser:
return
for name in request.fixturenames:
fixture_def = request._fixture_defs.get(name)
if not fixture_def or not fixture_def.cached_result:
continue
value = fixture_def.cached_result[0]
should_take_screenshot = (hasattr(value, "__splinter_browser__")
and splinter_make_screenshot_on_failure
and getattr(request.node, 'splinter_failure',
True))
from pytest_splinter import plugin
if should_take_screenshot:
plugin._take_screenshot(
request=request,
fixture_name=name,
session_tmpdir=session_tmpdir,
browser_instance=value,
splinter_screenshot_dir=splinter_screenshot_dir,
splinter_screenshot_getter_html=splinter_screenshot_getter_html,
splinter_screenshot_getter_png=splinter_screenshot_getter_png,
splinter_screenshot_encoding=splinter_screenshot_encoding,
)