CasperJS and downloading a file via iFrame and JavaScript

僤鯓⒐⒋嵵緔 提交于 2019-12-08 12:31:01

问题


I have a script to test that - on click - generates an iFrame which downloads a file. How can I intercept the response with CasperJS?

I already tried the sequence:

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

I need the content of the file but can not really produce the URL without clicking the element or changing the script I'm testing.

Ideas?


回答1:


I tried other events, but none got fired when downloading via the iframe. I found another solution that works - but if you have something better, I'd like to try it.

Here it comes:

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


来源:https://stackoverflow.com/questions/25163600/casperjs-and-downloading-a-file-via-iframe-and-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!