Test Overview
This test automates uploading a file using Cypress's cy.selectFile command. It verifies that the file input accepts the file and that the file name is displayed correctly after upload.
This test automates uploading a file using Cypress's cy.selectFile command. It verifies that the file input accepts the file and that the file name is displayed correctly after upload.
describe('File upload test', () => { it('uploads a file and verifies the file name', () => { cy.visit('https://example.com/upload') cy.get('input[type="file"]').selectFile('cypress/fixtures/sample.txt') cy.get('#file-name-display').should('contain.text', 'sample.txt') }) })
| Step | Action | System State | Assertion | Result |
|---|---|---|---|---|
| 1 | Test starts | Test runner initialized, no browser opened yet | — | PASS |
| 2 | Browser opens and navigates to 'https://example.com/upload' | Upload page is loaded with a file input and a file name display element | Page loaded successfully | PASS |
| 3 | Finds the file input element using selector 'input[type="file"]' | File input element is visible and enabled | File input element exists | PASS |
| 4 | Uploads the file 'cypress/fixtures/sample.txt' using cy.selectFile | File input now contains the selected file | File input value updated with 'sample.txt' | PASS |
| 5 | Finds the element with id 'file-name-display' to verify uploaded file name | Element visible on page showing uploaded file name | Element text contains 'sample.txt' | PASS |
| 6 | Test ends with all assertions passing | File upload verified successfully | All assertions passed | PASS |
cy.selectFile in Cypress testing?cy.selectFilecy.selectFile does not do.example.txt using cy.selectFile?cy.selectFilecypress/fixtures folder.cy.get('input[type=file]').selectFile('cypress/fixtures/photo.png')
.should('have.prop', 'files').then(files => {
expect(files[0].name).to.equal('photo.png')
})selectFile and file input propertiesfiles property contains the selected files. Accessing files[0].name should give the file name.cy.get('#fileInput').selectFile('example.pdf')selectFilecypress/fixtures folder or an absolute path. Just 'example.pdf' without path likely causes failure.selectFile works on input elements. Manual upload is not required in tests.cy.selectFile. Which code snippet correctly uploads two files named doc1.pdf and image1.png located in the fixtures folder?cy.selectFile accepts an array of file paths to upload multiple files. Each path must be relative to the fixtures folder or absolute.Pass the test and this pattern moves unknown