本文是小编为大家收集整理的关于CasperJS和通过iFrame和JavaScript下载一个文件的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。
问题描述
我有一个脚本可以测试 - 单击 - 生成一个下载文件的iframe.我如何用casperj拦截响应?
我已经尝试了序列:
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.
想法?
推荐答案
我尝试了其他事件,但是通过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'); })
问题描述
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?
推荐答案
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'); })