CasperJS并通过iFrame和JavaScript下载文件

时间:2014-08-06 14:53:15

标签: node.js phantomjs casperjs

我有一个脚本来测试 - 点击 - 生成一个下载文件的iFrame。如何拦截CasperJS的响应?

我已经尝试了这个序列:

casper.click('element');
casper.withFrame('frame', function(){
    console.log(this.getCurrentUrl()); // only return about:blank, but should have URL
    console.log("content: " + this.getHTML()); // Yep, empty HMTL

    this.on('resource.received', function(resource){
        console.log(resource.url); // never executed
    });
});

我需要文件的内容,但如果没有点击元素或更改我正在测试的脚本,就无法真正生成URL。

想法?

1 个答案:

答案 0 :(得分:0)

我尝试了其他活动,但是在通过iframe下载时没有被解雇。我找到了另一个有效的解决方案 - 但如果你有更好的东西,我想尝试一下。

来了:

// Check downloaded File
.then(function(){

    // Fetch URL via internals
    var url = this.evaluate(function(){
        return $('__saveReportFrame').src; // JavaScript function in the page
    });

    var file = fs.absolute('plaintext.txt');
    this.download(url, file);

    var fileString = fs.read(file);

    // Whatever test you want to make
    test.assert(fileString.match(/STRING/g) !== null, 'Downloaded File is good');
})
相关问题