From 2648d550333229d05908af7ab5d7c67b6ffde0fe Mon Sep 17 00:00:00 2001 From: ddm Date: Mon, 23 Dec 2013 17:51:13 +0100 Subject: [PATCH] [IMP] Refactor UI test code for reusability bzr revid: ddm@openerp.com-20131223165113-kx4aalzs6las1m2z --- addons/website/tests/test_ui.py | 31 ++++---- .../tests/ui_suite/banner_tour_test.js | 77 ++++++------------- .../tests/ui_suite/ui_test_template.js | 51 ++++++++++++ 3 files changed, 90 insertions(+), 69 deletions(-) create mode 100644 addons/website/tests/ui_suite/ui_test_template.js diff --git a/addons/website/tests/test_ui.py b/addons/website/tests/test_ui.py index eb799b0065d..b8a8633d694 100644 --- a/addons/website/tests/test_ui.py +++ b/addons/website/tests/test_ui.py @@ -43,9 +43,10 @@ class WebsiteUiTest(unittest.TestCase): class WebsiteUiSuite(unittest.TestSuite): # timeout in seconds - def __init__(self, testfile, timeout=10.0): - self.testfile = testfile - self.timeout = timeout + def __init__(self, testfile, options, timeout=10.0): + self._testfile = testfile + self._timeout = timeout + self._options = options self._test = None def __iter__(self): @@ -71,26 +72,24 @@ class WebsiteUiSuite(unittest.TestSuite): del result._exc_info_to_string def _run(self, result): - self._test = WebsiteUiTest(self.testfile) - self.start_time = time.time() + self._test = WebsiteUiTest(self._testfile) + start_time = time.time() last_check_time = time.time() - phantomOptions = json.dumps({ - 'timeout': self.timeout, - 'port': tools.config['xmlrpc_port'] - }) + self._options['timeout'] = self._timeout + self._options['port'] = tools.config['xmlrpc_port'] phantom = subprocess.Popen([ 'phantomjs', - os.path.join(ROOT, self.testfile), - phantomOptions + os.path.join(ROOT, self._testfile), + json.dumps(self._options) ], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) proc_stdout = LineReader(phantom.stdout.fileno()) readable = [proc_stdout] try: - while phantom.poll() is None and readable and last_check_time < self.start_time + self.timeout: + while phantom.poll() is None and readable and last_check_time < start_time + self._timeout: ready, _, _ = select.select(readable, [], [], 0.1) if not ready: last_check_time = time.time() @@ -105,8 +104,8 @@ class WebsiteUiSuite(unittest.TestSuite): # the runner expects only one output line # any subsequent line is ignored readable.remove(stream) - if last_check_time >= (self.start_time + self.timeout): - result.addError(self._test, "Timeout after %s s" % (last_check_time - self.start_time )) + if last_check_time >= (start_time + self._timeout): + result.addError(self._test, "Timeout after %s s" % (last_check_time - start_time )) finally: # kill phantomjs if phantom.exit() wasn't called in the test if phantom.poll() is None: @@ -139,6 +138,6 @@ class WebsiteUiSuite(unittest.TestSuite): result.addError(self._test, 'Unexpected message: "%s"' % line) def load_tests(loader, base, _): - base.addTest(WebsiteUiSuite('dummy_test.js')) - base.addTest(WebsiteUiSuite('banner_tour_test.js')) + base.addTest(WebsiteUiSuite('dummy_test.js', {})) + base.addTest(WebsiteUiSuite('banner_tour_test.js', { 'path': '/web#action=website.action_website_homepage&login=admin&password=admin' })) return base \ No newline at end of file diff --git a/addons/website/tests/ui_suite/banner_tour_test.js b/addons/website/tests/ui_suite/banner_tour_test.js index 5b9a6364b99..0d22d05629a 100644 --- a/addons/website/tests/ui_suite/banner_tour_test.js +++ b/addons/website/tests/ui_suite/banner_tour_test.js @@ -1,62 +1,33 @@ -var options = JSON.parse(phantom.args); -var url = 'http://localhost:'+options.port+'/web#action=website.action_website_homepage&login=admin&password=admin'; +var testTemplate = require('./ui_test_template.js'); -var page = require('webpage').create(); +var waitFor = testTemplate.waitFor; -page.onError = function(message, trace) { - console.log('{ "event": "error", "message": "'+message+'"}'); - phantom.exit(1); -}; - -function waitFor(ready, callback, timeout) { - var timeoutMillis = timeout ? Math.round(timeout*1000) : 30000; - var start = new Date().getTime(); - var condition = ready(); - var interval = setInterval(function() { - if ((new Date().getTime() - start < timeoutMillis) && !condition ) { - condition = ready(); - } else { - if(!condition) { - console.log('{ "event": "error", "message": "Timeout after '+timeoutMillis+' ms" }'); - phantom.exit(1); - } else { - clearInterval(interval); - callback(); - } - } - }, 100); -}; - -page.viewportSize = { width: 1920, height: 1080 }; -page.open(url, function (status) { - if (status !== 'success') { - console.log('{ "event": "failure", "message": "'+url+' failed to load"}'); - phantom.exit(1); - } else { +function test (page) { + page.evaluate(function () { + localStorage.clear(); + }); + waitFor(function () { + return page.evaluate(function () { + return window.openerp && window.openerp.website && window.openerp.website.TestConsole && window.openerp.website.TestConsole.test('banner'); + }); + }, function () { page.evaluate(function () { - localStorage.clear(); + window.openerp.website.TestConsole.test('banner').run(true); }); waitFor(function () { return page.evaluate(function () { - return window.openerp && window.openerp.website && window.openerp.website.TestConsole && window.openerp.website.TestConsole.test('banner'); + var $edit = $('button[data-action=edit]'); + var $carousel = $('#wrap [data-snippet-id=carousel]'); + var $columns = $('#wrap [data-snippet-id=three-columns]'); + return $carousel && $carousel.length === 1 + && $columns && $columns.length === 1 + && $('button[data-action=edit]').is(":visible"); }); }, function () { - page.evaluate(function () { - window.openerp.website.TestConsole.test('banner').run(true); - }); - waitFor(function () { - return page.evaluate(function () { - var $edit = $('button[data-action=edit]'); - var $carousel = $('#wrap [data-snippet-id=carousel]'); - var $columns = $('#wrap [data-snippet-id=three-columns]'); - return $carousel && $carousel.length === 1 - && $columns && $columns.length === 1 - && $('button[data-action=edit]').is(":visible"); - }); - }, function () { - console.log('{ "event": "success" }'); - phantom.exit(); - }); + console.log('{ "event": "success" }'); + phantom.exit(); }); - } -}); \ No newline at end of file + }); +} + +testTemplate.runTest(test); \ No newline at end of file diff --git a/addons/website/tests/ui_suite/ui_test_template.js b/addons/website/tests/ui_suite/ui_test_template.js new file mode 100644 index 00000000000..db355a07570 --- /dev/null +++ b/addons/website/tests/ui_suite/ui_test_template.js @@ -0,0 +1,51 @@ + + +function waitFor(ready, callback, timeout) { + var timeoutMillis = timeout ? Math.round(timeout*1000) : 30000; + var start = new Date().getTime(); + var condition = ready(); + var interval = setInterval(function() { + if ((new Date().getTime() - start < timeoutMillis) && !condition ) { + condition = ready(); + } else { + if(!condition) { + console.log('{ "event": "error", "message": "Timeout after '+timeoutMillis+' ms" }'); + phantom.exit(1); + } else { + clearInterval(interval); + callback(); + } + } + }, 100); +}; + + + +function runTest (test) { + var options = JSON.parse(phantom.args); + var port = options.port ? ':'+options.port : ''; + var path = options.path ? options.path : ''; + var url = 'http://localhost'+port+path; + + var page = require('webpage').create(); + + page.viewportSize = { width: 1920, height: 1080 }; + page.onError = function(message, trace) { + console.log('{ "event": "error", "message": "'+message+'"}'); + phantom.exit(1); + }; + + page.open(url, function (status) { + if (status !== 'success') { + console.log('{ "event": "failure", "message": "'+url+' failed to load"}'); + phantom.exit(1); + } else { + test(page); + } + }); +} + +module.exports = { + waitFor: waitFor, + runTest: runTest +} \ No newline at end of file