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 ) {
8
if(new Date - start > timeout) {
9
console.log('error', timeoutMessageCallback ? timeoutMessageCallback() : "Timeout after "+timeout+" ms");
12
var message = timeoutMessageCallback ? timeoutMessageCallback() : "Timeout after "+timeout+" ms";
13
console.log("Waiting for " + ready);
16
clearInterval(interval);
14
setTimeout(waitLoop, 250);
23
function error(message) {
24
console.log('error', message);
27
19
function PhantomTest() {
29
21
this.options = JSON.parse(phantom.args[phantom.args.length-1]);
55
47
msg.push('(leaf frame on top)')
57
error(JSON.stringify(msg.join('\n')));
49
console.log('error', JSON.stringify(msg.join('\n')));
59
52
this.page.onAlert = function(message) {
53
console.log('error', message);
62
56
this.page.onConsoleMessage = function(message) {
63
57
console.log(message);
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);
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, "*");
91
+ "\n\n" + (document.body && document.body.innerHTML)).replace(/[^a-z0-9\s~!@#$%^&*()_|+\-=?;:'",.<>\{\}\[\]\\\/]/gi, "*");
92
console.log('error', message);
103
99
// ----------------------------------------------------
104
100
this.run = function(url_path, code, ready) {
105
101
if(self.options.login) {
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('&');
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);
118
115
console.log('loaded', url, status);
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
console.log("page.evaluate eval expr:", ready);
124
123
r = !!eval(ready);
126
console.log("waiting for " + ready);
126
console.log("page.evaluate eval result:", r);
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')...");