devtools/qunit.py
changeset 9992 3905682ece11
parent 9991 3e7f1e9f3adc
child 9993 1ced03652d5e
equal deleted inserted replaced
9991:3e7f1e9f3adc 9992:3905682ece11
    60 
    60 
    61     profile_name_mask = 'PYTEST_PROFILE_%(uid)s'
    61     profile_name_mask = 'PYTEST_PROFILE_%(uid)s'
    62 
    62 
    63     def __init__(self, url=None):
    63     def __init__(self, url=None):
    64         self._process = None
    64         self._process = None
    65         self._tmp_dir = mkdtemp(prefix='cwtest-ffxprof-')
    65         self._profile_dir = mkdtemp(prefix='cwtest-ffxprof-')
    66         self._profile_data = {'uid': uuid4()}
       
    67         self._profile_name = self.profile_name_mask % self._profile_data
       
    68         stdout = TemporaryFile()
       
    69         stderr = TemporaryFile()
       
    70         self.firefox_cmd = ['firefox', '-no-remote']
    66         self.firefox_cmd = ['firefox', '-no-remote']
    71         if os.name == 'posix':
    67         if os.name == 'posix':
    72             self.firefox_cmd = [osp.join(osp.dirname(__file__), 'data', 'xvfb-run.sh'),
    68             self.firefox_cmd = [osp.join(osp.dirname(__file__), 'data', 'xvfb-run.sh'),
    73                                 '-a', '-s', '-noreset -screen 0 640x480x8'] + self.firefox_cmd
    69                                 '-a', '-s', '-noreset -screen 0 640x480x8'] + self.firefox_cmd
    74         try:
       
    75             home = osp.expanduser('~')
       
    76             user = getlogin()
       
    77             assert os.access(home, os.W_OK), \
       
    78                    'No write access to your home directory, Firefox will crash.'\
       
    79                    ' Are you sure "%s" is a valid home  for user "%s"' % (home, user)
       
    80             check_call(self.firefox_cmd + ['-CreateProfile',
       
    81                         '%s %s' % (self._profile_name, self._tmp_dir)],
       
    82                                    stdout=stdout, stderr=stderr)
       
    83         except CalledProcessError as cpe:
       
    84             stdout.seek(0)
       
    85             stderr.seek(0)
       
    86             raise VerboseCalledProcessError(cpe.returncode, cpe.cmd, stdout.read(), stderr.read())
       
    87 
    70 
    88     def start(self, url):
    71     def start(self, url):
    89         self.stop()
    72         self.stop()
    90         fnull = open(os.devnull, 'w')
    73         cmd = self.firefox_cmd + ['-silent', '--profile', self._profile_dir,
    91         self._process = Popen(self.firefox_cmd + ['-P', self._profile_name, url],
    74                                   '-url', url]
    92                               stdout=fnull, stderr=fnull)
    75         with open(os.devnull, 'w') as fnull:
       
    76             self._process = Popen(cmd, stdout=fnull, stderr=fnull)
    93 
    77 
    94     def stop(self):
    78     def stop(self):
    95         if self._process is not None:
    79         if self._process is not None:
    96             assert self._process.returncode is None,  self._process.returncode
    80             assert self._process.returncode is None,  self._process.returncode
    97             self._process.terminate()
    81             self._process.terminate()
    98             self._process.wait()
    82             self._process.wait()
    99             self._process = None
    83             self._process = None
   100 
    84 
   101     def __del__(self):
    85     def __del__(self):
   102         self.stop()
    86         self.stop()
   103         rmtree(self._tmp_dir)
    87         rmtree(self._profile_dir)
   104 
    88 
   105 
    89 
   106 class QUnitTestCase(CubicWebServerTC):
    90 class QUnitTestCase(CubicWebServerTC):
   107 
    91 
   108     tags = CubicWebServerTC.tags | Tags(('qunit',))
    92     tags = CubicWebServerTC.tags | Tags(('qunit',))
   167 
   151 
   168         while not self.test_queue.empty():
   152         while not self.test_queue.empty():
   169             self.test_queue.get(False)
   153             self.test_queue.get(False)
   170 
   154 
   171         browser = FirefoxHelper()
   155         browser = FirefoxHelper()
       
   156         # start firefox once to let it init the profile (and run system-wide
       
   157         # add-ons post setup, blegh), and then kill it ...
       
   158         browser.start('about:blank')
       
   159         import time; time.sleep(5)
       
   160         browser.stop()
       
   161         # ... then actually run the test file
   172         browser.start(html_test_file.name)
   162         browser.start(html_test_file.name)
   173         test_count = 0
   163         test_count = 0
   174         error = False
   164         error = False
   175         def raise_exception(cls, *data):
   165         def raise_exception(cls, *data):
   176             raise cls(*data)
   166             raise cls(*data)