2 function progress(work) {
7 * Wait until the test condition is true or a timeout occurs. Useful for waiting
8 * on a server response or for a ui change (fadeIn, etc.) to occur.
10 * @param testFx javascript condition that evaluates to a boolean,
11 * it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or
12 * as a callback function.
13 * @param onReady what to do when testFx condition is fulfilled,
14 * it can be passed in as a string (e.g.: "1 == 1" or "$('#bar').is(':visible')" or
15 * as a callback function.
16 * @param timeOutMillis the max amount of time to wait. If not specified, 5 min is used.
18 function waitFor(testFx, onReady, timeOutMillis) {
19 var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 300000, //< Default Max Timout is 5 min
20 start = new Date().getTime(),
22 var interval = setInterval(function() {
23 if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
24 // If not time-out yet and condition not yet fulfilled
25 condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
31 // Condition fulfilled (timeout and/or condition is 'true')
32 typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled
33 clearInterval(interval); //< Stop this interval
36 }, 250); //< repeat check every 250ms
39 phantom.onError = function(msg, trace) {
40 var msgStack = ['PHANTOM ERROR: ' + msg];
41 if (trace && trace.length) {
42 msgStack.push('TRACE:');
43 trace.forEach(function(t) {
44 msgStack.push(' -> ' + (t.file || t.sourceURL) + ': ' + t.line + (t.function ? ' (in function ' + t.function +')' : ''));
47 console.error(msgStack.join('\n'));
51 var page = require('webpage').create();
55 progress('#initialized');
60 orientation: "portrait",
64 page.zoomFactor = 1.57
66 page.open('%%url', function (status) {
68 page.render('%%file', {quality: '100'});
70 progress('#rendered');