如何在cypress中测试视频文件上传?

如何在cypress中测试视频文件上传?,cypress,Cypress,这个论坛真的很有用,因为我是自动化和Cypress新手。在尝试将文件上载到应用程序时,我也面临一个问题 我在command.js中使用了以下代码: Cypress.Commands.add('uploadFile', { prevSubject: true }, (subject, fileName, fileType = '') => { cy.fixture(fileName,'binary').then(content => { return Cypress

这个论坛真的很有用,因为我是自动化和Cypress新手。在尝试将文件上载到应用程序时,我也面临一个问题

我在command.js中使用了以下代码:

Cypress.Commands.add('uploadFile', { prevSubject: true }, (subject, fileName, fileType = '') => {
    cy.fixture(fileName,'binary').then(content => {
      return Cypress.Blob.binaryStringToBlob(content, fileType).then(blob => {
        const el = subject[0];
        const testFile = new File([blob], fileName, {type: fileType});
        const dataTransfer = new DataTransfer();

        dataTransfer.items.add(testFile);
        el.files = dataTransfer.files;
        cy.wrap(subject).trigger('change', { force: true });
      });
    });
  });
然后是测试中的以下代码:

const fileName = 'comp-test-malte.mp4';
const fileType = 'video/mp4';
cy.get('input[type=file]').uploadFile(fileName, fileType);
这对我来说是半途而废,因为它显示上载0%,但从数据库中获取以下错误,文件未上载:

“Firebase存储:索引0处的put中的参数无效:应为Blob或文件。”


请帮忙

目前,似乎不支持此功能,根据这些来源:

您可以使用npm包作为解决方案

在此中,您可能会找到一个合适的解决方法,如下所示:

// sample code found in the thread
.fixture('bear.mp4', 'binary')
  .then(Cypress.Blob.binaryStringToBlob)
  .then(fileContent => {
    cy.get('#upload-video').upload({
      fileContent,
      fileName: 'bear.mp4',
      mimeType: 'video/mp4',
      encoding: 'utf8'
    })
})

有关详细信息,请使用cypress upload file library提供帮助

首先在项目目录中使用终端安装依赖项:

npm install --save-dev cypress-upload-file
然后做:

    describe("Upload the file", () => {
  it("Upload the file and assert the name of the file uploaded", () => {
    cy.visit("https://your-url.example");

    const fileName = "bear.mp4";
    const fileType = "video/mp4";
    const selector = "#upload-video;

    uploadFile(selector, fileName, fileType);
  });
});

希望这对你有用!最诚挚的问候。

非常感谢您的回答。我尝试了上述方法,但再次面临另一个问题:(未捕获(承诺中)事件{isTrusted:true,键入:“error”,target:null,currentTarget:null,eventPhase:0,…}有什么想法吗???@CuriousSoul230您在我与您共享的评论线程链接中尝试过任何解决方案吗?您也可以尝试。让我知道您尝试了什么,以便我可以帮助您更好地了解这对视频文件的效果…我还有另一个方案可以上载json文件…但相同的方法不适用于此:(获取以下错误;标签配置必须是JSON文件。位置1I处JSON中意外的标记o我正在尝试解析JSON并解决问题,到目前为止,它还不起作用……我将尝试更多的方法,如果不起作用,我必须拿出最小的可复制示例。我必须查看文档以了解如何生成JSONe:(感谢到目前为止所有的帮助…:)这个问题为我解决了。下面的代码用于上传一个json文件。它('Uploading label Configuration',()=>{const fileName='config.json';const mimeType='application/json';cy.fixture(fileName)。然后(fileJson=>{const fileContent=json.stringify(fileJson);cy.get('input[type=“file”]”)。upload({fileContent,fileName,mimeType});});});