diff --git a/pytest_splinter/plugin.py b/pytest_splinter/plugin.py index 7e48622..597e3ae 100644 --- a/pytest_splinter/plugin.py +++ b/pytest_splinter/plugin.py @@ -23,6 +23,9 @@ from .webdriver_patches import patch_webdriver # pragma: no cover from .splinter_patches import patch_webdriverelement # pragma: no cover +import logging +LOGGER = logging.getLogger(__name__) + NAME_RE = re.compile('[\W]') @@ -308,7 +311,10 @@ def prepare_browser(parent): prepare_browser(parent) def make_screenshot_on_failure(): - if splinter_make_screenshot_on_failure and request.node.splinter_failure: + if not splinter_make_screenshot_on_failure or not request.node.splinter_failure: + return + + try: slaveoutput = getattr(request.config, 'slaveoutput', None) names = junitxml.mangle_testnames(request.node.nodeid.split("::")) classname = '.'.join(names[:-1]) @@ -321,6 +327,7 @@ def make_screenshot_on_failure(): else: screenshot_dir = tmpdir.mkdir('screenshots').strpath screenshot_path = os.path.join(screenshot_dir, screenshot_file_name) + LOGGER.info('Saving screenshot to {0}'.format(screenshot_path)) browser.driver.save_screenshot(screenshot_path) with open(screenshot_path) as fd: if slaveoutput is not None: @@ -329,6 +336,9 @@ def make_screenshot_on_failure(): 'file_name': screenshot_file_name, 'content': fd.read() }) + except Exception as e: + request.config.warn('splinter', "Could not save screenshot: {0}".format(e)) + pass request.addfinalizer(make_screenshot_on_failure) return browser diff --git a/tests/test_screenshot.py b/tests/test_screenshot.py index 7774db5..9962631 100644 --- a/tests/test_screenshot.py +++ b/tests/test_screenshot.py @@ -1,6 +1,8 @@ """Browser screenshot tests.""" import pytest +from mock import patch + def test_browser_screenshot_normal(testdir, mocked_browser): """Test making screenshots on test failure if the commandline option is passed. @@ -15,6 +17,20 @@ def test_screenshot(browser): assert testdir.tmpdir.join('test_browser_screenshot_normal', 'test_screenshot-browser.png').isfile() +@patch('_pytest.config.Config.warn') +def test_browser_screenshot_error(testdir, mocked_browser, request): + """Test warning with error during taking screenshots on test failure.""" + testdir.inline_runsource(""" + def test_screenshot(browser): + # Create a file here, so makedirs in make_screenshot_on_failure will fail. + open('isafile', 'w').close() + assert False + + def test_warn_called(request): + assert request.config.warn.call_count == 1 + """, "-vl", "--splinter-session-scoped-browser=false") + + @pytest.mark.skipif('not config.pluginmanager.getplugin("xdist")', reason='pytest-xdist is not installed') def test_browser_screenshot_xdist(testdir, mocked_browser): """Test making screenshots on test failure if the commandline option is passed.