0
0
Cypresstesting~10 mins

File upload (cy.selectFile) in Cypress - Test Execution Trace

Choose your learning style9 modes available
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.

Test Code - Cypress
Cypress
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')
  })
})
Execution Trace - 6 Steps
StepActionSystem StateAssertionResult
1Test startsTest runner initialized, no browser opened yet-PASS
2Browser opens and navigates to 'https://example.com/upload'Upload page is loaded with a file input and a file name display elementPage loaded successfullyPASS
3Finds the file input element using selector 'input[type="file"]'File input element is visible and enabledFile input element existsPASS
4Uploads the file 'cypress/fixtures/sample.txt' using cy.selectFileFile input now contains the selected fileFile input value updated with 'sample.txt'PASS
5Finds the element with id 'file-name-display' to verify uploaded file nameElement visible on page showing uploaded file nameElement text contains 'sample.txt'PASS
6Test ends with all assertions passingFile upload verified successfullyAll assertions passedPASS
Failure Scenario
Failing Condition: File input element not found or file path incorrect
Execution Trace Quiz - 3 Questions
Test your understanding
What does the command cy.selectFile('cypress/fixtures/sample.txt') do in this test?
AIt types the file name into a text input.
BIt downloads the file 'sample.txt' from the server.
CIt selects and uploads the file 'sample.txt' to the file input element.
DIt clicks a button to open the file dialog.
Key Result
Always verify that the file input selector matches the actual element on the page and that the file path used in cy.selectFile exists in your test fixtures folder.