Protractor - Unable to Find a Downloaded File - Download Test on Chrome

Viewed 4323

I want to run e2e test of file download on Google Chrome. I referred to several posts including Protractor - Jasmine - Download file to a relative path, Protractor e2e test case for downloading pdf file and Setting chromedriver preferences on protractor tests and they didn't give me satisfactory.

Here is a short look at my protractor configuration.

    ...
    'os': 'Windows',
    'os_version': '8.1',
    'browserName': 'Chrome',
    'version': '55',
    'chromeOptions': {
        args: ['--no-sandbox', '--test-type=browser'],
        prefs: {
            download: {
                'prompt_for_download': false,
                'directory_upgrade': true,
                'default_directory': '/tmp'
            }
        }
    }
    ...

And here is my test spec.

    it('file download test', () => {
        let filePath = path.resolve('/tmp/' + download-filename);
        // unlink(filePath);

        // click on a link by invoking anchor_element.click()
        // at the moment, file download will be done on chrome
        // with no prompt experienced

        // wait until file has been downloaded,
        //  (in fact, download can be finished within a sec)
        browser.wait(
            () => fs.existsSync(filePath),
            10000
        ).then(() => {
            // and then expectations here
        });
    });

So for my case, the files are downloaded successfully but chromeOptions doesn't seem to work since files are NOT downloaded in the directory given in the 'default_directory'.

What am I wrong? Or where is the file downloaded with chrome, in my case, and by default?

I am using BrowserStack for selenium server and running local test. And I am configuring protractor with multiple capabilities by using getMultiCapabilities option.

Hopefully someone will guide me with some key hints.

5 Answers

Please add below code in you conf.js

var path = require('path');
var downloadsPath = path.resolve(__dirname, './downloads');

And please reference the 'downloadsPath' in the prefs.

prefs: {
          download: {
            'prompt_for_download': false,
            'directory_upgrade': true,
            'default_directory': downloadsPath
          }
        }

I hope this helps.

Thanks

As your tests are running on different server, download does not happen on the local machine. In case the selenium instance is on the same machine then your approach will work just fine. Try again by removing selenium address in your config file and add "directconnect: true" this way you will be able to get the files downloaded in desired path.

But for the same use case i referred to: Remote File Downloads How to download a file using the remote selenium webdriver?

You can create a download folder in your project and set for Chrome. After you click the download button, you need browser wait function for downloading file. I don'n know how to make this work with FireFox, but Chrome passed. I think prefs of FireFox is the difference to prefs of Chrome.

To open prefs of firefox : open FF and paste about:config

i think you should use this:

{
  browserName: 'chrome',
  acceptInsecureCerts: true,
  chromeOptions: {
    args: [
      // "--headless",
    ],
    prefs: {
      'download': {
        'prompt_for_download': false,
        'directory_upgrade': true,
        'default_directory': process.cwd() + "/resources/test/download",
      },

    },
  },
}

You could use default_directory with absolute path with process.cwd() or __dirname (__dirname = _ _ d i r n a m e)

And, if you want to find what you want in about:config in firefox, you can use https://www.freeformatter.com/mime-types-list.html to get value of firefox's prefs.

This solved my issue:

npm i webdriver-manager@latest

after that, the downloaded file appeared in the default_directory

Related