Page polls remote url which stops casper execution

338 views
Skip to first unread message

Mike Pearce

unread,
Feb 7, 2013, 11:17:06 AM2/7/13
to casp...@googlegroups.com
Hi,

I've got a script which logs in, then visits a page. I want to take a screengrab of the page, but the execution pauses on the page because the page has some AJAX which is polling a remote URL to keep some data on the page up to date. How do I tell casper to stop waiting for the poll to finish and just move on to the next step?

Cheers,

Mike

Julien Escueta

unread,
Feb 7, 2013, 3:50:52 PM2/7/13
to casp...@googlegroups.com
You want to structure your script like so,

casper.then(function waitForStuff() {
  casper.waitForResource( 'url of the AJAX request' );
});

casper.then(function screenGrab() {
  casper.capture( ... );
});

Another option is to hook onto the resource.received event, documented here, http://casperjs.org/events-filters.html

Mike Pearce

unread,
Feb 8, 2013, 4:56:59 AM2/8/13
to casp...@googlegroups.com
Thanks for the reply Julien

My code looks like this:

casper.thenOpen("https://a-url.com/a-page#Stats", function() {

});

It just hangs here, because this page is called http://do-a-poll.com/poll every 5 seconds, so I guess casper thinks it hasn't finished loading. I've tried adding: 

casper.thenOpen("https://a-url.com/a-page#Stats", function() {
        this.on('resource.received', function(resource) {
   casper.capture('test2.png');
})
});

But, whatever I do, this is always the verbose output, it just hangs on "url changed to"

[debug] [phantom] opening url: https://url.com/accounts/***/stuff/9248#Stats, HTTP GET
[debug] [phantom] Navigation requested: url=https://url.com/accounts/***/stuff/9248#Stats, type=Other, lock=true, isMainFrame=true
[debug] [phantom] url changed to "https://url.com/accounts/***/stuff/9248#Stats"

Martin Myrseth

unread,
Mar 8, 2013, 7:09:57 AM3/8/13
to casp...@googlegroups.com
I'm having a similar issue with a page which has a long-polling mechanism going in the background. Turning on Casper logging, it stops at the same place: "url changed to ...".

On the server-side I can see that Phantom is continuing to send new requests as the long-polling times out. The page is fully functional in that sense. It's just that Casper won't trigger the next action, as if the document isn't ready loaded or something.

The next action I'm attempting is a waitForSelector, which is completely unresponsive. Supplying a timeout has no effect either.

Martin Myrseth

unread,
Mar 8, 2013, 8:28:26 AM3/8/13
to casp...@googlegroups.com
I can verify that it's the onLoadFinished handler which is not properly called. This is probably a Phantom issue entirely? I'm using Phantom v1.8.1 currently. I'll have a check over there.

Māris Krivtežs

unread,
Apr 9, 2014, 3:21:08 PM4/9/14
to casp...@googlegroups.com
Does anybody have solved this issue or has any workaround?

Maris

Fanch

unread,
Apr 11, 2014, 8:47:58 AM4/11/14
to casp...@googlegroups.com, mikey....@gmail.com
I have the same problem too, i didn't manage to hack this 'block in step' using event (resource.received).
Can't we ask casper to force the passage to the next step (set the flags to false) when the resource/selector/text wanted appears on the page? And block the next resources (prevent the refreshing loop resources). And manually ask for a refresh if needed.

Fanch

unread,
Apr 11, 2014, 9:03:56 AM4/11/14
to casp...@googlegroups.com, mikey....@gmail.com
Prevent polling so. By the way, slimerJS does the same.

Fanch

unread,
Apr 11, 2014, 10:24:12 AM4/11/14
to casp...@googlegroups.com, mikey....@gmail.com
This ugly hack works though :

casper.wait(7000, function(){
    //wait 7sec, to be sure you open the next url then do what you want
    //ugly wait time, but that works
});


casper.thenOpen("https://a-url.com/a-page#Stats", function() {
    //stuck
});

Fanch

unread,
Apr 11, 2014, 11:07:18 AM4/11/14
to casp...@googlegroups.com, mikey....@gmail.com
Well.... until you don't want to add a new step in the stack...

Māris Krivtežs

unread,
Apr 11, 2014, 11:10:53 AM4/11/14
to casp...@googlegroups.com, mikey....@gmail.com

If I have such test:
var url = 'http://localhost:25173/';

casper.test.begin('Can open companies page', function(test){
  casper.start(url, function(){
    // login here
  }).thenClick('a[href^="/Companies"]', function(){ // <-- here it hangs and function never get's called
    this.echo(this.getCurrentUrl());
    test.assert(url + 'Companies' == this.getCurrentUrl(), "Companies page opened");
  }).run(function(){
    test.done();
  });
});

I'd like to do more steps in the test of course, but can't figure it out how to get past this one.

It would be good to have some filter on resource requests where I could setup some pattern to ignore waiting of some requests.

Fanch

unread,
Apr 16, 2014, 3:35:54 AM4/16/14
to casp...@googlegroups.com, mikey....@gmail.com
Did someone post the issue?

Māris Krivtežs

unread,
Apr 16, 2014, 3:41:29 AM4/16/14
to casp...@googlegroups.com, mikey....@gmail.com
There is an issue at phantom.js:

Not sure if casper.js can handle this or it's phantom.js issue.

Māris Krivtežs

unread,
Apr 29, 2014, 6:31:18 AM4/29/14
to casp...@googlegroups.com, mikey....@gmail.com
My colleague solved the issue for my case. I am using SignalR which does long polling and solution is to stop it:

casper.on('resource.received', function(request){
    // Stop SignalR to avoid Casper from hanging
    if((/signalr\/connect/gi).test(request.url)){
        this.evaluate(function(){
            $.connection.hub.stop();
        });
Reply all
Reply to author
Forward
0 new messages