I am writting a phantomjs script to open a page for multiple times and track pages full load time. In the script below the page is opened 5 times, but the page load times are not accurate, because phantom does't wait for all resources to be downloaded and page fully loaded (he immediately triggers another function and overlaps previous). My Question is: how to make phantomjs each time in a loop wait for all resources and page to be fully loaded so I could track the actual page load time for 5 times and then calculate the average page load? I am new at writting scripts and programming, and it's killing me already, as this is a work task I don't have enough time to learn for myself these things.
var loadpage = function (i, max){
if (i === max) {
phantom.exit();
return;
}
var address = 'http://www.google.com';
t = Date.now();
page.open(address, function(status) {
if (status !== 'success') {
console.log('FAIL to load the address');
} else {
t = Date.now() - t;
console.log('Loading ' + address);
console.log('Loading time ' + t + ' msec');
}
loadpage(i+1, max)
});
};
loadpage(0, 5);