This is the path

 capabilities: {
        'browserName': 'chrome',
        'chromeOptions': {

            prefs: {
                download: {
                    'prompt_for_download': false,
                    'directory_upgrade': true,
                    'default_directory':  'src/test/javascript/e2e/downloads'
                }
            }
        }
    },

I download the file with this,

productDashboardPageObject.exportDashboard();

It downloads the file, but I am not able to check if it was downloaded. It has only count in name, so I can check if it contains count.

After all is done I will delete files, so only those files will stay after I download.

I did lots of things but could not manage.

First delete any already downloaded files in the download location:

 deleteAlreadyDownloadedFiles: function(){
      var filename = global.downloadsPath + '/fileName.extension';
      var fs = require('fs');
      if (fs.existsSync(filename))
      {
        // delete if there is any existing file with same name
        fs.unlinkSync(filename);
      }

    },

And perform the download action and then verify the file is downloaded or not:

 verifyFileDownload: function() {
  var filename = global.downloadsPath + '/fileName.extension';
    browser.driver.wait(function() {
      return fs.existsSync(filename);
    }, 30000).then(function() {
      console.log("Getting the ERROR while downloading file as file is not downloaded.");
    });

}

This is how I did, in protractor+typescript using Jasmine. If anybody knows better implementation please do share.

First, do:

npm install download-file --save

Page Object:

const downloadsFolder = require('downloads-folder');
const fs = require('fs');

export class TestPage{
...
...   

async verifyFileInDownloadsFolder(fileName: string) {
    await console.log('Getting users download path ' + downloadsFolder());
    const filePath = (downloadsFolder() + '\\' + fileName). replace(/\\/g, '/');
    await console.log('Getting the path ' + filePath);
    await browser.wait(async() => fs.existsSync(filePath), this.DEFAULT_WAIT_TIME_SECONDS * 1000, 'File never appeared!');
    await expect(fs.existsSync(filePath)).toBe(true,
      'Failed to download file: ' +  fileName + ' in user directory' + fullFilePath);
    await console.log('File download was successful');
  }
}

Spec:

Import {TestPage} from './projectpath.po'

describe('testsuite desc', ()=> { 
    let test: TestPage;
    beforeEach( async()=> {
        test = new TestPage();
    });

    describe('test case name', ()=> {
        it('TC desc', async()=> {
            await test.verifyFileInDownloadsFolder(abc.docx);
        });
    });
});
New contributor
Panee S is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.