[IMP] test_ui conversion, needs validation from chm

bzr revid: al@openerp.com-20140209232040-12uq85pseheth8z0
This commit is contained in:
Antony Lesuisse 2014-02-10 00:20:40 +01:00
parent 85d37619e4
commit a21b1c4f8c
8 changed files with 33 additions and 330 deletions

View File

@ -1,156 +1,18 @@
import unittest
import subprocess
import os
import select
import time
import json
from openerp import sql_db, tools
import glob
# avoid "ValueError: too many values to unpack"
def _exc_info_to_string(err, test):
return err
import openerp
class Stream:
def __init__(self, file_descriptor):
self._file_descriptor = file_descriptor
self._buffer = ''
fname, _ = os.path.splitext(__file__)
def fileno(self):
return self._file_descriptor
# TODO Rewrite & fix
def readlines(self):
data = os.read(self._file_descriptor, 4096)
if not data: # EOF
return None
self._buffer += data
if '\n' not in data:
return []
tmp = self._buffer.split('\n')
lines, self._buffer = tmp[:-1], tmp[-1]
return lines
class TestUiAdmin(openerp.tests.HttpCase):
def test(self):
for i in glob.glob('%s_admin_*.js' % fname):
self.phantomjs(openerp.modules.module.get_module_resource('website','tests', i))
class WebsiteUiTest(unittest.TestCase):
def __init__(self, name):
self.name = name
def shortDescription(self):
return None
def __str__(self):
return self.name
class TestUiPublic(openerp.tests.HttpCase):
def test(self):
for i in glob.glob('%s_public_*.js' % fname):
self.phantomjs(openerp.modules.module.get_module_resource('website','tests', i))
class WebsiteUiSuite(unittest.TestSuite):
# timeout in seconds
def __init__(self, testfile, options, timeout=60.0):
self._testfile = testfile
self._timeout = timeout + 5.0
self._options = options
self._test = None
self._ignore_filters = [
# Ignore phantomjs warnings
"*** WARNING:",
# Disabled because of the 'web_hello' addon
"hello",
# Fixes an issue with PhantomJS 1.9.2 on OS X 10.9 (Mavericks)
# cf. https://github.com/ariya/phantomjs/issues/11418
"CoreText performance note",
]
def __iter__(self):
return iter([self])
def run(self, result):
# clean slate
if sql_db._Pool is not None:
sql_db._Pool.close_all(sql_db.dsn(tools.config['db_name']))
# check for PhantomJS...
try:
subprocess.call([ 'phantomjs', '-v' ], stdout=open(os.devnull, 'w'), stderr=subprocess.STDOUT)
except OSError:
test = WebsiteUiTest('UI Tests')
result.startTest(test)
result.addSkip(test, "phantomjs command not found (cf. http://phantomjs.org/)")
result.stopTest(test)
return
# ...then run the actual test
result._exc_info_to_string = _exc_info_to_string
try:
self._run(result)
finally:
del result._exc_info_to_string
def _run(self, result):
self._test = WebsiteUiTest("%s (as %s)" %
(self._testfile, self._options.get('user') or "Anonymous" if 'user' in self._options else "admin" ))
start_time = time.time()
last_check_time = time.time()
self._options['timeout'] = self._timeout
self._options['port'] = tools.config.get('xmlrpc_port', 80)
self._options['db'] = tools.config.get('db_name', '')
if 'user' not in self._options:
self._options['user'] = 'admin'
self._options['password'] = 'admin'
phantom = subprocess.Popen([
'phantomjs',
#'--debug=true',
self._testfile,
json.dumps(self._options)
], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
readable = [Stream(phantom.stdout.fileno())]
try:
output = []
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()
continue
for stream in ready:
lines = stream.readlines()
if lines is None: # EOF
filtered_lines = [line for line in output if not any(ignore in line for ignore in self._ignore_filters)]
if (filtered_lines):
self.process(filtered_lines, result)
readable.remove(stream)
else:
output += lines
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:
phantom.terminate()
result.stopTest(self._test)
def process(self, lines, result):
# Test protocol
# -------------
# use console.log in phantomjs to output test results using the following format:
# - for a success: { "event": "success" }
# - for an error: { "event": "error", "message": "Short error description" }
# the first line is treated as a JSON message (JSON should be formatted on one line)
# subsequent lines are displayed only if the first line indicated an error
# or if the first line was not a JSON message (still an error)
result.startTest(self._test)
try:
args = json.loads(lines[0])
event = args.get('event', None)
if event == 'success':
result.addSuccess(self._test)
elif event == 'error':
result.addError(self._test, args.get('message', "")+"\n"+"\n".join(lines[1::]))
else:
result.addError(self._test, 'Unexpected message: "%s"' % "\n".join(lines))
except ValueError:
result.addError(self._test, 'Unexpected message: "%s"' % "\n".join(lines))
def full_path(pyfile, filename):
return os.path.join(os.path.join(os.path.dirname(pyfile), 'ui_suite'), filename)
def load_tests(loader, base, _):
base.addTest(WebsiteUiSuite(full_path(__file__, 'dummy_test.js'), {}, 5.0))
base.addTest(WebsiteUiSuite(full_path(__file__, 'login_test.js'), {'path': '/', 'user': None}, 60.0))
base.addTest(WebsiteUiSuite(full_path(__file__, 'simple_dom_test.js'), {'redirect': '/page/website.homepage'}, 60.0))
base.addTest(WebsiteUiSuite(full_path(__file__, 'homepage_test.js'), {'redirect': '/page/website.homepage'}, 60.0))
return base

View File

@ -0,0 +1,6 @@
// Load helper
phantom.injectJs(phantom.args[0]);
pt = new PhantomTest();
pt.run_admin("/", "console.log('ok')", "window.openerp.website");

View File

@ -0,0 +1,6 @@
// Load helper
phantom.injectJs(phantom.args[0]);
pt = new PhantomTest();
pt.run_admin("/", "openerp.website.Tour.run_test('banner')", "openerp.website.Tour");

View File

@ -1,14 +0,0 @@
var testRunner = require('./ui_test_runner.js');
var waitFor = testRunner.waitFor;
testRunner.run(function simpleDomTest (page, timeout) {
waitFor(function clientReady () {
return page.evaluate(function () {
return window.openerp && window.openerp.website;
});
}, function finish () {
console.log('{ "event": "success" }');
phantom.exit();
}, timeout);
});

View File

@ -1,3 +0,0 @@
var testRunner = require('./ui_test_runner.js');
testRunner.run_test('banner');

View File

@ -1,7 +0,0 @@
var testRunner = require('./ui_test_runner.js');
testRunner.run_test('login_edit', {
"inject": [
"./../../../website/static/src/js/website.tour.test.js",
"./../../../website/static/src/js/website.tour.test.admin.js"]
});

View File

@ -0,0 +1,10 @@
// Load helper
phantom.injectJs(phantom.args[0]);
pt = new PhantomTest();
pt.inject = [
"./../../../website/static/src/js/website.tour.test.js",
"./../../../website/static/src/js/website.tour.test.admin.js"]
];
pt.run("/", "openerp.website.Tour.run_test('login_edit')", "openerp.website.Tour");

View File

@ -1,157 +0,0 @@
function waitFor (ready, callback, timeout, timeoutMessageCallback) {
timeout = timeout || 10000;
var start = new Date().getTime();
var condition = ready();
var interval = setInterval(function() {
if ((new Date().getTime() - start < timeout) && !condition ) {
condition = ready();
} else {
if(!condition) {
var message = timeoutMessageCallback ? timeoutMessageCallback() : "Timeout after "+timeout+" ms";
console.log('{ "event": "error", "message": "'+message+'" }');
console.log("Waiting for...\n"+ready);
phantom.exit(1);
} else {
clearInterval(interval);
callback();
}
}
}, 100);
}
function run (test, onload, inject) {
var options = JSON.parse(phantom.args);
var timeout = options.timeout ? Math.round(parseFloat(options.timeout)*1000-5000) : 60000;
var scheme = options.scheme ? options.scheme+'://' : 'http://';
var host = options.host ? options.host : 'localhost';
var port = options.port ? ':'+options.port : '';
var path = options.path ? options.path : '/web/login';
var queryParams = [];
if (options.db) queryParams.push('db='+options.db);
if (options.user) queryParams.push('login='+options.user);
if (options.password) queryParams.push('key='+options.password);
if (options.redirect) queryParams.push('redirect='+options.redirect);
var query = queryParams.length > 0 ? '?'+queryParams.join('&') : '';
var hashParams = [];
if (options.action) hashParams.push('action='+options.action);
var hash = hashParams.length > 0 ? '#'+hashParams.join('&') : '';
var url = scheme+host+port+path+query+hash;
var page = require('webpage').create();
page.viewportSize = { width: 1440, height: 900 };
page.onError = function(message, trace) {
console.log('{ "event": "error", "message": "'+message+'"}');
phantom.exit(1);
};
page.onAlert = function(message) {
console.log(message);
phantom.exit(1);
};
page.onConsoleMessage = function(message) {
console.log(message);
try {
var result = JSON.parse(message);
if (result.event === 'success') {
phantom.exit(0);
} else if (result.event === 'console') {
return;
} else {
phantom.exit(1);
}
} catch (exception) {
phantom.exit(1);
}
};
page.onCallback = function(data) {
if (data.event && data.event === 'start') {
if (test) test(page, timeout, options);
}
};
page.onLoadFinished = function(status) {
if (status === "success") {
if (inject) {
if (!inject instanceof Array) {
inject = [inject];
}
for (var k in inject) {
if(!page.injectJs(inject[k])) {
console.log("Can't inject "+inject[k]);
phantom.exit(1);
}
}
}
if (onload) onload(page, timeout, options);
}
};
var maxRetries = 10;
var retryDelay = 1000; // ms
var tries = 0;
page.open(url, function openPage (status) {
if (status !== 'success') {
tries++;
if (tries < maxRetries) {
setTimeout(function () {
page.open(url, openPage);
}, retryDelay);
} else {
console.log('{ "event": "error", "message": "'+url+' failed to load '+tries+' times ('+status+')"}');
phantom.exit(1);
}
} else {
page.evaluate(function () {
window.callPhantom({ event: 'start' });
});
}
});
setTimeout(function () {
page.evaluate(function (timeout) {
var message = ("Timeout after " +(timeout/1000)+ " s"
+ "\nhref: " + window.location.href
+ "\nreferrer: " + document.referrer
+ "\n\n" + document.body.innerHTML).replace(/[^a-z0-9\s~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, "*");
console.log(JSON.stringify({ "event": "error", "message": message}));
phantom.exit(1);
},timeout);
}, timeout);
}
function run_test (testname, options) {
options = options || {};
run(
function start (page, timeout) {
page.evaluate(function () { localStorage.clear(); });
},
function onload (page, timeout) {
waitFor(function clientReady () {
return page.evaluate(function () {
return window.$
&& window.openerp
&& window.openerp.website
&& window.openerp.website.Tour;
});
}, function executeTest () {
page.evaluate(function (testname) {
window.openerp.website.Tour.run_test(testname);
}, testname);
}, timeout);
},
options.inject || null
);
}
module.exports = {
waitFor: waitFor,
run: run,
run_test: run_test
}