Skip to content
This repository has been archived by the owner on Mar 24, 2021. It is now read-only.

Commit

Permalink
Merge pull request #139 from alphagov/browser-screenshot-on-failure
Browse files Browse the repository at this point in the history
Browser screenshot on failure
  • Loading branch information
maxfliri committed Aug 22, 2013
2 parents 29a2d18 + b827e1f commit 61da2a1
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
4 changes: 2 additions & 2 deletions features/environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ def before_scenario(context, _):
storage.connection.drop_database(storage.name)


def after_scenario(context, _):
context.client.after_scenario()
def after_scenario(context, scenario):
context.client.after_scenario(scenario)


def after_feature(context, _):
Expand Down
23 changes: 22 additions & 1 deletion features/support/splinter_client.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
from datetime import datetime
import logging
from pymongo import MongoClient
from splinter import Browser

from features.support.support import Api, BaseClient


logger = logging.getLogger(__name__)


class SplinterClient(BaseClient):

def __init__(self, database_name):
Expand All @@ -16,7 +21,9 @@ def storage(self):
def before_scenario(self):
self.browser = Browser('phantomjs', wait_time=3)

def after_scenario(self):
def after_scenario(self, scenario):
if scenario.status == 'failed':
self._save_screenshot(self._screenshot_name(scenario.name))
self.browser.quit()

def spin_down(self):
Expand All @@ -26,6 +33,20 @@ def get(self, url, headers=None):
self.browser.visit(self._write_api.url(url))
return SplinterResponse(self.browser)

def _screenshot_name(self, scenario_name):
return "tmp/screenshot_%s_%s.png" % (
datetime.now().isoformat(), scenario_name.replace(' ', '_'))

def _save_screenshot(self, filename):
try:
success = self.browser.driver.save_screenshot(filename)
if not success:
logger.warn(
"Unable to save screenshot %s: IO error" % filename)
except Exception as e:
logger.warn("Unable to save screenshot %s: Exception" % filename)
logger.warn(e)


class SplinterResponse:
def __init__(self, browser):
Expand Down
2 changes: 1 addition & 1 deletion features/support/support.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class BaseClient(object):
def before_scenario(self):
pass

def after_scenario(self):
def after_scenario(self, scenario):
pass

def spin_down(self):
Expand Down

0 comments on commit 61da2a1

Please sign in to comment.