[IMP] tests: manage console.log(object)

Before this commit, console.error were catched by browser_js
in order to log them but only value was used on received object,
which is correct only when the received object is text.

With owl arrival, error object may be logged. This
commit add the ability to manage logged object, fallbacking on
a complete representation of object if object is not an error or
description is empty.

closes odoo/odoo#39592

Signed-off-by: Christophe Monniez (moc) <moc@odoo.com>
This commit is contained in:
Xavier-Do
2019-10-30 15:12:10 +00:00
parent f11cb858be
commit 6456434382
3 changed files with 62 additions and 2 deletions
+1
View File
@@ -8,6 +8,7 @@ from . import test_db_cursor
from . import test_expression
from . import test_float
from . import test_func
from . import test_http_case
from . import test_image
from . import test_ir_actions
from . import test_ir_attachment
+40
View File
@@ -0,0 +1,40 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo.tests.common import HttpCase, tagged
from odoo.tools import mute_logger
from unittest.mock import patch
@tagged('-at_install', 'post_install')
class TestHttpCase(HttpCase):
def test_console_error_string(self):
with self.assertRaises(AssertionError) as error_catcher:
code = "console.error('test error','message')"
with patch('odoo.tests.common.ChromeBrowser.take_screenshot', return_value=None):
self.browser_js(url_path='about:blank', code=code)
# second line must contains error message
self.assertEqual(error_catcher.exception.args[0].split('\n', 1)[1], "test error message")
def test_console_error_object(self):
with self.assertRaises(AssertionError) as error_catcher:
code = "console.error(TypeError('test error ' + 'message'))"
with patch('odoo.tests.common.ChromeBrowser.take_screenshot', return_value=None):
self.browser_js(url_path='about:blank', code=code)
# second line must contains error message
self.assertEqual(error_catcher.exception.args[0].split('\n', 1)[1],
'TypeError: test error message\n at <anonymous>:1:15')
def test_console_log_object(self):
with self.assertLogs() as log_catcher:
code = "console.log({custom:{1:'test', 2:'a'}, value:1, description:'dummy'});console.log('test successful');"
self.browser_js(url_path='about:blank', code=code)
console_log_count = 0
for log in log_catcher.output:
if 'console log' in log:
text = log.split('console log: ', 1)[1]
if text == 'test successful':
continue
self.assertEqual(log.split('console log: ', 1)[1], "Object\n{custom:Object, value:1, description:'dummy'}")
console_log_count +=1
self.assertEqual(console_log_count, 1)
+21 -2
View File
@@ -834,7 +834,23 @@ class ChromeBrowser():
elif res and res.get('method') == 'Runtime.consoleAPICalled' and res.get('params', {}).get('type') in ('log', 'error', 'trace'):
logs = res.get('params', {}).get('args')
log_type = res.get('params', {}).get('type')
content = " ".join([str(log.get('value', '')) for log in logs])
content = []
for log in logs:
text = ''
if log.get('type') == 'string':
text = str(log.get('value', '`Empty string`'))
elif log.get('type') == 'object' and 'Error' in log.get('className', '') and log.get('description'):
text = str(log.get('description'))
else:
type_ = log.get('className') or log.get('type')
properties = log.get('preview', {}).get('properties')
if log.get('type') == 'object' and properties and all(p.get('name') is not None and p.get('value') is not None for p in properties):
elems = ['%s:%s' % (p.get('name'), "'%s'" % p.get('value') if p.get('type') == 'string' else p.get('value')) for p in properties]
text = "%s\n{%s}" % (type_, ", ".join(elems))
else:
text = str(log)
content.append(text)
content = " ".join(content)
if log_type == 'error':
self.take_screenshot()
self._save_screencast()
@@ -1026,7 +1042,10 @@ class HttpCase(TransactionCase):
# flush updates to the database before launching the client side,
# otherwise they simply won't be visible
ICP.flush()
url = "%s%s" % (base_url, url_path or '/')
if re.match('[a-z]*:', url_path or ''): # about:, http:, ...
url = url_path
else:
url = "%s%s" % (base_url, url_path or '/')
self._logger.info('Open "%s" in browser', url)
if self.browser.screencasts_dir: