本文介绍了CasperJS waitForResource:如何获取我一直在等待的资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

casper.test.begin('Test foo', 1, function suite(test) {
    casper.start("http://www.foo.com", function() {
        casper.waitForResource("bar", function(resource) {
            casper.echo(resource.url);
        });
    });

    casper.run(function() {
        test.done();
    });
});

casper.echo 返回 www.foo.com 资源( casper.start 中的一个),而不是带有 bar的资源。

casper.echo returns www.foo.com resource (the one in casper.start), not the one with "bar".

我如何获得 waitForResource 等待的资源?

How can I get the resource i've waited for with waitForResource?

推荐答案

您实际上是在等待 bar 资源。问题在于,then 回调函数中的资源。 org / en / latest / modules / casper.html#waitforresource rel = nofollow> waitForResource 实际上是最后一个<$ c $的页面资源c>开始或打开 thenOpen )调用。它也可能是单页应用程序的当前页面资源。

You actually waited for the "bar" resource. The problem is that resource inside the then callback function of waitForResource is actually the page resource of the last start or open (thenOpen) call. It may also be the current page resource for single page applications.

如果您要等待资源根据其进行操作,您将不得不跳过一些箍:

If you want to wait for the resource and do something based on it, you would have to jump through some hoops:

var res;
casper.waitForResource(function check(resource){
    res = resource;
    return resource.url.indexOf("bar") != -1;
    // or as regular expression:
    //return /bar/.test(resource.url);
}, function(){
    this.echo("Resource found" + res.url);
});

如果您不需要为当前流程做任何事情,则可以随时进行资源处理在事件处理程序中:

If you don't need to do something for the current flow, you can always do the resource handling in the event handler:

casper.on("resource.received", function(resource){
    if (resource.url.indexOf("bar") != -1) {
        // do something
    }
});
casper.start(url); // ...

这篇关于CasperJS waitForResource:如何获取我一直在等待的资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 07:51