| Index: telemetry/telemetry/testing/browser_test_case.py
|
| diff --git a/telemetry/telemetry/testing/browser_test_case.py b/telemetry/telemetry/testing/browser_test_case.py
|
| index 85e6797223392acc4cf2c743cc83a9c130bf3ed9..710904b07ef7c31110f1ba308c35888d7561272c 100644
|
| --- a/telemetry/telemetry/testing/browser_test_case.py
|
| +++ b/telemetry/telemetry/testing/browser_test_case.py
|
| @@ -2,7 +2,11 @@
|
| # Use of this source code is governed by a BSD-style license that can be
|
| # found in the LICENSE file.
|
|
|
| +from functools import wraps
|
| +import logging
|
| import os
|
| +import sys
|
| +import types
|
| import unittest
|
|
|
| from telemetry.internal.browser import browser_finder
|
| @@ -13,6 +17,52 @@ current_browser_options = None
|
| current_browser = None
|
|
|
|
|
| +class _MetaBrowserTestCase(type):
|
| + """Metaclass for BrowserTestCase.
|
| +
|
| + The metaclass wraps all test* methods of all subclasses of BrowserTestCase to
|
| + print browser standard output and log upon failure.
|
| + """
|
| +
|
| + def __new__(mcs, name, bases, dct):
|
| + new_dct = {}
|
| + for attributeName, attribute in dct.iteritems():
|
| + if (isinstance(attribute, types.FunctionType) and
|
| + attributeName.startswith('test')):
|
| + attribute = mcs._PrintBrowserStandardOutputAndLogOnFailure(attribute)
|
| + new_dct[attributeName] = attribute
|
| + return type.__new__(mcs, name, bases, new_dct)
|
| +
|
| + @staticmethod
|
| + def _PrintBrowserStandardOutputAndLogOnFailure(method):
|
| + @wraps(method)
|
| + def WrappedMethod(self):
|
| + try: # pylint: disable=broad-except
|
| + method(self)
|
| + except Exception:
|
| + exc_info = sys.exc_info()
|
| +
|
| + logging.info('*************** BROWSER STANDARD OUTPUT ***************')
|
| + try: # pylint: disable=broad-except
|
| + logging.info(self._browser.GetStandardOutput())
|
| + except Exception:
|
| + logging.exception('Failed to get browser standard output:')
|
| + logging.info('*********** END OF BROWSER STANDARD OUTPUT ************')
|
| +
|
| + logging.info('********************* BROWSER LOG *********************')
|
| + try: # pylint: disable=broad-except
|
| + logging.info(self._browser.GetLogFileContents())
|
| + except Exception:
|
| + logging.exception('Failed to get browser log:')
|
| + logging.info('***************** END OF BROWSER LOG ******************')
|
| +
|
| + # Re-raise the original exception. Note that we can't just use 'raise'
|
| + # without any arguments because an exception might have been thrown when
|
| + # the browser standard output was retrieved.
|
| + raise exc_info[0], exc_info[1], exc_info[2]
|
| + return WrappedMethod
|
| +
|
| +
|
| def teardown_browser():
|
| global current_browser
|
| global current_browser_options
|
| @@ -24,6 +74,8 @@ def teardown_browser():
|
|
|
|
|
| class BrowserTestCase(unittest.TestCase):
|
| + __metaclass__ = _MetaBrowserTestCase
|
| +
|
| @classmethod
|
| def setUpClass(cls):
|
| cls._platform = None
|
|
|