3

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);
  • Possible duplicate of [phantomjs not waiting for "full" page load](http://stackoverflow.com/questions/11340038/phantomjs-not-waiting-for-full-page-load) – Artjom B. Oct 31 '15 at 11:55
  • 1
    The question is how you define a "full" page load. If you define it by "all resources are loaded" then these answers should get you closer: [one](http://stackoverflow.com/a/14748934/1816580) & [two](http://stackoverflow.com/a/21401636/1816580) – Artjom B. Oct 31 '15 at 11:58

1 Answers1

0

The answer is in phantomjs documentation and also in the examples. Here is the code for loadtime.js :

"use strict";
var page = require('webpage').create(),
    system = require('system'),
    t, address;

if (system.args.length === 1) {
    console.log('Usage: loadspeed.js <some URL>');
    phantom.exit(1);
} else {
    t = Date.now();
    address = system.args[1];
    page.open(address, function (status) {
        if (status !== 'success') {
            console.log('FAIL to load the address');
        } else {
            t = Date.now() - t;
            console.log('Page title is ' + page.evaluate(function () {
                return document.title;
            }));
            console.log('Loading time ' + t + ' msec');
        }
        phantom.exit();
    });
}

You can use this sample like this :

phantomjs loadtime.js http://www.google.com

It gives an output like this :

Page title is Google
Loading time 767 msec

I found that it took 10% more time with phantomjs than with a manual check with Chrome and results are always like this in the comparison.

Mantisse
  • 309
  • 4
  • 15