使用casperjs获取页面加载时间 [英] Getting page rload time using casperjs

查看:42
本文介绍了使用casperjs获取页面加载时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用casperjs捕获页面加载时间。有问题的页面受登录保护。这是我到目前为止的内容:

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();

我可以说这是错误的方法,因为我也许应该等待页面加载然后记录结束时间。但是在casper js文档页面上,我找不到任何让我知道该页面何时完全加载的东西。

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 ?

推荐答案

您应该使用以下事件:示例:

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

我使用IIFE只是为了创建一个范围var s(开始)和e(结束)。

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

对于您想要的内容,可以使用 load.started load进行相同操作.finished

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

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

但是有更好的工具可以这样做,Casper不太适合监视。

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

这篇关于使用casperjs获取页面加载时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆