【问题标题】:Getting page rload time using casperjs使用 casperjs 获取页面加载时间
【发布时间】:2014-06-10 21:05:10
【问题描述】:

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

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 文档页面上,我没有找到任何可以让我知道页面何时完全加载的内容。寻找结束标签并查看它是否已加载是否有意义?

【问题讨论】:

  • 是单页应用吗?如果没有,那么您可以在下一个casper.then 块中捕获var end = Date.now();,因为它是一个步进函数,它只会在页面从上一步完全加载时继续执行。

标签: casperjs


【解决方案1】:

你应该使用事件:这里是一个例子:

(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(结束)创建范围。

对于您想要的,您可以对load.startedload.finished 执行相同的操作。

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

但是有更好的工具可以做到这一点,Casper 不太适合监控。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多