2013-12-18 14:56:28 +00:00
|
|
|
import unittest
|
2013-12-23 10:00:03 +00:00
|
|
|
import subprocess
|
2013-12-18 14:56:28 +00:00
|
|
|
import os
|
2013-12-23 10:00:03 +00:00
|
|
|
import select
|
2013-12-18 14:56:28 +00:00
|
|
|
import time
|
2013-12-23 10:00:03 +00:00
|
|
|
import json
|
|
|
|
from openerp import tools
|
2013-12-18 14:56:28 +00:00
|
|
|
|
|
|
|
__all__ = ['load_tests', 'WebsiteUiSuite']
|
|
|
|
|
2013-12-31 14:28:24 +00:00
|
|
|
# TODO according to al this should be one line of Python
|
2013-12-23 10:17:01 +00:00
|
|
|
class LineReader:
|
2013-12-23 16:22:34 +00:00
|
|
|
def __init__(self, file_descriptor):
|
|
|
|
self._file_descriptor = file_descriptor
|
|
|
|
self._buffer = ''
|
2013-12-23 10:00:03 +00:00
|
|
|
|
|
|
|
def fileno(self):
|
2013-12-23 16:22:34 +00:00
|
|
|
return self._file_descriptor
|
2013-12-23 10:00:03 +00:00
|
|
|
|
|
|
|
def readlines(self):
|
2013-12-23 16:22:34 +00:00
|
|
|
data = os.read(self._file_descriptor, 4096)
|
2013-12-23 10:00:03 +00:00
|
|
|
if not data:
|
|
|
|
# EOF
|
|
|
|
return None
|
2013-12-23 16:22:34 +00:00
|
|
|
self._buffer += data
|
2013-12-23 10:00:03 +00:00
|
|
|
if '\n' not in data:
|
|
|
|
return []
|
2013-12-23 16:22:34 +00:00
|
|
|
tmp = self._buffer.split('\n')
|
|
|
|
lines, self._buffer = tmp[:-1], tmp[-1]
|
2013-12-23 10:00:03 +00:00
|
|
|
return lines
|
|
|
|
|
2013-12-18 14:56:28 +00:00
|
|
|
class WebsiteUiTest(unittest.TestCase):
|
|
|
|
def __init__(self, name):
|
|
|
|
self.name = name
|
|
|
|
def shortDescription(self):
|
|
|
|
return None
|
|
|
|
def __str__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
class WebsiteUiSuite(unittest.TestSuite):
|
2013-12-23 16:22:34 +00:00
|
|
|
# timeout in seconds
|
2013-12-27 16:58:24 +00:00
|
|
|
def __init__(self, testfile, options, timeout=60.0):
|
2013-12-23 16:51:13 +00:00
|
|
|
self._testfile = testfile
|
|
|
|
self._timeout = timeout
|
|
|
|
self._options = options
|
2013-12-18 14:56:28 +00:00
|
|
|
self._test = None
|
|
|
|
|
|
|
|
def __iter__(self):
|
|
|
|
return iter([self])
|
|
|
|
|
|
|
|
def run(self, result):
|
2013-12-31 14:28:24 +00:00
|
|
|
# check for PhantomJS...
|
2013-12-18 14:56:28 +00:00
|
|
|
try:
|
2013-12-31 14:28:24 +00:00
|
|
|
subprocess.call([ 'phantomjs', '-v' ], stdout=open(os.devnull, 'w'), stderr=subprocess.STDOUT)
|
2013-12-18 14:56:28 +00:00
|
|
|
except OSError:
|
|
|
|
test = WebsiteUiTest('UI Tests')
|
|
|
|
result.startTest(test)
|
|
|
|
result.addSkip(test, "phantomjs command not found")
|
|
|
|
result.stopTest(test)
|
|
|
|
return
|
2013-12-31 14:28:24 +00:00
|
|
|
# ...then run the actual test
|
|
|
|
self._run(result)
|
2013-12-18 14:56:28 +00:00
|
|
|
|
|
|
|
def _run(self, result):
|
2013-12-23 16:51:13 +00:00
|
|
|
self._test = WebsiteUiTest(self._testfile)
|
|
|
|
start_time = time.time()
|
2013-12-23 10:28:26 +00:00
|
|
|
last_check_time = time.time()
|
2013-12-23 10:00:03 +00:00
|
|
|
|
2013-12-23 16:51:13 +00:00
|
|
|
self._options['timeout'] = self._timeout
|
2013-12-27 16:58:24 +00:00
|
|
|
self._options['port'] = tools.config.get('xmlrpc_port', 80)
|
2013-12-27 11:25:54 +00:00
|
|
|
self._options['db'] = tools.config.get('db_name', '')
|
2013-12-31 14:28:24 +00:00
|
|
|
# TODO use correct key from tools if exists (I could not find it --ddm)
|
2013-12-27 11:25:54 +00:00
|
|
|
self._options['user'] = 'admin'
|
2013-12-30 13:13:22 +00:00
|
|
|
self._options['admin_password'] = tools.config.get('admin_passwd', 'admin')
|
2013-12-23 10:28:26 +00:00
|
|
|
|
2013-12-18 14:56:28 +00:00
|
|
|
phantom = subprocess.Popen([
|
2013-12-31 14:28:24 +00:00
|
|
|
'phantomjs',
|
|
|
|
os.path.join(os.path.join(os.path.dirname(__file__), 'ui_suite'), self._testfile),
|
|
|
|
json.dumps(self._options)
|
|
|
|
], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
2013-12-23 10:00:03 +00:00
|
|
|
proc_stdout = LineReader(phantom.stdout.fileno())
|
|
|
|
readable = [proc_stdout]
|
2013-12-18 14:56:28 +00:00
|
|
|
try:
|
2013-12-23 16:51:13 +00:00
|
|
|
while phantom.poll() is None and readable and last_check_time < start_time + self._timeout:
|
2013-12-23 10:28:26 +00:00
|
|
|
ready, _, _ = select.select(readable, [], [], 0.1)
|
2013-12-23 10:00:03 +00:00
|
|
|
if not ready:
|
|
|
|
last_check_time = time.time()
|
|
|
|
continue
|
|
|
|
for stream in ready:
|
|
|
|
lines = stream.readlines()
|
|
|
|
if lines is None:
|
2013-12-23 16:22:34 +00:00
|
|
|
# EOF
|
2013-12-23 10:00:03 +00:00
|
|
|
readable.remove(stream)
|
2013-12-23 13:54:25 +00:00
|
|
|
else:
|
2013-12-27 13:44:36 +00:00
|
|
|
self.process(lines, result)
|
2013-12-23 13:54:25 +00:00
|
|
|
readable.remove(stream)
|
2013-12-23 16:51:13 +00:00
|
|
|
if last_check_time >= (start_time + self._timeout):
|
|
|
|
result.addError(self._test, "Timeout after %s s" % (last_check_time - start_time ))
|
2013-12-18 14:56:28 +00:00
|
|
|
finally:
|
2013-12-18 15:23:52 +00:00
|
|
|
# kill phantomjs if phantom.exit() wasn't called in the test
|
2013-12-18 14:56:28 +00:00
|
|
|
if phantom.poll() is None:
|
|
|
|
phantom.terminate()
|
2013-12-23 13:54:25 +00:00
|
|
|
result.stopTest(self._test)
|
2013-12-18 14:56:28 +00:00
|
|
|
|
2013-12-27 13:44:36 +00:00
|
|
|
def process(self, lines, result):
|
2013-12-18 14:56:28 +00:00
|
|
|
# Test protocol
|
|
|
|
# -------------
|
|
|
|
# use console.log in phantomjs to output test results using the following format:
|
|
|
|
# - for a success: { "event": "success" }
|
2013-12-31 14:28:24 +00:00
|
|
|
# - for an error: { "event": "error", "message": "Short error description" }
|
2013-12-27 13:53:30 +00:00
|
|
|
# the first line is treated as a JSON message (JSON should be formatted on one line)
|
2013-12-27 13:44:36 +00:00
|
|
|
# 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)
|
2013-12-18 15:23:52 +00:00
|
|
|
result.startTest(self._test)
|
|
|
|
try:
|
2013-12-27 13:44:36 +00:00
|
|
|
args = json.loads(lines[0])
|
2013-12-23 10:00:03 +00:00
|
|
|
event = args.get('event', None)
|
2013-12-18 15:23:52 +00:00
|
|
|
if event == 'success':
|
|
|
|
result.addSuccess(self._test)
|
2013-12-23 16:22:34 +00:00
|
|
|
elif event == 'error':
|
|
|
|
message = args.get('message', "")
|
2013-12-27 13:44:36 +00:00
|
|
|
result.addError(self._test, message+"\n"+"\n".join(lines[1::]))
|
2013-12-18 15:23:52 +00:00
|
|
|
else:
|
2013-12-27 13:44:36 +00:00
|
|
|
result.addError(self._test, 'Unexpected message: "%s"' % "\n".join(lines))
|
2013-12-18 15:23:52 +00:00
|
|
|
except ValueError:
|
2013-12-27 13:44:36 +00:00
|
|
|
result.addError(self._test, 'Unexpected message: "%s"' % "\n".join(lines))
|
2013-12-18 14:56:28 +00:00
|
|
|
|
|
|
|
def load_tests(loader, base, _):
|
2013-12-23 16:51:13 +00:00
|
|
|
base.addTest(WebsiteUiSuite('dummy_test.js', {}))
|
2013-12-30 16:30:32 +00:00
|
|
|
base.addTest(WebsiteUiSuite('simple_dom_test.js', { 'action': 'website.action_website_homepage' }, 120.0))
|
|
|
|
base.addTest(WebsiteUiSuite('homepage_test.js', { 'action': 'website.action_website_homepage' }, 120.0))
|
2013-12-23 16:57:08 +00:00
|
|
|
return base
|