Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function waitFor(testFx, onReady, timeOutMillis) {
- var maxtimeOutMillis = timeOutMillis ? timeOutMillis : 3000, //< Default Max Timout is 3s
- start = new Date().getTime(),
- condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()), //< defensive code
- interval = setInterval(function() {
- if ( (new Date().getTime() - start < maxtimeOutMillis) && !condition ) {
- // If not time-out yet and condition not yet fulfilled
- condition = (typeof(testFx) === "string" ? eval(testFx) : testFx()); //< defensive code
- } else {
- if(!condition) {
- // If condition still not fulfilled (timeout but condition is 'false')
- console.log("'waitFor()' timeout");
- phantom.exit(1);
- } else {
- // Condition fulfilled (timeout and/or condition is 'true')
- console.log("'waitFor()' finished in " + (new Date().getTime() - start) + "ms.");
- typeof(onReady) === "string" ? eval(onReady) : onReady(); //< Do what it's supposed to do once the condition is fulfilled
- clearInterval(interval); //< Stop this interval
- }
- }
- }, 250); //< repeat check every 250ms
- };
- var system = require('system'), page = require('webpage').create(), url, output, waitTime, pwidth = 1400, pheight = 700;
- console.log(system.args+"\n");
- function isInt(value) {
- return !isNaN(value) && parseInt(Number(value)) == value && !isNaN(parseInt(value, 10));
- }
- if(system.args.length < 4 || system.args.length > 6){
- console.log("Invalid parameter number, Usage: phantomjs script.js url output(e.g file.png) waitTime width(optional) height(option) \n Default size is {width: 1400, height: 700}");
- phantom.exit();
- }
- url = system.args[1];
- output = system.args[2];
- waitTime = system.args[3];
- if(system.args.length == 6 && isInt(system.args[4]) && isInt(system.args[5])){
- pwidth = system.args[4];
- pheight = system.args[5];
- }
- var resources = [];
- page.onResourceRequested = function(request) {
- resources[request.id] = request.stage;
- };
- page.onResourceReceived = function(response) {
- resources[response.id] = response.stage;
- };
- page.viewportSize = { width: pwidth, height: pheight };
- page.open(url, function(status) {
- if(status !== 'success'){
- console.log('Unable to load the address!');
- phantom.exit();
- }else{
- waitFor(function() {
- // Check in the page if a specific element is now visible
- for ( var i = 1; i < resources.length; ++i) {
- if (resources[i] != 'end') {
- return false;
- }
- }
- return true;
- }, function() {
- page.render(output);
- phantom.exit();
- }, 10000);
- /*window.setTimeout(function(){
- page.render(output);
- phantom.exit();
- }, waitTime
- );*/
- }
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement