Identify downloaded file in windows selenium node from unix machine

时间:2018-12-19 11:15:45

标签: java selenium selenium-webdriver selenium-chromedriver selenium-grid

My selenium code is deployed in jenkinns which is in unix machine. When my schedulers trigger a job suite, job will run on selenium nodes which are in windows. Gere, i have a test case where file is downloaded in one of the node and i need to verify that downloaded file.

How can i identify a downloaded file in windows from unix machine (Both are different environments).

1 个答案:

答案 0 :(得分:0)

如果您希望在chrome上进行验证,请使用以下代码。

注意:它是用打字稿写的,所以您必须对其进行修改。

function checkChromeForDownloadedFile(fileName: string, state: string = 'COMPLETE') {
    // open new tab
    await browser.executeScript('return window.open()');

    // switch to downloads tab window
    let tabs: string[] = await browser.getAllWindowHandles();
    await browser.switchTo().window(tabs[1]);

    // open downloads page
    await browser.get('chrome://downloads');

    // 1 sec delay.
    await browser.sleep(1000);

    let downloadedItems;

    try {
        await browser.wait(() => {
            // fetch downloaded items 
            return browser.executeScript('return downloads.Manager.get().items_').then((result) => {
                downloadedItems = result;
                if (!downloadedItems) {
                    return false;
                }
                // search for downloaded file with state complete and filename
                return downloadedItems.some(i => i.file_name === fileName && i.state === state.toUpperCase());
            }).catch( () => {
                return false;
            });
        }, 10000, `File ${fileName} with download sate ${state} was not found within 10 seconds`);
    } catch (error) {
        console.log('there was an error while trying to fetch downloaded files');
        throw error;
    }


    // close the tab
    await browser.close();


    // switch back to original window
    await browser.switchTo().window(tabs[0]);
}