David Starkey
David Starkey

Reputation: 1840

Casperjs slowing down looping through links

I'm learning CasperJS by making a test for my website that grabs all the links from the nav bar and loops through opening them all up and running a small test for each page (check the title, hit a search button, see if results come back, etc). I also included a "Quick Test" flag that will only check the page title before moving on to the next link. There are about 25 links total.

The issue is that somehow the script gets stuck after about 10 full tests, but works fine with quick-testing. This is the loop I'm using to open each page:

casper.each(linkList, function(self, link){
    self.thenOpen(link, function(){
        self.echo(link);
        temp = Date.now();
        this.open(urlPrefix + link);
        this.then(function(){
            temp = (Date.now()) - temp;
            self.echo("Load time: "+temp.toString()+"ms");
            switch(link){
                //case statements for specific pages
                // - run specialized versions of testPage()
                case "Example":
                    testExample(this);
                    break;
                default:
                    testPage(this);
                    break;
            }
        });
    });
});

The testPage() and page specific functions all look something like:

function testPage(ths){
    checkTitle(ths, "Page Title"); 
    if(quickTest)
        return;

    ths.click('#searchButton');

    casper.waitForSelectorTextChange("#results",function(){
        temp = ths.evaluate(function(){
            return $("tr.row").length;
        });
        if(temp>0)
            casper.echo("Results returned");
        else
            casper.echo("No results returned");
    });
}

The checkTitle() function is just a simple:

function checkTitle(ths, name){
    temp = ths.getTitle();
    casper.echo("Page Title: "+temp+" - App loads: "+(temp==name ?  "PASSED" : "FAILURE"));
}

Now, if quickTest is true then the loop finishes, no problems. If quickTest is false then the loop hangs indefinitely on the 12th page. Coincidentally, the 11th page is literally the same page, just with more options for the search filters. Additionally, my casperjs scripts is telling me it take the page 13410ms to load with quickTest=false and only 460ms with quickTest=true, which is confusing since no code between the 2 timestamps is skipped/added from that flag and loading the page in IE doesn't take nearly that long.

Why is casper slowing down after looping through links?

Upvotes: 0

Views: 137

Answers (1)

David Starkey
David Starkey

Reputation: 1840

I managed to stumble upon this page. It appears that somewhere in this process there is a memory leak. While I'm still unfamiliar with casperjs and phantomjs, I would guess it involves the this.open() bit in the loop. I've managed to get all the tests to finish by adding the following:

casper.page.close();
casper.page = casper.newPage();

So the beginning of the loop code now looks like:

casper.each(linkList, function(self, link){
    self.thenOpen(link, function(){
        self.echo(link);
        casper.page.close();
        casper.page = casper.newPage();
        temp = Date.now();
        this.open(urlPrefix + link);
        ......

Upvotes: 1

Related Questions