devtools/cwwindmill.py
changeset 6425 8d7c2fd2ac66
parent 6424 f443a2b8a5c7
child 6438 abae10f81a85
equal deleted inserted replaced
6423:c560f8d9faee 6425:8d7c2fd2ac66
    23     * manage command line option from pytest to run specific use tests only
    23     * manage command line option from pytest to run specific use tests only
    24 """
    24 """
    25 
    25 
    26 
    26 
    27 import os, os.path as osp
    27 import os, os.path as osp
       
    28 from logging import getLogger, ERROR
    28 import sys
    29 import sys
    29 
    30 
    30 # imported by default to simplify further import statements
    31 # imported by default to simplify further import statements
    31 from logilab.common.testlib import TestCase, unittest_main
    32 from logilab.common.testlib import TestCase, unittest_main
    32 
    33 
    33 import windmill
    34 import windmill
    34 from windmill.dep import functest
    35 from windmill.dep import functest
       
    36 from windmill.bin.admin_lib import configure_global_settings, setup, teardown
    35 
    37 
    36 from cubicweb.devtools.httptest import CubicWebServerTC
    38 from cubicweb.devtools.httptest import CubicWebServerTC
    37 
    39 
    38 
    40 
    39 # Excerpt from :ref:`windmill.authoring.unit`
    41 # Excerpt from :ref:`windmill.authoring.unit`
    42         self.test_list = test_list
    44         self.test_list = test_list
    43 
    45 
    44 unittestreporter = UnitTestReporter()
    46 unittestreporter = UnitTestReporter()
    45 functest.reports.register_reporter(unittestreporter)
    47 functest.reports.register_reporter(unittestreporter)
    46 
    48 
    47 class WindmillUnitTestCase(TestCase):
    49 
       
    50 # Windmill use case are written with no anonymous user
       
    51 from cubicweb.devtools.httptest import CubicWebServerConfig
       
    52 CubicWebServerConfig.anonymous_logged = False
       
    53 
       
    54 class CubicWebWindmillUseCase(CubicWebServerTC):
       
    55     """basic class for Windmill use case tests
       
    56 
       
    57     If you want to change cubicweb test server parameters, define a new
       
    58     :class:`CubicWebServerConfig` and override the :var:`configcls`
       
    59     attribute:
       
    60 
       
    61         configcls = CubicWebServerConfig
       
    62 
       
    63     From Windmill configuration:
       
    64 
       
    65     .. attribute:: browser
       
    66         identification string (firefox|ie|safari|chrome) (firefox by default)
       
    67     .. attribute :: edit_test
       
    68         load and edit test for debugging (False by default)
       
    69     .. attribute:: test_dir (optional)
       
    70         testing file path or directory (windmill directory under your unit case
       
    71         file by default)
       
    72 
       
    73     Examples:
       
    74 
       
    75         browser = 'firefox'
       
    76         test_dir = osp.join(__file__, 'windmill')
       
    77         edit_test = False
       
    78 
       
    79     If you prefer, you can put here the use cases recorded by windmill GUI
       
    80     (services transformer) instead of the windmill sub-directory
       
    81     You can change `test_dir` as following:
       
    82 
       
    83         test_dir = __file__
       
    84 
       
    85     Instead of toggle `edit_test` value, try `pytest -i`
       
    86     """
       
    87     browser = 'firefox'
       
    88     edit_test = "-i" in sys.argv # detection for pytest invocation
       
    89 
       
    90     def _test_dir(self):
       
    91         """access to class attribute if possible or make assumption
       
    92         of expected directory"""
       
    93         try:
       
    94             return getattr(self, 'test_dir')
       
    95         except AttributeError:
       
    96             if os.path.basename(sys.argv[0]) == "pytest":
       
    97                 test_dir = os.getcwd()
       
    98             else:
       
    99                 import inspect
       
   100                 test_dir = os.path.dirname(inspect.stack()[-1][1])
       
   101             return osp.join(test_dir, 'windmill')
       
   102 
    48     def setUp(self):
   103     def setUp(self):
       
   104         # Start CubicWeb session before running the server to populate self.vreg
       
   105         CubicWebServerTC.setUp(self)
       
   106         # XXX reduce log output (should be done in a cleaner way)
       
   107         # windmill fu** up our logging configuration
       
   108         for logkey in ('windmill', 'logilab', 'cubicweb'):
       
   109             getLogger(logkey).setLevel(ERROR)
       
   110         self.test_dir = self._test_dir()
       
   111         msg = "provide a valid 'test_dir' as the given test file/dir (current: %s)"
       
   112         assert os.path.exists(self.test_dir), (msg % self.test_dir)
       
   113         # windmill setup
    49         windmill.stdout, windmill.stdin = sys.stdout, sys.stdin
   114         windmill.stdout, windmill.stdin = sys.stdout, sys.stdin
    50         from windmill.bin.admin_lib import configure_global_settings, setup
       
    51         configure_global_settings()
   115         configure_global_settings()
    52         windmill.settings['TEST_URL'] = self.test_url
   116         windmill.settings['TEST_URL'] = self.config['base-url']
    53         if hasattr(self,"windmill_settings"):
   117         if hasattr(self,"windmill_settings"):
    54             for (setting,value) in self.windmill_settings.iteritems():
   118             for (setting,value) in self.windmill_settings.iteritems():
    55                 windmill.settings[setting] = value
   119                 windmill.settings[setting] = value
    56         self.windmill_shell_objects = setup()
   120         self.windmill_shell_objects = setup()
    57 
   121 
    58     def tearDown(self):
   122     def tearDown(self):
    59         from windmill.bin.admin_lib import teardown
       
    60         teardown(self.windmill_shell_objects)
   123         teardown(self.windmill_shell_objects)
    61 
       
    62 
       
    63 class CubicWebWindmillUseCase(CubicWebServerTC, WindmillUnitTestCase):
       
    64     """basic class for Windmill use case tests
       
    65 
       
    66     :param browser: browser identification string (firefox|ie|safari|chrome) (firefox by default)
       
    67     :param test_dir: testing file path or directory (./windmill by default)
       
    68     :param edit_test: load and edit test for debugging (False by default)
       
    69     """
       
    70     browser = 'firefox'
       
    71     test_dir = osp.join(os.getcwd(), 'windmill')
       
    72     edit_test = "-i" in sys.argv # detection for pytest invocation
       
    73 
       
    74     def setUp(self):
       
    75         # reduce log output
       
    76         from logging import getLogger, ERROR
       
    77         getLogger('cubicweb').setLevel(ERROR)
       
    78         getLogger('logilab').setLevel(ERROR)
       
    79         getLogger('windmill').setLevel(ERROR)
       
    80         # Start CubicWeb session before running the server to populate self.vreg
       
    81         CubicWebServerTC.setUp(self)
       
    82         assert os.path.exists(self.test_dir), "provide 'test_dir' as the given test file/dir"
       
    83         WindmillUnitTestCase.setUp(self)
       
    84 
       
    85     def tearDown(self):
       
    86         WindmillUnitTestCase.tearDown(self)
       
    87         CubicWebServerTC.tearDown(self)
   124         CubicWebServerTC.tearDown(self)
    88 
   125 
    89     def testWindmill(self):
   126     def testWindmill(self):
    90         if self.edit_test:
   127         if self.edit_test:
    91             # see windmill.bin.admin_options.Firebug
   128             # see windmill.bin.admin_options.Firebug
    92             windmill.settings['INSTALL_FIREBUG'] = 'firebug'
   129             windmill.settings['INSTALL_FIREBUG'] = 'firebug'
    93             windmill.settings.setdefault('MOZILLA_PLUGINS', []).extend(
   130             windmill.settings.setdefault('MOZILLA_PLUGINS', []).extend(
    94                 '/usr/share/mozilla-extensions/',
   131                 ['/usr/share/mozilla-extensions/',
    95                 '/usr/share/xul-ext/')
   132                  '/usr/share/xul-ext/'])
    96         controller = self.windmill_shell_objects['start_' + self.browser]()
   133         controller = self.windmill_shell_objects['start_' + self.browser]()
    97         self.windmill_shell_objects['do_test'](self.test_dir,
   134         self.windmill_shell_objects['do_test'](self.test_dir,
    98                                                load=self.edit_test,
   135                                                load=self.edit_test,
    99                                                threaded=False)
   136                                                threaded=False)
   100         # set a breakpoint to be able to debug windmill test
   137         # set a breakpoint to be able to debug windmill test