Remote File Downloads

一个人想着一个人 提交于 2019-12-25 00:38:07

问题


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.

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.


回答1:


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


来源:https://stackoverflow.com/questions/47890581/remote-file-downloads

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