1 // Phantomjs openerp helper
3 function waitFor (ready, callback, timeout, timeoutMessageCallback) {
4 timeout = timeout || 10000;
8 if(new Date - start > timeout) {
9 console.log('error', timeoutMessageCallback ? timeoutMessageCallback() : "Timeout after "+timeout+" ms");
14 setTimeout(waitLoop, 250);
19 function PhantomTest() {
21 this.options = JSON.parse(phantom.args[phantom.args.length-1]);
22 this.inject = this.options.inject || [];
23 this.timeout = this.options.timeout ? Math.round(parseFloat(this.options.timeout)*1000 - 5000) : 10000;
24 this.origin = 'http://localhost';
25 this.origin += this.options.port ? ':' + this.options.port : '';
27 // ----------------------------------------------------
28 // configure phantom and page
29 // ----------------------------------------------------
31 'domain': 'localhost',
33 'value': this.options.session_id,
35 this.page = require('webpage').create();
36 this.page.viewportSize = { width: 1366, height: 768 };
37 this.page.onError = function(message, trace) {
39 if (trace && trace.length) {
40 msg.push.apply(msg, trace.map(function (frame) {
41 var result = [' at ', frame.file, ':', frame.line];
43 result.push(' (in ', frame.function, ')');
45 return result.join('');
47 msg.push('(leaf frame on top)')
49 console.log('error', JSON.stringify(msg.join('\n')));
52 this.page.onAlert = function(message) {
53 console.log('error', message);
56 this.page.onConsoleMessage = function(message) {
59 this.page.onLoadFinished = function(status) {
60 if (status === "success") {
61 for (var k in self.inject) {
63 var v = self.inject[k];
69 found = self.page.evaluate(function(code) {
78 console.log('Injecting', src, 'needed for', need);
79 if(!self.page.injectJs(src)) {
80 console.log('error', "Cannot inject " + src);
87 setTimeout(function () {
88 self.page.evaluate(function () {
89 var message = ("Timeout\nhref: " + window.location.href
90 + "\nreferrer: " + document.referrer
91 + "\n\n" + (document.body && document.body.innerHTML)).replace(/[^a-z0-9\s~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, "*");
92 console.log('error', message);
97 // ----------------------------------------------------
99 // ----------------------------------------------------
100 this.run = function(url_path, code, ready) {
101 if(self.options.login) {
103 qp.push('db=' + self.options.db);
104 qp.push('login=' + self.options.login);
105 qp.push('key=' + self.options.password);
106 qp.push('redirect=' + encodeURIComponent(url_path));
107 url_path = "/login?" + qp.join('&');
109 var url = self.origin + url_path;
110 self.page.open(url, function(status) {
111 if (status !== 'success') {
112 console.log('error', "failed to load " + url);
115 console.log('loaded', url, status);
118 console.log("PhantomTest.run: wait for condition: " + ready);
119 return self.page.evaluate(function (ready) {
122 console.log("page.evaluate eval expr:", ready);
126 console.log("page.evaluate eval result:", r);
131 console.log("PhantomTest.run: condition statified, executing: " + code);
132 self.page.evaluate(function (code) { return eval(code); }, code);
133 console.log("PhantomTest.run: execution launched, waiting for console.log('ok')...");
140 // js mode or jsfile mode
141 if(phantom.args.length === 1) {
142 pt = new PhantomTest();
143 pt.run(pt.options.url_path, pt.options.code, pt.options.ready);