~openerp-dev/openobject-server/trunk-imp-onchange-behave-darshan

« back to all changes in this revision

Viewing changes to openerp/tests/phantomtest.js

  • Committer: Darshan Kalola(OpenERP)
  • Date: 2014-04-09 08:44:52 UTC
  • mfrom: (4936.1.232 openobject-server)
  • Revision ID: dka@tinyerp.com-20140409084452-w1e499j21i3eli9d
[MERGE]sync with trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
2
2
 
3
3
function waitFor (ready, callback, timeout, timeoutMessageCallback) {
4
4
    timeout = timeout || 10000;
5
 
    var start = new Date().getTime();
6
 
    var condition = ready();
7
 
    var interval = setInterval(function() {
8
 
        if ((new Date().getTime() - start < timeout) && !condition ) {
9
 
            condition = ready();
 
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();
10
13
        } else {
11
 
            if(!condition) {
12
 
                var message = timeoutMessageCallback ? timeoutMessageCallback() : "Timeout after "+timeout+" ms";
13
 
                console.log("Waiting for " + ready);
14
 
                error(message);
15
 
            } else {
16
 
                clearInterval(interval);
17
 
                callback();
18
 
            }
 
14
            setTimeout(waitLoop, 250);
19
15
        }
20
 
    }, 250);
 
16
    }());
21
17
}
22
18
 
23
 
function error(message) {
24
 
    console.log('error', message);
25
 
    phantom.exit(1);
26
 
}
27
19
function PhantomTest() {
28
20
    var self = this;
29
21
    this.options = JSON.parse(phantom.args[phantom.args.length-1]);
54
46
            }));
55
47
            msg.push('(leaf frame on top)')
56
48
        }
57
 
        error(JSON.stringify(msg.join('\n')));
 
49
        console.log('error', JSON.stringify(msg.join('\n')));
 
50
        phantom.exit(1);
58
51
    };
59
52
    this.page.onAlert = function(message) {
60
 
        error(message);
 
53
        console.log('error', message);
 
54
        phantom.exit(1);
61
55
    };
62
56
    this.page.onConsoleMessage = function(message) {
63
57
        console.log(message);
83
77
                if(!found) {
84
78
                    console.log('Injecting', src, 'needed for', need);
85
79
                    if(!self.page.injectJs(src)) {
86
 
                        error("Cannot inject " + src);
 
80
                        console.log('error', "Cannot inject " + src);
 
81
                        phantom.exit(1);
87
82
                    }
88
83
                }
89
84
            }
93
88
        self.page.evaluate(function () {
94
89
            var message = ("Timeout\nhref: " + window.location.href
95
90
                + "\nreferrer: " + document.referrer
96
 
                + "\n\n" + document.body.innerHTML).replace(/[^a-z0-9\s~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, "*");
97
 
            error(message);
 
91
                + "\n\n" + (document.body && document.body.innerHTML)).replace(/[^a-z0-9\s~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, "*");
 
92
            console.log('error', message);
 
93
            phantom.exit(1);
98
94
        });
99
95
    }, self.timeout);
100
96
 
103
99
    // ----------------------------------------------------
104
100
    this.run = function(url_path, code, ready) {
105
101
        if(self.options.login) {
106
 
            qp = [];
 
102
            var qp = [];
107
103
            qp.push('db=' + self.options.db);
108
104
            qp.push('login=' + self.options.login);
109
105
            qp.push('key=' + self.options.password);
110
106
            qp.push('redirect=' + encodeURIComponent(url_path));
111
 
            var url_path = "/login?" + qp.join('&');
 
107
            url_path = "/login?" + qp.join('&');
112
108
        }
113
109
        var url = self.origin + url_path;
114
110
        self.page.open(url, function(status) {
115
111
            if (status !== 'success') {
116
 
                error("failed to load " + url)
 
112
                console.log('error', "failed to load " + url);
 
113
                phantom.exit(1);
117
114
            } else {
118
115
                console.log('loaded', url, status);
119
116
                // process ready
120
117
                waitFor(function() {
121
 
                    return self.page.evaluate(function (ready) { 
 
118
                    console.log("PhantomTest.run: wait for condition: " + ready);
 
119
                    return self.page.evaluate(function (ready) {
122
120
                        var r = false;
123
121
                        try {
 
122
                            console.log("page.evaluate eval expr:", ready);
124
123
                            r = !!eval(ready);
125
124
                        } catch(ex) {
126
 
                            console.log("waiting for " + ready);
127
125
                        }
 
126
                        console.log("page.evaluate eval result:", r);
128
127
                        return r;
129
128
                    }, ready);
130
129
                // run test
131
130
                }, function() {
 
131
                    console.log("PhantomTest.run: condition statified, executing: " + code);
132
132
                    self.page.evaluate(function (code) { return eval(code); }, code);
 
133
                    console.log("PhantomTest.run: execution launched, waiting for console.log('ok')...");
133
134
                });
134
135
            }
135
136
        });