Skip to content

Commit

Permalink
[FIX] tests: clean up code
Browse files Browse the repository at this point in the history
Closes odoo#26467
  • Loading branch information
rco-odoo authored and KangOl committed Oct 18, 2018
1 parent 9973994 commit 6148849
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions odoo/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ def tearDown(self):


class ChromeBrowser():
""" Helper object to control a Chrome headless process. """

def __init__(self):
if websocket is None:
Expand Down Expand Up @@ -600,7 +601,7 @@ def navigate_to(self, url, wait_stop=False):
_logger.info('Waiting for frame "%s" to stop loading' % frame_id)
self._websocket_wait_event('Page.frameStoppedLoading', params={'frameId': frame_id})

def browser_cleanup(self):
def clear(self):
self._websocket_send('Page.stopScreencast')
self.screencast_frames = []
self._websocket_send('Page.stopLoading')
Expand All @@ -611,11 +612,12 @@ def browser_cleanup(self):
self._websocket_wait_id(cl_id)
self.navigate_to('about:blank', wait_stop=True)


class HttpCase(TransactionCase):
""" Transactional HTTP TestCase with url_open and Chrome headless helpers.
"""
registry_test_mode = True
chrome_browser = None
browser = None

def __init__(self, methodName='runTest'):
super(HttpCase, self).__init__(methodName)
Expand All @@ -628,15 +630,16 @@ def __init__(self, methodName='runTest'):
self._logger = logging.getLogger('%s.%s' % (cls.__module__, cls.__name__))

@classmethod
def start_chrome(cls):
if cls.chrome_browser is None:
cls.chrome_browser = ChromeBrowser()
def start_browser(cls):
# start browser on demand
if cls.browser is None:
cls.browser = ChromeBrowser()

@classmethod
def tearDownClass(cls):
if cls.chrome_browser:
cls.chrome_browser.stop()
cls.chrome_browser = None
if cls.browser:
cls.browser.stop()
cls.browser = None
super(HttpCase, cls).tearDownClass()

def setUp(self):
Expand Down Expand Up @@ -705,9 +708,9 @@ def authenticate(self, user, password):
session._fix_lang(session.context)

odoo.http.root.session_store.save(session)
if self.chrome_browser:
_logger.info('Setting session cookie in chrome headless')
self.chrome_browser.set_cookie('session_id', self.session_id, '/', '127.0.0.1')
if self.browser:
_logger.info('Setting session cookie in browser')
self.browser.set_cookie('session_id', self.session_id, '/', '127.0.0.1')

def browser_js(self, url_path, code, ready='', login=None, timeout=60, **kw):
""" Test js code running in the browser
Expand All @@ -724,29 +727,27 @@ def browser_js(self, url_path, code, ready='', login=None, timeout=60, **kw):
If neither are done before timeout test fails.
"""

# Start chrome only if needed (instead of using a setupClass)
self.start_chrome()
self.start_browser()

try:
self.authenticate(login, login)
url = "http://%s:%s%s" % (HOST, PORT, url_path or '/')
_logger.info('Open "%s" in chrome headless' % url)
_logger.info('Open "%s" in browser' % url)

_logger.info('Starting screen cast')
self.chrome_browser.start_screencast()
self.chrome_browser.navigate_to(url)
self.browser.start_screencast()
self.browser.navigate_to(url)

# Needed because tests like test01.js (qunit tests) are passing a ready
# code = ""
ready = ready or "document.readyState === 'complete'"
self.assertTrue(self.chrome_browser._wait_ready(ready), 'The ready "%s" code was always falsy' % ready)
self.assertTrue(self.chrome_browser._wait_code_ok(code, timeout), 'The test code "%s" failed' % code)
self.assertTrue(self.browser._wait_ready(ready), 'The ready "%s" code was always falsy' % ready)
self.assertTrue(self.browser._wait_code_ok(code, timeout), 'The test code "%s" failed' % code)

finally:
# better at the end of the method, in case we call the method multiple
# times in the same test
self.chrome_browser.browser_cleanup()
# clear browser to make it stop sending requests, in case we call
# the method several times in a test method
self.browser.clear()
self._wait_remaining_requests()

phantom_js = browser_js
Expand Down

0 comments on commit 6148849

Please sign in to comment.