self.phantom_js()¶
From odoo/tests/common.py:
def phantom_js(self, url_path, code, ready="window", login=None, timeout=60, **kw):
""" Test js code running in the browser
- optionnally log as 'login'
- load page given by url_path
- wait for ready object to be available
- eval(code) inside the page
To signal success test do:
console.log('ok')
To signal failure do:
console.log('error')
If neither are done before timeout test fails.
"""
i.e.
odoo first loads
url_pathas userlogin(e.g.'admin','demo'etc.) or as non-authed userthen waits for
readycondition, i.e. when some js variable (e.g.window) become truthythen executes js
codethen wait for one of condition:
someone prints
console.log('ok')– test passedsomeone prints
console.log('error')– test failedtimeoutseconds are passed – test failed
Example¶
Example from mail_sent:
# -*- coding: utf-8 -*-
import odoo.tests
@odoo.tests.common.at_install(False)
@odoo.tests.common.post_install(True)
class TestUi(odoo.tests.HttpCase):
def test_01_mail_sent(self):
# wait till page loaded and then click and wait again
code = """
setTimeout(function () {
$(".mail_sent").click();
setTimeout(function () {console.log('ok');}, 3000);
}, 1000);
"""
link = '/web#action=%s' % self.ref('mail.mail_channel_action_client_chat')
self.phantom_js(link, code, "odoo.__DEBUG__.services['mail_sent.sent'].is_ready", login="demo")
In this test:
odoo first loads
/web#action=...pagethen waits for
odoo.__DEBUG__.services['mail_sent.sent'].is_readyodoo.__DEBUG__.services['mail_sent.sent']is similar torequire('mail_sent.sent')is_readyis a variable in sent.js
then executes js
code:setTimeout(function () { $(".mail_sent").click(); setTimeout(function () {console.log('ok');}, 3000); }, 1000);
which clicks on
Sentmenu and gives to the page 3 seconds to load it.This code neither throws errors (e.g. via
throw new Error('Some error description')nor logconsole.log('error'), but you can add ones to your code to catch failed cases you need.then if everything is ok, odoo get message
console.log('ok')