Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload an PDF file with cypress.io

Tags:

cypress

I would like to upload a PDF file using cypress. The idea will be to go and find the pdf file into my directory. I didn't find some way yet.

This is the element used to upload

like image 962
Marius B Avatar asked Feb 04 '26 09:02

Marius B


2 Answers

2022 Update

Since Cypress v9.3.0 you can use

.selectFile('fileName.pdf')

you can find more details about it in Cypress api docs

like image 132
Ahmed Khashaba Avatar answered Feb 05 '26 23:02

Ahmed Khashaba


Cypress doesn't support file upload out of the box. cypress-file-upload is a npm package which provides a custom cypress command to upload files easily. But, it requires the file to be present in the fixtures folder.

Sample code for uploading pdf file:

const fileName = 'myfile.pdf';

cy.fixture(fileName).then(fileContent => {
  cy.get('#filesToUpload').upload({ fileContent, fileName, mimeType: 'application/pdf' }, { subjectType: 'input' });
});

You can also find more workarounds here: https://github.com/cypress-io/cypress/issues/170

like image 23
Bushra Alam Avatar answered Feb 05 '26 22:02

Bushra Alam