Casperjs 减慢链接循环

Casperjs slowing down looping through links

我正在通过对我的网站进行测试来学习 CasperJS,该测试从导航栏中获取所有 links 并循环打开它们并 运行 对每个页面进行一个小测试(检查标题,点击搜索按钮,查看结果是否返回等)。我还包含了一个 "Quick Test" 标志,它只会在进入下一个 link 之前检查页面标题。总共大约有25 link。

问题是脚本在大约 10 次完整测试后卡住了,但在 quick-testing 上运行良好。这是我用来打开每个页面的循环:

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;
            }
        });
    });
});

testPage() 和页面特定函数都类似于:

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");
    });
}

checkTitle()函数很简单:

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

现在,如果 quickTesttrue 那么循环结束,没有问题。如果 quickTestfalse 则循环在第 12 页无限期挂起。巧合的是,第 11 页实际上是同一页,只是搜索过滤器的选项更多。此外,我的 casperjs 脚本告诉我使用 quickTest=false 加载页面需要 13410 毫秒,而使用 quickTest=true 加载页面只需要 460 毫秒,这很令人困惑,因为两个时间戳之间的代码都不是来自该标志的 skipped/added在 IE 中加载页面几乎不需要那么长时间。

为什么 casper 在循环 link 秒后变慢了?

我偶然发现了 this page。似乎在此过程中的某处存在内存泄漏。虽然我仍然不熟悉 casperjs 和 phantomjs,但我猜它涉及循环中的 this.open() 位。通过添加以下内容,我设法完成了所有测试:

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

所以循环代码的开头现在看起来像:

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);
        ......