From 64564343820156c09afeb3eb7c61e79b565f901a Mon Sep 17 00:00:00 2001 From: Xavier-Do Date: Wed, 30 Oct 2019 12:14:34 +0000 Subject: [PATCH] [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) --- odoo/addons/base/tests/__init__.py | 1 + odoo/addons/base/tests/test_http_case.py | 40 ++++++++++++++++++++++++ odoo/tests/common.py | 23 ++++++++++++-- 3 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 odoo/addons/base/tests/test_http_case.py diff --git a/odoo/addons/base/tests/__init__.py b/odoo/addons/base/tests/__init__.py index 81a12552ff2..54a68834374 100644 --- a/odoo/addons/base/tests/__init__.py +++ b/odoo/addons/base/tests/__init__.py @@ -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 diff --git a/odoo/addons/base/tests/test_http_case.py b/odoo/addons/base/tests/test_http_case.py new file mode 100644 index 00000000000..1ab44b901cb --- /dev/null +++ b/odoo/addons/base/tests/test_http_case.py @@ -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 :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) diff --git a/odoo/tests/common.py b/odoo/tests/common.py index 063f1d51e07..67cabbb4f10 100644 --- a/odoo/tests/common.py +++ b/odoo/tests/common.py @@ -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: