[IMP] don't reimplement lazy_property by hand when we already have one
[odoo/odoo.git] / openerp / tests / phantomtest.js
1 // Phantomjs openerp helper
2
3 function waitFor (ready, callback, timeout, timeoutMessageCallback) {
4     timeout = timeout || 10000;
5     var start = new Date;
6
7     (function waitLoop() {
8         if(new Date - start > timeout) {
9             console.log('error', timeoutMessageCallback ? timeoutMessageCallback() : "Timeout after "+timeout+" ms");
10             phantom.exit(1);
11         } else if (ready()) {
12             callback();
13         } else {
14             setTimeout(waitLoop, 250);
15         }
16     }());
17 }
18
19 function PhantomTest() {
20     var self = this;
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 : '';
26
27     // ----------------------------------------------------
28     // configure phantom and page
29     // ----------------------------------------------------
30     phantom.addCookie({
31         'domain': 'localhost',
32         'name': 'session_id',
33         'value': this.options.session_id,
34     });
35     this.page = require('webpage').create();
36     this.page.viewportSize = { width: 1366, height: 768 };
37     this.page.onError = function(message, trace) {
38         var msg = [message];
39         if (trace && trace.length) {
40             msg.push.apply(msg, trace.map(function (frame) {
41                 var result = [' at ', frame.file, ':', frame.line];
42                 if (frame.function) {
43                     result.push(' (in ', frame.function, ')');
44                 }
45                 return result.join('');
46             }));
47             msg.push('(leaf frame on top)')
48         }
49         console.log('error', JSON.stringify(msg.join('\n')));
50         phantom.exit(1);
51     };
52     this.page.onAlert = function(message) {
53         console.log('error', message);
54         phantom.exit(1);
55     };
56     this.page.onConsoleMessage = function(message) {
57         console.log(message);
58     };
59     this.page.onLoadFinished = function(status) {
60         if (status === "success") {
61             for (var k in self.inject) {
62                 var found = false;
63                 var v = self.inject[k];
64                 var need = v;
65                 var src = v;
66                 if (v[0]) {
67                     need = v[0];
68                     src = v[1];
69                     found = self.page.evaluate(function(code) {
70                         try {
71                             return !!eval(code);
72                         } catch (e) {
73                             return false;
74                         }
75                     }, need);
76                 }
77                 if(!found) {
78                     console.log('Injecting', src, 'needed for', need);
79                     if(!self.page.injectJs(src)) {
80                         console.log('error', "Cannot inject " + src);
81                         phantom.exit(1);
82                     }
83                 }
84             }
85         }
86     };
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);
93             phantom.exit(1);
94         });
95     }, self.timeout);
96
97     // ----------------------------------------------------
98     // run test
99     // ----------------------------------------------------
100     this.run = function(url_path, code, ready) {
101         if(self.options.login) {
102             var qp = [];
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('&');
108         }
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);
113                 phantom.exit(1);
114             } else {
115                 console.log('loaded', url, status);
116                 // process ready
117                 waitFor(function() {
118                     console.log("PhantomTest.run: wait for condition: " + ready);
119                     return self.page.evaluate(function (ready) {
120                         var r = false;
121                         try {
122                             console.log("page.evaluate eval expr:", ready);
123                             r = !!eval(ready);
124                         } catch(ex) {
125                         }
126                         console.log("page.evaluate eval result:", r);
127                         return r;
128                     }, ready);
129                 // run test
130                 }, function() {
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')...");
134                 });
135             }
136         });
137     };
138 }
139
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);
144 }
145
146 // vim:et: