Getting page rload time using casperjs

妖精的绣舞 提交于 2019-12-08 12:39:43

问题


I'm trying to capture the page load time using casperjs. The page in question is login protected. Here is what I have so far:

var casper = require('casper').create();

// Enter the login page, fill up the form and submit it
casper.start('https://example.net/login', function () {
        // Fill the form
    this.fill('form[name=signin]', {
       'user': 'username',
       'passwd': 'password'
    }, false);

        // Submit the form by clicking the submit button
    this.then(function() { 
        this.click('#sign_in');
    });
});

// Now on the loggedin page click the link of page for which response time is needed
casper.then (function() {
        var start = Date.now();
    this.click ('#pageLink');
        // Measure response time of this page
        var end = Date.now();
        this.echo (end - start);
});

casper.run();

I can pretty much tell it's the wrong approach because I should perhaps wait for the page to load and then capture the end time. But on the casper js documentation page, I did not find any thing that lets me know when the page has loaded fully. Would it make sense to look for the closing tag and see if that has loaded ?


回答1:


You should use the events : here an example :

(function(){
    "use strict";
    var s
    ,e
    ;
    //we use casper events to calculate the time between a http request and its response
    casper.on('page.resource.requested', function(requestData, request) {
        //console.log("request url " + requestData.url);
        s = new Date().getTime();
    });

    casper.on('page.resource.received', function(response) {
        //console.log("response url " + response.url);
        e = new Date().getTime();
        casper.echo("Time between HTTP request and HTTP response : " + (e-s) + "ms","INFO");
    });
})();//then your code

I use an IIFE just to create a scope for var s (start) and e (end).

For what you want, you could do the same with load.started and load.finished.

http://casperjs.readthedocs.org/en/latest/events-filters.html

But there are better tools to do that, Casper isn't that good for monitoring.



来源:https://stackoverflow.com/questions/24089526/getting-page-rload-time-using-casperjs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!