0

I am automating one of the use cases in cypress and I am unable to upload the image using attach file function. Below is the code and the snippets from HTML tags

UI Upload Image

UI Upload Image

HTML tags

HTML tags

VS code

VS code

Fody
  • 23,754
  • 3
  • 20
  • 37
  • check this question. https://stackoverflow.com/questions/47074225/how-to-test-file-inputs-with-cypress – Dinesh s Oct 20 '21 at 15:22
  • Please provide enough code so others can better understand or reproduce the problem. – Community Oct 20 '21 at 15:23
  • It's better to paste in the test and HTML as text rather than images, because it makes it easier to write an answer by copy/paste and modify for corrections. – Fody Oct 20 '21 at 20:08

1 Answers1

0

I think you want to attach the file to the <input> as a first step. Then click the button, or you may not even have to do the click().

From the docs,

// 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 });
Fody
  • 23,754
  • 3
  • 20
  • 37
  • It threw the same error which I was facing with my code. It says the path or name is missing but I am unable to understand why it is throwing the same error even after giving the path. Error from cypress: missing "filePath" or "fileName". Please make sure you are passing either "filePath" or "fileName" – Sai Krupa Peraka Oct 21 '21 at 11:28
  • Ok, the error about the file path means `/fixtures/product.jpeg` should be changed to `product.jpeg`. Maybe that's all you need to change in your original code. I assumed it was something to do with encoding. – Fody Oct 21 '21 at 20:16
  • Also, looks like the `fileName` parameter can be anything, or even omitted. – Fody Oct 21 '21 at 20:20
  • I tried with all the possibilities but it still throws the same error. – Sai Krupa Peraka Oct 22 '21 at 07:15