远程文件下载 [英] Remote File Downloads

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

问题描述

背景:使用 Protractor 和 Chrome 进行一些文件下载测试.我在 selenium 网格上运行,因此测试和我的 Node env 在服务器上执行(例如 8.2.2.2),而文件下载在远程 Windows 机器上(例如 14.3.3.3).

Background: Working on some file download tests with Protractor and Chrome. I run on a selenium grid, so the tests and my Node env are executing on a server (e.g. 8.2.2.2) while the file downloads are on a remote windows machine (e.g. 14.3.3.3).

文件下载曾经存储在启动测试的同一台服务器上,所以我只是在执行断言之前等待文件存在:

The file download used to be stored on the same server that also kicked off the tests, so I was just waiting for a file to exist before performing my assertion:

browser.wait(() => {
    return fs.existsSync(filePath)
}).then(() => {
    // expect something
})

问题: 现在,文件不会写入服务器(它们直接下载到浏览器)所以我没有什么可抓取的......到目前为止.由于我使用了硒网格,因此无法直接从测试服务器读取远程机器.

Problem: Now, the files dont write to the Server (they download directly to the browser) so I have nothing to grab... so far. Since I use a selenium grid I can't directly read the remote machine from the test server.

问题: 量角器浏览器对象或 chromedriver 是否有我可以获取的有关该文件下载的任何信息?试图找到一种方法来访问文件名和文件大小?我正在研究浏览器对象,但还没有找到任何东西.

Question: Will the protractor browser object or chromedriver have any information about that file download that I can grab? Trying to find a way to access both file name and file size? I'm digging into the browser object but havent found anything yet.

推荐答案

忘记了这个问题从来没有人回答过,所以我会在@Florent B 在评论中帮助我后发布我自己的解决方案.为简单起见,我将其分解,代码可以更简洁(也取决于您的用例):

Forgot this was never answered, so I'll post my own solution after @Florent B helped me in the comments. I broke this down for simplicity sake, the code could be much cleaner (also depends on your use case):

it('generates a file', () => {
    // begin file download
    btnGenerateReport.click()
    .then(() => {
        // open a new window to leave current one in state
        return browser.executeScript('window.open()')
    })
    .then(() => {
        // switch to new window
        return browser.getAllWindowHandles().then((handles) => {
            return browser.switchTo().window(handles[1]);
        })
    })
    .then(() => {
        // navigate to downloads
        return browser.get('chrome://downloads')
    })
    .then(() => {
        // pauses tests until download has 1 item AND item status is 'Complete'
        return browser.wait(() => {
            return browser.executeScript('return downloads.Manager.get().items_.length > 0 && downloads.Manager.get().items_[0].state === "COMPLETE"');
        }, 600000, `"downloads.Manager.get().items_" did not have length > 0 and/or item[0].state did not === "COMPLETE" within ${600000/1000} seconds`)
    })
    .then(() => {
        // get downloads
        return browser.executeScript('return downloads.Manager.get().items_');
    }).then((items) => {
        // this is your download item(s)
        console.log(items);
    });
});

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

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