File upload (cy.selectFile) in Cypress - Build an Automation Script
describe('File Upload Test', () => { beforeEach(() => { cy.visit('/upload'); }); it('uploads a file and verifies success message', () => { // Select the file input and attach the file cy.get('#file-upload').selectFile('cypress/fixtures/sample.txt'); // Verify the file input has the file selected cy.get('#file-upload').then(input => { expect(input[0].files[0].name).to.equal('sample.txt'); }); // Click the upload button cy.get('#upload-submit').click(); // Verify the success message appears with correct text cy.get('#upload-success') .should('be.visible') .and('contain.text', 'File uploaded successfully!'); }); });
The test starts by visiting the upload page before each test.
It uses cy.get('#file-upload').selectFile() to simulate selecting the file sample.txt from the fixtures folder.
Then it asserts the file input element actually contains the selected file by checking the file name.
Next, it clicks the upload button to submit the file.
Finally, it verifies the success message is visible and contains the expected text, confirming the upload succeeded.
This approach uses Cypress best practices: stable selectors, no hard waits, and clear assertions.
Now add data-driven testing to upload three different files: 'sample.txt', 'image.png', and 'document.pdf', verifying success for each.
Practice
cy.selectFile in Cypress testing?Solution
Step 1: Understand the role of
This command is designed to simulate a user action of selecting a file in a file input element during automated tests.cy.selectFileStep 2: Compare options with this purpose
Options B, C, and D describe actions unrelated to file selection for upload, such as downloading, deleting, or opening files, whichcy.selectFiledoes not do.Final Answer:
To simulate a user selecting a file for upload in a test -> Option AQuick Check:
File selection = Simulate user upload [OK]
- Confusing file upload with file download
- Thinking selectFile deletes files
- Assuming selectFile opens files in browser
example.txt using cy.selectFile?Solution
Step 1: Understand file path usage in
The file path should point to the test fixture location, usually inside thecy.selectFilecypress/fixturesfolder.Step 2: Evaluate each option's path
cy.get('input[type=file]').selectFile('cypress/fixtures/example.txt') correctly uses the relative path to the fixture file. cy.get('input[type=file]').selectFile('example.txt') uses just the filename without path, which won't work. cy.get('input[type=file]').selectFile({ contents: 'example.txt' }) incorrectly uses an object with contents as string. cy.get('input[type=file]').selectFile('file://example.txt') uses an invalid file URI.Final Answer:
cy.get('input[type=file]').selectFile('cypress/fixtures/example.txt') -> Option DQuick Check:
Use fixture path for selectFile [OK]
- Using only filename without path
- Passing file contents as string instead of path
- Using invalid file URI scheme
cy.get('input[type=file]').selectFile('cypress/fixtures/photo.png')
.should('have.prop', 'files').then(files => {
expect(files[0].name).to.equal('photo.png')
})Solution
Step 1: Understand
After selecting a file, the input element'sselectFileand file input propertiesfilesproperty contains the selected files. Accessingfiles[0].nameshould give the file name.Step 2: Analyze the assertion
The assertion checks if the first file's name equals 'photo.png', which matches the selected file. This is valid and should pass.Final Answer:
The test will pass because the file name matches -> Option AQuick Check:
File name check = Pass [OK]
- Assuming files property is missing
- Confusing syntax chaining in Cypress
- Expecting undefined file name
cy.get('#fileInput').selectFile('example.pdf')What is the most likely reason for the failure?
Solution
Step 1: Check file path requirements for
The file path must be relative to theselectFilecypress/fixturesfolder or an absolute path. Just 'example.pdf' without path likely causes failure.Step 2: Verify other options
Selector syntax '#fileInput' is valid.selectFileworks on input elements. Manual upload is not required in tests.Final Answer:
The file path is incorrect or missing the fixture folder -> Option BQuick Check:
File path must include fixture folder [OK]
- Using filename without path
- Thinking selector syntax is wrong
- Believing selectFile can't upload files
cy.selectFile. Which code snippet correctly uploads two files named doc1.pdf and image1.png located in the fixtures folder?Solution
Step 1: Understand multiple file upload syntax
cy.selectFileaccepts an array of file paths to upload multiple files. Each path must be relative to the fixtures folder or absolute.Step 2: Evaluate each option
cy.get('input[type=file]').selectFile(['cypress/fixtures/doc1.pdf', 'cypress/fixtures/image1.png']) correctly provides an array with full fixture paths. cy.get('input[type=file]').selectFile('cypress/fixtures/doc1.pdf, cypress/fixtures/image1.png') uses a single string with comma-separated paths, which is invalid. cy.get('input[type=file]').selectFile({ files: ['doc1.pdf', 'image1.png'] }) uses an object with files array but missing paths. cy.get('input[type=file]').selectFile(['doc1.pdf', 'image1.png']) uses array without fixture folder path, which will fail.Final Answer:
cy.get('input[type=file]').selectFile(['cypress/fixtures/doc1.pdf', 'cypress/fixtures/image1.png']) -> Option CQuick Check:
Array of fixture paths = Correct multiple upload [OK]
- Passing comma-separated string instead of array
- Omitting fixture folder in paths
- Using object without correct file paths
Ready to prove it?
Pass the test and this pattern moves unknown
