0

I have a form that accepts a file and using cypress-file-upload, I made it to work correctly. But I also have a scenario where I need to verify an error when no file is uploaded. The package itself is validating the file is exist or not. How we can test in such case?

cy.get('input[type="file"]').attachFile();// Cannot read property 'filePath' of undefined
cy.get('input[type="file"]').attachFile(""); //missing "filePath" or "fileName".
Please make sure you are passing either "filePath" or "fileName"
cy.get('input[type="file"]').attachFile("expense1.json")//Works for the positive case
cy.get(".alert").should('be.visible').and('contain', "You poor guy did not upload a file...")
Dhamo
  • 1,171
  • 3
  • 19
  • 41

1 Answers1

0

It's not clear from the question how a user would do the wrong thing and cause the alert, this is what you'd want to replicate in test.

Maybe there is no such user path, if it's just for checking some internal error occurred.

You should find out exactly what triggers the alert and test accordingly.

In this case, here's some ideas

  • option allowEmpty: true and cy.get('input[type="file"]').attachFile("")

  • make expense1.json a binary file, or an empty file, or a badly formed json (remove a bracket)

  • give an invalid encoding in options, encoding: 'binary' and use valid expense1.json`

  • give an invalid mimeType in options

Fody
  • 23,754
  • 3
  • 20
  • 37