webui-ci: save screenshot on test failure

New decorator: ui_driver.screenshot created. It should be applied on test methods.

Screenshot is saved on each exception except SkipTest.

Configuration:
- add: `save_screenshots: True` to ~/.ipa/ui_test.conf to enable saving screenshots
- optionally add `screenshot_dir: /path/to/dir` to specify target directory
  otherwise screenshots are saved to current directory

Reviewed-By: Adam Misnyovszki <amisnyov@redhat.com>
This commit is contained in:
Petr Vobornik 2014-04-28 16:59:50 +02:00
parent 37c238a6ef
commit 6a61378586

View File

@ -24,9 +24,12 @@ Contains browser driver and common tasks.
"""
import nose
from datetime import datetime
import time
import re
import os
from functools import wraps
from nose.plugins.skip import SkipTest
try:
from selenium import webdriver
@ -77,6 +80,29 @@ DEFAULT_PORT = 4444
DEFAULT_TYPE = 'local'
def screenshot(fn):
"""
Decorator for saving screenshot on exception (test fail)
Should be applied on methods of UI_driver subclasses
"""
@wraps(fn)
def screenshot_wrapper(*args):
try:
return fn(*args)
except SkipTest:
raise
except Exception:
self = args[0]
name = '%s_%s_%s' % (
datetime.now().isoformat(),
self.__class__.__name__,
fn.__name__)
self.take_screenshot(name)
raise
return screenshot_wrapper
class UI_driver(object):
"""
Base class for all UI integration tests
@ -368,6 +394,14 @@ class UI_driver(object):
screen = self.get_login_screen()
return screen and screen.is_displayed()
def take_screenshot(self, name):
if self.config.get('save_screenshots'):
scr_dir = self.config.get('screenshot_dir')
path = name + '.png'
if scr_dir:
path = os.path.join(scr_dir, path)
self.driver.get_screenshot_as_file(path)
def navigate_to_entity(self, entity, facet=None):
self.driver.get(self.get_url(entity, facet))
self.wait_for_request(n=3, d=0.4)