CasperJS并通过iFrame和JavaScript下载文件 [英] CasperJS and downloading a file via iFrame and JavaScript

查看:71
本文介绍了CasperJS并通过iFrame和JavaScript下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本来测试-单击时会生成一个下载文件的iFrame.如何使用CasperJS拦截响应?

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

我需要文件的内容,但是如果不单击元素或更改我正在测试的脚本,就无法真正生成URL.

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.

想法?

推荐答案

我尝试了其他事件,但通过iframe下载时未触发任何事件.我找到了另一个可行的解决方案-但如果您有更好的选择,我想尝试一下.

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.

在这里:

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

这篇关于CasperJS并通过iFrame和JavaScript下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆