0

I am using casperJS and slimerJS engine to get videos, loaded by ajax responses.

steps

  1. Get list of urls
  2. Recursive calls on casper.open for each url in the list
  3. Each casper.open call need to wait the previous casper.open send an ajax response.

The two first points work good. I have problem with the third one. It's important to understand that i don't need to wait that the page finish to load before to load the next casper.open. I just need to wait one ajax response from each url. When i catch this response, i can close this url and open the next one.

To do that, the recursive loop begin with the first occurrence. Then, listen the network with resource.received event and wait for a special contentType. This contentType give me the response where i can find the url to load the video. I store this url in one array and can close thise casper.open and begin the next one.

Here is my code :

// Some JS code...

function receiver(resource) {
    if (resource.contentType == 'myContentType') {
        sources.push(resource.url);// When catch good conteType, push in sources array
    }
}
casper.on("resource.received", receiver); // Listen responses

// Some JS code...

// Login below
casper.then(function() {
    this.evaluate(function(username, password) {
        document.querySelector('#username').value = username;
        document.querySelector('#password').value = password;
        document.querySelector('#LoginSend').click();
    }, username, password);
});

casper.then(function() {
    this.wait(1000, function() {
        this.waitFor(function() {
            urls = this.evaluate(function() {
                //Get and return all urls
            });
            return true;
        });
    });
});

casper.then(function() {
    this.wait(1000, function() {
        click_link();
    });
});

function click_link() {
    casper.then(function() {
        this.wait(1000, function() {
            this.open(address + urls[i]);// Open new url, i tried with theOpen, don't work too.
            this.waitFor(function() {
                return !!sources[i]; // Wait for the contentType is catch
            }, function() {
                sources.pop(); // Because resource.received has two staged
                this.wait(2000, function() {
                    // Until we don't go to all url, recursive call
                    if (sources.length < urls.length) {
                        i++;
                        click_link();
                    }
                });
            });
        });

    });
}

// Some JS code...

The problem is that the first casper.open open the page, wait to catch the good contentType and store the good url in my array sources. Then, the next casper.open is call and the program stay in the waitFor forever. It's because the contentType is never catch... But it's the same kind of page that the previous one. If i begin by this second page first, it works. It's only the next occurrence of the recursive loop which doesn't work, the first occurrence doesn't matter.
Also, if i open these pages in a browser (chrome or firefox), it's working great, he catch the contentType. So, the problem come from my program.
I have an error sometimes during the resource.received listener : "Error type $.cookie". But even if i had jquery.cookie.js, it's not working.

I tried to close the page or to store the cookie and to clear the page between each casper.open, but it still not working.

Thank you in advance.

ElJackiste
  • 4,011
  • 5
  • 23
  • 36
  • Which Slimer.js version do you use? Please register to the `resource.error`, `page.error`, `remote.message` and `casper.page.onResourceTimeout` events ([Example](https://gist.github.com/artjomb/4cf43d16ce50d8674fdf#file-2_caspererrors-js)). Maybe there are errors. – Artjom B. Feb 03 '16 at 18:17
  • Slimerjs 0.9.6, I already put that listeners before and no errors, but i will try again tonight and let you know if i don't find the problem with that. Thank you for the answer. – ElJackiste Feb 03 '16 at 18:31
  • I didn't have time to check it before, sorry for that. I had already all these event listeners in my program. They gives me no error. Just as i said in my first post, one error with one file received : "$.cookie is not a function"... But even if i had jquery.cookie.js it sill not working. – ElJackiste Feb 20 '16 at 15:17
  • In fact i just find it out. I was including "jquery" and "jquery.cookie.js" in the clientScripts options. It was the problem... :( Thank you anyway. – ElJackiste Feb 20 '16 at 15:27

0 Answers0