【问题标题】:Cypress is not uploading the image赛普拉斯没有上传图片
【发布时间】:2021-12-07 10:19:15
【问题描述】:

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

界面上传图片

HTML 标签

VS 代码

【问题讨论】:

  • 请提供足够的代码,以便其他人更好地理解或重现问题。
  • 最好将测试和 HTML 粘贴为文本而不是图像,因为这样可以更轻松地通过复制/粘贴来编写答案并进行修改以进行更正。

标签: javascript cypress


【解决方案1】:

我认为您首先要将文件附加到<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 });

【讨论】:

  • 它抛出了与我的代码相同的错误。它说路径或名称丢失,但我无法理解为什么即使在给出路径之后它也会抛出相同的错误。来自 cypress 的错误:缺少“filePath”或“fileName”。请确保您传递的是“filePath”或“fileName”
  • 好的,文件路径的错误意思是/fixtures/product.jpeg应该改成product.jpeg。也许这就是您需要更改原始代码的全部内容。我认为这与编码有关。
  • 另外,看起来fileName 参数可以是任何值,甚至可以省略。
  • 我尝试了所有的可能性,但仍然抛出同样的错误。
猜你喜欢
  • 2020-03-22
  • 1970-01-01
  • 1970-01-01
  • 2020-12-23
  • 1970-01-01
  • 1970-01-01
  • 2021-11-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多