【问题标题】:How to assert the values in a downloaded file using Cypress如何使用赛普拉斯断言下载文件中的值
【发布时间】:2020-02-05 17:36:33
【问题描述】:

我正在下载一个压缩了 json 的 zip 文件。使用 cy.readfile,我可以读取内容,但我不确定可以使用哪些命令来断言里面的值。 (请让我知道是否有办法在阅读之前解压缩文件) 我需要验证 json 中存在 3 个 objectid 以及元素的一些值。 我尝试了以下方法,但没有成功。

cy.readFile(`/Users/${username}/Downloads/${fileName}.zip`) 
  .should('contain','objectid').and('have.length',3); 

上面的命令对我不起作用:(

有人可以帮我举一些例子吗?我是柏树和编码的新手,因此有点挣扎。

【问题讨论】:

  • 文件被下载到下载文件夹而不是夹具。
  • 我查了很多帖子,都没有提到如何更改文件下载位置。我刚刚用 cy.readfile 的样子更新了帖子。我实际上需要一些关于如何断言 json 中的值的见解。在这种情况下,正常的断言命令无法正常工作
  • cy.fixture() 似乎不起作用.. 错误:在以下任何路径中都找不到夹具文件:tests\e2e\fixtures\Task1.exportZip.zip 我认为解压缩不是什么大问题,但我需要一些断言示例的帮助。这是一个非常基本的问题,但一些示例会有所帮助。如果您可以查看帖子中的图像并给我一个示例来断言该文件具有 2 个 objectid。

标签: automation cypress


【解决方案1】:

您可以在每个测试用例中更改下载文件夹!!

在 -> cypress -> plugins -> index.js 中查看你的 index.js 并写下:

module.exports = (on, config) => {
 on('before:browser:launch', (browser, options) => {
    const downloadDirectory = 'C:\\downloads\\';    // this is the path you want to download

    options.preferences.default['download'] = { default_directory: downloadDirectory };

    return options;
  });

};

【讨论】:

    【解决方案2】:

    这样做

    cy.readFile(`/Users/${username}/Downloads/${fileName}.zip`)
     .then((data) => {
          // you can write whatever assertions you want on data 
          debugger;
          console.log(data);
          expect(data).to....
      })
    

    你可以像上面那样放置调试器和日志来检查数据包含什么然后断言

    使用此链接了解可用的断言https://docs.cypress.io/guides/references/assertions.html#BDD-Assertions

    【讨论】:

    • (请让我知道是否有办法在阅读之前解压缩文件)我在括号中添加了这个,因为它对我来说不是一个阻止程序。cy.readfile 能够在没有的情况下读取 zip 文件解压缩。 docs.cypress.io/guides/references/… 这个链接非常有用,但由于我是赛普拉斯的新手,如果有人可以根据所附图片给我一些基本示例,那将非常有帮助。
    • 您不太可能针对 zip 文件的原始内容进行测试。需要先解压获取JSON内容。
    • @Ackroydd 你知道如何在 cypress 中解压吗?
    【解决方案3】:

    所以这是我正在遵循的方法。它很长,但仍然发布,因为它可能对某人有帮助。如果您有任何改进建议,请在此处发表评论。 我正在使用 npm-unzipper 解压下载的文件。

    第一步:$ npm install unzipper

    第 2 步:在插件中 > index.js

    const fs = require('fs');
    const os = require('os');
    const osplatform = os.platform();
    const unzipper = require('unzipper');
    const userName = os.userInfo().username;
    let downloadPath =`/${userName}/Downloads/`; 
      if (osplatform == 'win32'){
        downloadPath = `/Users/${userName}/Downloads/`;        
      }
    on('task', {
            extractzip(zipname) {
                const zipPath = downloadPath + zipname; 
                if (fs.existsSync(zipPath)) {
                  const readStream = fs.createReadStream(zipPath);
                  readStream.pipe(unzipper.Extract({path: `${downloadPath}`}));
                  const jsonname = 'testfile.json'
                  const jsonPath = downloadPath + jsonname;
                  return jsonPath;
                }
                else{
                  console.error('file not downloaded')
                  return null; 
                }
            }
      })
    

    第 3 步:支持 > commands.js

    Cypress.Commands.add('comparefiles', { prevSubject: false }, (subject, options = {}) => {
        cy.task('extractzip', 'exportfile.zip').then((jsonPath) => {
            cy.fixture('export.json').then((comparefile) => {
                cy.readFile(jsonPath).then((exportedfile) => {
                    var exported_objectinfo = exportedfile.objectInfo;
                    var compare_objectinfo = comparefile.objectInfo;
                    var exported_metaInfo = exportedfile.metaInfo;
                    var compare_metaInfo = comparefile.metaInfo;
                    expect(exported_objectinfo).to.contain.something.like(compare_objectinfo)
                    expect(exported_metaInfo).to.have.deep.members(compare_metaInfo)
                })
            })
        });
    });
    

    第 4 步:规范 > exportandcompare.js

    cy.get('[data-ci-button="Export"]').click();
    cy.comparefiles(); 
    

    【讨论】:

      猜你喜欢
      • 2021-12-21
      • 2021-12-22
      • 2022-07-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多