Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
314 views
in Technique[技术] by (71.8m points)

javascript - How to probe if a file was download using Selenium/WebdriverIO

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

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

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...