如何在 Cypress 中测试文件上传功能?

Jus*_*ald 3 javascript qa file-upload e2e-testing cypress

我目前正在深入研究 Cypress 并为应用程序编写 e2e 测试。在测试文件上传功能时,我似乎遇到了障碍。由于我作为 QA 工程师的初学者身份以及在这个特定问题上缺乏互联网流量,我已经陷入僵局。我在文档中遇到了Cypres.Blob。不幸的是,文档并不多,我无法将这些示例应用于我需要学习的内容。

describe('文件上传测试', () => {

it('should upload a file', () => {
    let testfile = cy.fixture('../fixtures/cypresstest.txt')
    cy.get('input[type=file]').then(($input) => {
    return Cypress.Blob.base64StringToBlob(testfile, 'file/txt')
        .then((blob) => {
        $input.fileupload('add', { files: blob })
        })
    })
})
Run Code Online (Sandbox Code Playgroud)

});

小智 5

我正在测试我们的文件上传服务,如下所示:

Cypress.Commands.add('uploadFile', (fileNamePath, fileName, fileType = ' ', selector) => {
  cy.get(selector).then((subject) => {
    cy.fixture(fileNamePath, 'base64')
      .then(Cypress.Blob.base64StringToBlob)
      .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
      })
  })
})
Run Code Online (Sandbox Code Playgroud)

然后在规范文件中。单击上传按钮并等待文件加载:

cy.server().route('POST', api-upload-url).as('loadUp')
        cy.uploadFile('./pathToFile/' + fileName, fileName, fileType, fileInput);
        cy.get('.btn-upload.mat-raised-button.mat-primary').click()
        cy.wait('@loadUp').then((response) => {
            expect(response.method).to.eq('POST')
            expect(response.status).to.eq(200)
            expect(response.response.body.status).to.eq('OK')
        })
Run Code Online (Sandbox Code Playgroud)

还有一个检查所需的响应。

它不是超级花哨和优化。但它适用于 3.1.5 和 3.2.0。它使用有头和无头的电子 59