5

I need to download a CSV and check its content in a Protractor test. The tests are running as part of our deployment process, so the path of this download needs to be relative.

Looking at other solutions, I have created a folder at /e2e called download and added this to my protractor.conf.js:

capabilities: {
      browserName: 'chrome',
      chromeOptions: {
          args: ['--disable-gpu']
      },
      prefs: {
        'download': {
          'prompt_for_download': false,
          'default_directory': '/e2e/download'
        }
      }
    },

Here is the original test, which is clicking on a button to download the file and then checking the contents in my local Downloads folder (this needs to be changed once the download itself is actually working).

  it('should export the csv', () => {
    const filename = '../../../Downloads/new_Name_List_members.csv';
    const exportButton = testUI.findElementBy({findBy: 'buttonText', value: 'Export to CSV'});
    exportButton.click().then(() => {
      browser.driver.wait(function() {
        // Wait until the file has been downloaded.
        return fs.existsSync(filename);
      }, 5000).then(function() {
        expect(fs.readFileSync(filename, { encoding: 'utf8' })).toContain('username,email,name');
      });
    });
  });

Even though I've specified the path in capabilities.prefs, the file is still downloading to my local Downloads folder when the button is clicked. Even if I set this to the full path---'default_directory': '/Users/kkanya/Documents/Code/project/e2e/download'---it still downloads to my local Downloads.

I've looked through the protractor config.ts file and can't spot anything that might allow me to change the default download directory. In fact, I don't see a prefs, which leads me to believe this may not be the correct way to do it with the newest Protractor.

Any help is much appreciated.

kriskanya
  • 667
  • 2
  • 6
  • 17

1 Answers1

3

This is what I came up with. It seems to be working. It first deletes the download directory in e2e and then recreates it at the time the file is downloaded.

Note that prefs is a value on chromeOptions (I had this incorrect in my original question.)

protractor.conf.js:

exports.config = {
    capabilities: {
      browserName: 'chrome',
      chromeOptions: {
        args: ['--disable-gpu'],
        prefs: {
          download: {
            'prompt_for_download': false,
            'default_directory': './e2e/download'
          }
        }
      }
    },

    ...

    onPrepare() {
      // delete ./e2e/download before tests are run
        rmDir('./e2e/download');
    }

// https://sqa.stackexchange.com/questions/24667/how-to-clear-a-directory-before-running-protractor-tests/27653
var fs = require('fs');  

function rmDir (dirPath) {
  try { var files = fs.readdirSync(dirPath); }
  catch(e) { return; }
  if (files.length > 0)
    for (var i = 0; i < files.length; i++) {
      var filePath = dirPath + '/' + files[i];
      if (fs.statSync(filePath).isFile())
        fs.unlinkSync(filePath);
      else
        rmDir(filePath);
    }
  fs.rmdirSync(dirPath);
};

the .spec.ts file:

  it('should export the csv', () => {
    const filename = './e2e/download/new_Name_List_members.csv';

    const exportButton = testUI.findElementBy({findBy: 'buttonText', value: 'Export to CSV'});
    exportButton.click().then(() => {
      browser.driver.wait(function() {
        // Wait until the file has been downloaded.
        return fs.existsSync(filename);
      }, 5000).then(function() {
        expect(fs.readFileSync(filename, { encoding: 'utf8' })).toContain('username,email,name');
      });
    });
  });

Hope this helps someone.

kriskanya
  • 667
  • 2
  • 6
  • 17