首页 > 解决方案 > 赛普拉斯没有上传图片

问题描述

我正在自动化 cypress 中的一个用例,但我无法使用附加文件功能上传图像。以下是 HTML 标记的代码和片段

用户界面上传图片

用户界面上传图片

HTML 标签

HTML 标签

VS 代码

VS 代码

标签: javascriptcypress

解决方案


我认为您想将文件附加到<input>作为第一步。然后单击按钮,或者您甚至可能不必执行 click()。

从文档中,

// start watching the POST requests
cy.intercept({ method: 'POST', url: /upload_endpoint/ }).as('upload');

const myFilePath = 'product.jpeg'; // file is in "/cypress/fixtures/" folder

cy.fixture(myFilePath, 'binary')
  .then(Cypress.Blob.binaryStringToBlob)
  .then(fileContent => {
    cy.get('input#productImageUpload').attachFile({
      fileContent,
      fileName: myFilePath,
      mimeType: 'application/octet-stream',
      encoding:'utf8',
      lastModified: new Date().getTime(
    })
  })

cy.get('.label-side').click()   // may not need to do this

// wait for the 'upload_endpoint' request, and leave a 2 minutes delay before throwing an error
cy.wait('@upload', { requestTimeout: 120000 });

推荐阅读