用于骨干Ajax的CasperJS waitForResource即使资源实际加载

时间:2015-06-24 16:32:42

标签: javascript ajax backbone.js phantomjs casperjs

我添加.waitForResource,即使资源成功加载,它也会超时。

这仅适用于我们的骨干/木偶应用程序进行的Ajax调用。

resource.requestedresource.received添加日志记录表示已加载,我们的测试表明所有数据都已存在。

但是,测试脚本会挂起,直到超时发生。

我在请求AJAX资源时记录,然后我们等待它,并在收到它时记录,并在超时时记录。

Requested发射一次,Received发射两次,timeout发射一次!!!!

Request id 149 : http://localhost:8080/my/resource
[info] [phantom] Wait for request 149 http://localhost:8080/my/resource
Received id 149 : http://localhost:8080/my/resource
Received id 149 : http://localhost:8080/my/resource
[error] [phantom] request for http://localhost:8080/my/resource timed out.

任何人都有骨干应用吗?

更新

这是phantomJS 1.9.8,casperJS 1.1.0-beta3。

这是一个重复的例子,可以重现这个......

casper.options.logLevel= 'debug';
casper.options.verbose = true;

/**
 * Auto waitForResource on all Ajax requests.
 * Inserts a 250ms delay after load to allow any page renders with response
 */
casper.on("resource.requested", function (request){
    //is this Ajax..
    var isAjax = request.headers.some(function(header) {
        return (header.name == "X-Requested-With" && header.value == "XMLHttpRequest");
    });

    if(isAjax){
        this.log("Wait for request " + request.id + " " + request.url, 'info');
        this.waitForResource(request.url, function(){
            this.wait(250); //wait quarter of a sec for any page render after an ajax load...
        }, function(){
            this.log("request for " + request.url + " timed out.", 'error')
        }, 10000);
    }
});

casper.on("resource.received", function (response){
    this.log('Received :'+ response.id + " " + response.url, 'debug');
});


casper.test.begin('CMS Login : Odd Ajax issue ', function (test) {
    casper.start("http://localhost:8080/login")
        .wait(1000)
        .then(function() {
            this.fill('#login_form', { sign_in_username_email: 'test', sign_in_password: 'REDACTED'}, true);
        })
        .waitForUrl('http://localhost:8080')
        .thenOpen("http://localhost:8080/slides")
        .waitForSelector("#main_region .slides")
        .then(function() {
            test.assertExists('#main_region .slides .slide-item', 'Slides have been loaded');
        })
        .run(function() {
            test.done();
        });
});

这是截断的日志。 你可以看到最后,它等待Ajax请求,接收它两次,说完了等待,然后超时。

$ casperjs test tests/ui/auth/test.js
Test file: tests/ui/auth/test.js
# CMS Login : Odd Ajax issue
[info] [phantom] Starting...
[info] [phantom] Running suite: 8 steps
[debug] [phantom] opening url: http://localhost:8080/login, HTTP GET
[debug] [phantom] Navigation requested: url=http://localhost:8080/login, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] Received :1 http://localhost:8080/login
[debug] [phantom] url changed to "http://localhost:8080/login"
[debug] [phantom] Received :1 http://localhost:8080/login
[debug] [phantom] Received :6 http://localhost:8080/resource/css/font-awesome.css?v=
[debug] [phantom] Received :7 http://localhost:8080/resource/css/style.css?v=

<-- more received files -->

[debug] [phantom] Successfully injected Casper client-side utilities
[debug] [phantom] start page is loaded
[info] [phantom] Step _step 3/8 http://localhost:8080/login (HTTP 200)
[info] [phantom] Step _step 3/8: done in 1580ms.

<-- more received files -->

[info] [phantom] wait() finished waiting for 1000ms.
[info] [phantom] Step anonymous 4/8 http://localhost:8080/login (HTTP 200)
[info] [remote] attempting to fetch form element from selector: '#login_form'
[debug] [remote] Set "sign_in_username_email" field value to test
[debug] [remote] Set "sign_in_password" field value to *********
[info] [remote] submitting form to http://localhost:8080/login/auth, HTTP POST
[info] [phantom] Wait for request 44 http://localhost:8080/login/auth
[info] [phantom] Step anonymous 4/8: done in 2609ms.
[info] [phantom] Step _step 5/9 http://localhost:8080/login (HTTP 200)
[info] [phantom] Step _step 5/9: done in 2610ms.
[debug] [phantom] Received :44 http://localhost:8080/login/auth
[debug] [phantom] Received :44 http://localhost:8080/login/auth
[debug] [phantom] Navigation requested: url=http://localhost:8080/, type=Other, willNavigate=true, isMainFrame=true
[info] [phantom] waitFor() finished in 215ms.
[debug] [phantom] Received :45 http://localhost:8080/
[debug] [phantom] url changed to "http://localhost:8080/"
[debug] [phantom] Received :45 http://localhost:8080/
[debug] [phantom] Received :48 http://localhost:8080/resource/css/presentations.css?v=

<-- more received files -->

[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step anonymous 6/10 http://localhost:8080/ (HTTP 200)
[info] [phantom] Step anonymous 6/10: done in 3566ms.
[info] [phantom] Step _step 7/11 http://localhost:8080/ (HTTP 200)
[info] [phantom] Step _step 7/11: done in 3573ms.


<-- more received files -->

[info] [phantom] wait() finished waiting for 250ms.
[info] [phantom] Step _step 8/11 http://localhost:8080/ (HTTP 200)
[info] [phantom] Step _step 8/11: done in 3836ms.
[info] [phantom] waitFor() finished in 42ms.
[debug] [phantom] opening url: http://localhost:8080/slides, HTTP GET
[debug] [phantom] Navigation requested: url=http://localhost:8080/slides, type=Other, willNavigate=true, isMainFrame=true
[debug] [phantom] Received :115 http://localhost:8080/slides
[debug] [phantom] url changed to "http://localhost:8080/slides"
[debug] [phantom] Navigation requested: url=about:blank, type=Other, willNavigate=true, isMainFrame=false
[debug] [phantom] Received :115 http://localhost:8080/slides

<-- more received files -->

[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step _step 10/11 http://localhost:8080/slides (HTTP 200)
[info] [phantom] Step _step 10/11: done in 4389ms.

<-- more received files -->

[info] [phantom] Wait for request 149 http://localhost:8080/api/slide?product_id=1&country_code=go&modal=false&page=1&limit=18&order_by=date_modified&order_direction=DESC&_=1435166114633
[debug] [phantom] Received :149 http://localhost:8080/api/slide?product_id=1&country_code=go&modal=false&page=1&limit=18&order_by=date_modified&order_direction=DESC&_=1435166114633
[debug] [phantom] Received :149 http://localhost:8080/api/slide?product_id=1&country_code=go&modal=false&page=1&limit=18&order_by=date_modified&order_direction=DESC&_=1435166114633
[info] [phantom] waitFor() finished in 666ms.
[info] [phantom] Step _step 11/12 http://localhost:8080/slides (HTTP 200)
[info] [phantom] Step _step 11/12: done in 5076ms.

[warning] [phantom] Casper.waitFor() timeout
[error] [phantom] request for http://localhost:8080/api/slide?product_id=1&country_code=go&modal=false&page=1&limit=18&order_by=date_modified&order_direction=DESC&_=1435166114633 timed out.

[info] [phantom] Step anonymous 12/12 http://localhost:8080/slides (HTTP 200)
PASS Slides have been loaded
[info] [phantom] Step anonymous 12/12: done in 15112ms.
[info] [phantom] Done 12 steps in 15130ms

PASS 1 test executed in 15.134s, 1 passed, 0 failed, 0 dubious, 0 skipped.

更新

这一次,我删除了自定义超时处理程序,并且只记录了为api调用收到的资源(这些都是ajax请求)。

你可以看到它在等待,然后说[info] [phantom] waitFor() finished in 646ms.,然后几行后面说[warning] [phantom] Casper.waitFor() timeout但是收到的事件仍然会发射两次。我不知道......

[debug] [phantom] url changed to "http://localhost:8080/slides"
[debug] [phantom] Successfully injected Casper client-side utilities
[info] [phantom] Step _step 10/11 http://localhost:8080/slides (HTTP 200)
[info] [phantom] Step _step 10/11: done in 4206ms.
[info] [phantom] Wait for request 142 http://localhost:8080/api/slide?product_id=1&country_code=go&modal=false&page=1&limit=18&order_by=date_modified&order_direction=DESC&_=1435171549698
[debug] [phantom] Received :142 http://localhost:8080/api/slide?product_id=1&country_code=go&modal=false&page=1&limit=18&order_by=date_modified&order_direction=DESC&_=1435171549698
[debug] [phantom] Received :142 http://localhost:8080/api/slide?product_id=1&country_code=go&modal=false&page=1&limit=18&order_by=date_modified&order_direction=DESC&_=1435171549698
[info] [phantom] waitFor() finished in 646ms.
[info] [phantom] Step _step 11/12 http://localhost:8080/slides (HTTP 200)
[info] [phantom] Step _step 11/12: done in 4853ms.
[warning] [phantom] Casper.waitFor() timeout
FAIL http://localhost:8080/api/slide?product_id=1&country_code=go&modal=false&page=1&limit=18&order_by=date_modified&order_direction=DESC&_=1435171549698 did not load in 5000ms
#    type: uncaughtError
#    file: tests/ui/auth/test.js
#    error: http://localhost:8080/api/slide?product_id=1&country_code=go&modal=false&page=1&limit=18&order_by=date_modified&order_direction=DESC&_=1435171549698 did not load in 5000ms

#    stack: not provided
FAIL 1 test executed in 9.885s, 0 passed, 1 failed, 0 dubious, 0 skipped.

Details for the 1 failed test:

In tests/ui/auth/test.js
  CMS Login : API form validation
    uncaughtError: http://localhost:8080/api/slide?product_id=1&country_code=go&modal=false&page=1&limit=18&order_by=date_modified&order_direction=DESC&_=1435171549698 did not load in 5000ms

0 个答案:

没有答案