Angular 6 - 使用 Protractor 测试文件下载时,如何设置文件目的地的相对路径

kri*_*nya 5 protractor angular

我需要下载 CSV 并在 Protractor 测试中检查其内容。测试作为我们部署过程的一部分运行,因此此下载的路径需要是相对的。

查看其他解决方案,我在 /e2e 创建了一个名为 download 的文件夹并将其添加到我的protractor.conf.js

capabilities: {
      browserName: 'chrome',
      chromeOptions: {
          args: ['--disable-gpu']
      },
      prefs: {
        'download': {
          'prompt_for_download': false,
          'default_directory': '/e2e/download'
        }
      }
    },
Run Code Online (Sandbox Code Playgroud)

这是原始测试,单击按钮下载文件,然后检查本地下载文件夹中的内容(下载本身实际运行后需要更改)。

  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');
      });
    });
  });
Run Code Online (Sandbox Code Playgroud)

尽管我已在 中指定了路径capabilities.prefs,但单击按钮时该文件仍会下载到我的本地下载文件夹。即使我将其设置为完整路径--'default_directory': '/Users/kkanya/Documents/Code/project/e2e/download'它仍然会下载到我的本地下载。

我查看了量角器config.ts 文件,但找不到任何可能允许我更改默认下载目录的内容。事实上,我没有看到prefs,这让我相信这可能不是使用最新量角器的正确方法。

任何帮助深表感谢。

kri*_*nya 3

这就是我想出来的。似乎正在发挥作用。它首先删除download其中的目录e2e,然后在下载文件时重新创建它。

请注意,这prefs是一个值chromeOptions(我在原来的问题中遇到了这个错误。)

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);
};
Run Code Online (Sandbox Code Playgroud)

文件.spec.ts

  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');
      });
    });
  });
Run Code Online (Sandbox Code Playgroud)

希望这对某人有帮助。