4

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.

Community
  • 1
  • 1
firstor
  • 243
  • 1
  • 3
  • 8

5 Answers5

5

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

Kuldeep Bhimte
  • 961
  • 1
  • 10
  • 25
0

Try to replace 'default_directory': '/tmp' by 'default_directory': '//tmp'

Bob Bai
  • 283
  • 4
  • 20
  • Thank you for your answer, Bob. However, it doesn't still work. Giving `'//tmp'` to the `'default_directory'` option, what will happen? I mean where will the file be saved after download is completed? so I can check by `fs.existsSync(filePath)`. – firstor May 23 '17 at 06:27
  • If you are running on Windows, `'default_directory': 'C:\\Downloads'`, this should work – Bob Bai May 23 '17 at 07:53
  • As I mentioned in my question, the target platform where the test spec is executed is Windows 8.1. But my work environment is Ubuntu (I run test command on Ubuntu like as `npm run test-e2e`). In such circumstance, when I try to check file existence, `fs.existsSync()` searches the file on Ubuntu, not on Windows. This is the actual problem. I really appreciate your comment. Hopefully you will provide more hints with background principle to me. – firstor May 23 '17 at 11:53
0

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?

0

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.

AquariusPotter
  • 156
  • 2
  • 10
0

This solved my issue:

npm i webdriver-manager@latest

after that, the downloaded file appeared in the default_directory

Vince
  • 31
  • 5