【问题标题】:Does tests order matter in Cypress? When tests uploading pdf files are at the beginning they pass, when at the end: Failed to execute atob on Window赛普拉斯的测试顺序重要吗?当上传 pdf 文件的测试在开始时通过,在结束时:无法在 Window 上执行 atob
【发布时间】:2025-12-13 23:20:02
【问题描述】:

我有一个用 Cypress 编写的测试。上传一个 pdf 文件的测试之一如下所示:

it('uploads one document', () => {
        cy.get('input[type="file"]')
        .attachFile({ filePath: pdfFilePath, encoding: 'base64' , mimeType: 'application/pdf'});

        cy.get('div.table-body>.table-row')
            .find('.col-upload-name')
            .should('have.text', 'TestDocument.pdf')

        cy.get('a.button-action').contains('Upload').click()

        cy.get('div.table-body>.table-row')
            .find('.status-done')
            .should('contain.text', 'Uploaded')
    })

其中 pdfFilePath 是灯具目录中 pdf 文件的字符串路径。

当上传 pdf 文件的测试位于测试套件的开头时,它们通过了:

当他们在测试套件结束时,他们失败了:

我得到这个错误:

有人知道为什么会这样吗?

【问题讨论】:

  • 在测试之间尝试清除本地存储

标签: javascript automated-tests cypress cypress-file-upload


【解决方案1】:

我尝试在我这边重现该问题。我没有得到同样的错误,但它没有添加文件。添加fileContent 后它对我有用。

    cy.get('[type="file"]').attachFile({
      fileContent,
      filePath: "filepath",
      fileName: "filename",
      encoding: 'base64', 
      mimeType: 'application/pdf'

});

【讨论】:

  • 感谢您的回答。你是怎么定义fileConent的?
  • 你没有用fileContent 定义任何东西。这只是让赛普拉斯知道你正在传递一个原始文件。请参阅此处了解更多信息npmjs.com/package/cypress-file-upload
最近更新 更多