Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to know how I can verify if a file was downloaded using Selenium Webdriver after I click the download button.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
308 views
Welcome To Ask or Share your Answers For Others

1 Answer

Your question doesn't say whether you want to confirm it locally or remotely(like browserstack) . If it is remotely then my answer will be "NO" as you can see that the file is getting downloaded but you can not access the folder. So you wont be able to assert that the file has been downloaded.

If you want to achieve this locally(in Chrome) then the answer is "YES", you can do it something like this:

In wdio.conf.js(To know where it is getting downloaded)

var path = require('path');

const pathToDownload = path.resolve('chromeDownloads');

// chromeDownloads above is the name of the folder in the root directory
exports.config = {
capabilities: [{
        maxInstances: 1,     
        browserName: 'chrome',
        os: 'Windows',      
        chromeOptions: {
            args: [
                'user-data-dir=./chrome/user-data',
            ],
            prefs: {
                "download.default_directory": pathToDownload,
            }
        }
    }],

And your spec file(To check if the file is downloaded or not ?)

const fsExtra = require('fs-extra');
const pathToChromeDownloads = './chromeDownloads';

describe('User can download and verify a file', () =>{

    before(() => {
        // Clean up the chromeDownloads folder and create a fresh one
        fsExtra.removeSync(pathToChromeDownloads);
        fsExtra.mkdirsSync(pathToChromeDownloads);
    });

    it('Download the file', () =>{
        // Code to download 
    });

    it('Verify the file is downloaded', () =>{
        // Code to verify 
        // Get the name of file and assert it with the expected name
    });
});

more about fs-extra : https://www.npmjs.com/package/fs-extra

Hope this helps.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...