Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 12 additions & 15 deletions pytest_splinter/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,7 @@ def prepare_browser(parent):
prepare_browser(parent)

def make_screenshot_on_failure():
if not splinter_make_screenshot_on_failure or not request.node.splinter_failure:
return

try:
if splinter_make_screenshot_on_failure and request.node.splinter_failure:
slaveoutput = getattr(request.config, 'slaveoutput', None)
names = junitxml.mangle_testnames(request.node.nodeid.split("::"))
classname = '.'.join(names[:-1])
Expand All @@ -328,17 +325,17 @@ def make_screenshot_on_failure():
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:
slaveoutput.setdefault('screenshots', []).append({
'class_name': classname,
'file_name': screenshot_file_name,
'content': fd.read()
})
except Exception as e:
request.config.warn('splinter', "Could not save screenshot: {0}".format(e))
pass
try:
browser.driver.save_screenshot(screenshot_path)
with open(screenshot_path) as fd:
if slaveoutput is not None:
slaveoutput.setdefault('screenshots', []).append({
'class_name': classname,
'file_name': screenshot_file_name,
'content': fd.read()
})
except Exception as e:
request.config.warn('SPL504', "Could not save screenshot: {0}".format(e))
request.addfinalizer(make_screenshot_on_failure)

return browser
Expand Down
30 changes: 0 additions & 30 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,2 @@
"""Configuration for pytest runner."""
import mock

import pytest

pytest_plugins = 'pytester'


@pytest.fixture
def mocked_browser(browser_pool, request):
"""Mock splinter browser."""
# to avoid re-using of cached browser from other tests
for browser in browser_pool.values():
browser.quit()
browser_pool.clear()

def mocked_browser(*args, **kwargs):
mocked_browser = mock.MagicMock()
mocked_browser.driver = mock.MagicMock()
mocked_browser.driver.profile = mock.MagicMock()

def save_screenshot(path):
with open(path, 'w'):
pass

mocked_browser.driver.save_screenshot = save_screenshot
return mocked_browser

patcher = mock.patch('pytest_splinter.plugin.splinter.Browser', mocked_browser)
request.addfinalizer(patcher.stop)
patcher.start()
return mocked_browser
30 changes: 24 additions & 6 deletions tests/mocked_browser/conftest.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
"""Configuration for pytest runner."""

pytest_plugins = 'pytester'
import mock

import pytest

Expand All @@ -11,7 +10,26 @@ def splinter_session_scoped_browser():
return False


@pytest.fixture(autouse=True)
def mocked_browser(mocked_browser):
"""Make mocked browser fixture autoused."""
return mocked_browser
@pytest.yield_fixture(autouse=True)
def mocked_browser(browser_pool, request):
"""Mock splinter browser."""
# to avoid re-using of cached browser from other tests
for browser in browser_pool.values():
browser.quit()
browser_pool.clear()

def mocked_browser(*args, **kwargs):
mocked_browser = mock.MagicMock()
mocked_browser.driver = mock.MagicMock()
mocked_browser.driver.profile = mock.MagicMock()

def save_screenshot(path):
with open(path, 'w'):
pass

mocked_browser.driver.save_screenshot = save_screenshot
return mocked_browser

patcher = mock.patch('pytest_splinter.plugin.splinter.Browser', mocked_browser)
yield patcher.start()
patcher.stop()
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
"""Browser screenshot tests."""
import pytest

from mock import patch
import mock

from _pytest.config import Config

def test_browser_screenshot_normal(testdir, mocked_browser):
"""Test making screenshots on test failure if the commandline option is passed.

def test_browser_screenshot_normal(testdir):
"""Test making screenshots on test failure.

Normal test run.
"""
Expand All @@ -17,23 +19,21 @@ 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):
@mock.patch('pytest_splinter.plugin.splinter.Browser')
@mock.patch.object(Config, 'warn', autospec=True)
def test_browser_screenshot_error(mocked_warn, mocked_browser, testdir):
"""Test warning with error during taking screenshots on test failure."""
mocked_browser.return_value.driver.save_screenshot.side_effect = Exception('Failed')
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")
""", "-vvl", "-r w", "--splinter-session-scoped-browser=false")
mocked_warn.assert_called_with(mock.ANY, 'SPL504', 'Could not save screenshot: Failed')


@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.
def test_browser_screenshot_xdist(testdir):
"""Test making screenshots on test failure in distributed mode (xdist).

Distributed test run.
"""
Expand Down
2 changes: 1 addition & 1 deletion tests/test_plugin.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Tests for pytest-bdd-splinter subplugin."""
"""Tests for pytest-splinter plugin."""
import os.path
import time

Expand Down