2014-02-09 22:39:17 +00:00
|
|
|
// Phantomjs openerp helper
|
|
|
|
|
|
|
|
function waitFor (ready, callback, timeout, timeoutMessageCallback) {
|
|
|
|
timeout = timeout || 10000;
|
2014-03-10 11:24:01 +00:00
|
|
|
var start = new Date;
|
|
|
|
|
|
|
|
(function waitLoop() {
|
|
|
|
if(new Date - start > timeout) {
|
|
|
|
error(timeoutMessageCallback ? timeoutMessageCallback() : "Timeout after "+timeout+" ms");
|
|
|
|
} else if (ready()) {
|
|
|
|
callback();
|
2014-02-09 22:39:17 +00:00
|
|
|
} else {
|
2014-03-10 11:24:01 +00:00
|
|
|
setTimeout(waitLoop, 250);
|
2014-02-09 22:39:17 +00:00
|
|
|
}
|
2014-03-10 11:24:01 +00:00
|
|
|
}());
|
2014-02-09 22:39:17 +00:00
|
|
|
}
|
|
|
|
|
2014-02-19 12:38:50 +00:00
|
|
|
function error(message) {
|
|
|
|
console.log('error', message);
|
|
|
|
phantom.exit(1);
|
|
|
|
}
|
2014-02-09 22:39:17 +00:00
|
|
|
function PhantomTest() {
|
|
|
|
var self = this;
|
2014-02-16 21:22:22 +00:00
|
|
|
this.options = JSON.parse(phantom.args[phantom.args.length-1]);
|
2014-02-17 00:01:50 +00:00
|
|
|
this.inject = this.options.inject || [];
|
2014-02-09 22:39:17 +00:00
|
|
|
this.timeout = this.options.timeout ? Math.round(parseFloat(this.options.timeout)*1000 - 5000) : 10000;
|
|
|
|
this.origin = 'http://localhost';
|
|
|
|
this.origin += this.options.port ? ':' + this.options.port : '';
|
|
|
|
|
|
|
|
// ----------------------------------------------------
|
2014-02-16 21:22:22 +00:00
|
|
|
// configure phantom and page
|
2014-02-09 22:39:17 +00:00
|
|
|
// ----------------------------------------------------
|
2014-02-16 21:22:22 +00:00
|
|
|
phantom.addCookie({
|
2014-02-09 22:39:17 +00:00
|
|
|
'domain': 'localhost',
|
|
|
|
'name': 'session_id',
|
|
|
|
'value': this.options.session_id,
|
|
|
|
});
|
2014-02-16 21:22:22 +00:00
|
|
|
this.page = require('webpage').create();
|
|
|
|
this.page.viewportSize = { width: 1366, height: 768 };
|
2014-02-09 22:39:17 +00:00
|
|
|
this.page.onError = function(message, trace) {
|
2014-02-19 12:38:50 +00:00
|
|
|
var msg = [message];
|
|
|
|
if (trace && trace.length) {
|
|
|
|
msg.push.apply(msg, trace.map(function (frame) {
|
|
|
|
var result = [' at ', frame.file, ':', frame.line];
|
|
|
|
if (frame.function) {
|
|
|
|
result.push(' (in ', frame.function, ')');
|
|
|
|
}
|
|
|
|
return result.join('');
|
|
|
|
}));
|
|
|
|
msg.push('(leaf frame on top)')
|
|
|
|
}
|
|
|
|
error(JSON.stringify(msg.join('\n')));
|
2014-02-09 22:39:17 +00:00
|
|
|
};
|
|
|
|
this.page.onAlert = function(message) {
|
2014-02-19 12:38:50 +00:00
|
|
|
error(message);
|
2014-02-09 22:39:17 +00:00
|
|
|
};
|
|
|
|
this.page.onConsoleMessage = function(message) {
|
|
|
|
console.log(message);
|
|
|
|
};
|
|
|
|
this.page.onLoadFinished = function(status) {
|
|
|
|
if (status === "success") {
|
|
|
|
for (var k in self.inject) {
|
2014-02-17 00:01:50 +00:00
|
|
|
var found = false;
|
|
|
|
var v = self.inject[k];
|
|
|
|
var need = v;
|
|
|
|
var src = v;
|
|
|
|
if (v[0]) {
|
|
|
|
need = v[0];
|
|
|
|
src = v[1];
|
|
|
|
found = self.page.evaluate(function(code) {
|
|
|
|
try {
|
|
|
|
return !!eval(code);
|
|
|
|
} catch (e) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}, need);
|
2014-02-10 13:19:10 +00:00
|
|
|
}
|
2014-02-17 00:01:50 +00:00
|
|
|
if(!found) {
|
|
|
|
console.log('Injecting', src, 'needed for', need);
|
|
|
|
if(!self.page.injectJs(src)) {
|
2014-02-19 12:38:50 +00:00
|
|
|
error("Cannot inject " + src);
|
2014-02-17 00:01:50 +00:00
|
|
|
}
|
2014-02-09 22:39:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
setTimeout(function () {
|
|
|
|
self.page.evaluate(function () {
|
|
|
|
var message = ("Timeout\nhref: " + window.location.href
|
|
|
|
+ "\nreferrer: " + document.referrer
|
|
|
|
+ "\n\n" + document.body.innerHTML).replace(/[^a-z0-9\s~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, "*");
|
2014-02-19 12:38:50 +00:00
|
|
|
error(message);
|
2014-02-09 22:39:17 +00:00
|
|
|
});
|
|
|
|
}, self.timeout);
|
|
|
|
|
|
|
|
// ----------------------------------------------------
|
|
|
|
// run test
|
|
|
|
// ----------------------------------------------------
|
|
|
|
this.run = function(url_path, code, ready) {
|
2014-02-10 00:45:17 +00:00
|
|
|
if(self.options.login) {
|
|
|
|
qp = [];
|
|
|
|
qp.push('db=' + self.options.db);
|
|
|
|
qp.push('login=' + self.options.login);
|
|
|
|
qp.push('key=' + self.options.password);
|
|
|
|
qp.push('redirect=' + encodeURIComponent(url_path));
|
2014-02-16 21:22:22 +00:00
|
|
|
var url_path = "/login?" + qp.join('&');
|
2014-02-10 00:45:17 +00:00
|
|
|
}
|
2014-02-09 22:39:17 +00:00
|
|
|
var url = self.origin + url_path;
|
|
|
|
self.page.open(url, function(status) {
|
|
|
|
if (status !== 'success') {
|
2014-02-19 12:38:50 +00:00
|
|
|
error("failed to load " + url)
|
2014-02-09 22:39:17 +00:00
|
|
|
} else {
|
|
|
|
console.log('loaded', url, status);
|
|
|
|
// process ready
|
|
|
|
waitFor(function() {
|
2014-03-10 11:24:01 +00:00
|
|
|
return self.page.evaluate(function (ready) {
|
|
|
|
console.log("waiting for", ready);
|
2014-02-09 22:39:17 +00:00
|
|
|
var r = false;
|
|
|
|
try {
|
|
|
|
r = !!eval(ready);
|
2014-03-10 11:24:01 +00:00
|
|
|
} catch(ex) { }
|
2014-02-09 22:39:17 +00:00
|
|
|
return r;
|
|
|
|
}, ready);
|
|
|
|
// run test
|
|
|
|
}, function() {
|
|
|
|
self.page.evaluate(function (code) { return eval(code); }, code);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2014-02-10 00:45:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// js mode or jsfile mode
|
|
|
|
if(phantom.args.length === 1) {
|
|
|
|
pt = new PhantomTest();
|
|
|
|
pt.run(pt.options.url_path, pt.options.code, pt.options.ready);
|
2014-02-09 22:39:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// vim:et:
|