【问题标题】:Cypress - check if the file is downloaded赛普拉斯 - 检查文件是否已下载
【发布时间】:2020-06-15 23:43:27
【问题描述】:

我在尝试检查文件是否已下载时遇到了一点问题。

按钮单击会生成一个 PDF 文件并开始下载。

我需要检查它是否有效。

赛普拉斯可以做到这一点吗?

【问题讨论】:

标签: automation automated-tests cypress


【解决方案1】:

cypress/plugins/index.js

    const path = require('path');
    const fs = require('fs');
    
    const downloadDirectory = path.join(__dirname, '..', 'downloads');
    
    const findPDF = (PDFfilename) => {
      const PDFFileName = `${downloadDirectory}/${PDFfilename}`;
      const contents = fs.existsSync(PDFFileName);
      return contents;
    };
    
    const hasPDF = (PDFfilename, ms) => {
      const delay = 10;
      return new Promise((resolve, reject) => {
        if (ms < 0) {
          return reject(
            new Error(`Could not find PDF ${downloadDirectory}/${PDFfilename}`)
          );
        }
        const found = findPDF(PDFfilename);
        if (found) {
          return resolve(true);
        }
        setTimeout(() => {
          hasPDF(PDFfilename, ms - delay).then(resolve, reject);
        }, delay);
      });
    };
    
    module.exports = (on, config) => {
      require('@cypress/code-coverage/task')(on, config);
      on('before:browser:launch', (browser, options) => {
        if (browser.family === 'chromium') {
          options.preferences.default['download'] = {
            default_directory: downloadDirectory,
          };
          return options;
        }
        if (browser.family === 'firefox') {
          options.preferences['browser.download.dir'] = downloadDirectory;
          options.preferences['browser.download.folderList'] = 2;
          options.preferences['browser.helperApps.neverAsk.saveToDisk'] =
            'text/csv';
          return options;
        }
      });
    
      on('task', {
        isExistPDF(PDFfilename, ms = 4000) {
          console.log(
            `looking for PDF file in ${downloadDirectory}`,
            PDFfilename,
            ms
          );
          return hasPDF(PDFfilename, ms);
        },
      });
    
      return config;
    };

集成/pdfExport.spec.js

    before('Clear downloads folder', () => {
       cy.exec('rm cypress/downloads/*', { log: true, failOnNonZeroExit: false });
    });
        
    it('Should download my PDF file and verify its present', () => {
        cy.get('ExportPdfButton').click();
        cy.task('isExistPDF', 'MyPDF.pdf').should('equal', true);
    });

【讨论】:

【解决方案2】:

我建议您查看 HTTP 响应正文。 您可以通过cy.server().route('GET', 'url').as('download') 获得回复(如果您不知道这些方法,请查看 cypress 文档)。

并捕获响应以验证正文不为空:

cy.wait('@download')
    .then((xhr) => {
        assert.isNotNull(xhr.response.body, 'Body not empty')
    })

或者如果下载成功时有弹窗提示成功,也可以验证弹窗是否存在:

cy.get('...').find('.my-pop-up-success').should('be.visible')

最好的,

编辑

请注意cy.server().route() may be deprecated:

cy.server() 和 cy.route() 在 Cypress 6.0.0 中已弃用。在未来的版本中,将删除对 cy.server() 和 cy.route() 的支持。考虑改用 cy.intercept()。

根据migration guide,这是等价的:cy.intercept('GET', 'url').as('download')

【讨论】:

  • 我认为cy.server().route() 方法行不通,因为文件下载不是GET 请求。
  • 感谢 François,它的作用就像魅力一样。在触发下载之前,我调用 GET api 来获取 CSV 使用情况,因此这种方法有助于确保响应有效。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-10-30
  • 1970-01-01
  • 1970-01-01
  • 2023-01-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多