0
0
Cypresstesting~20 mins

File upload (cy.selectFile) in Cypress - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
File Upload Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the result of this Cypress test snippet?

Consider this Cypress test code that uploads a file using cy.selectFile. What will be the test result?

Cypress
cy.get('input[type=file]').selectFile('cypress/fixtures/sample.txt')
  .then(input => {
    expect(input[0].files[0].name).to.equal('sample.txt')
  })
ATest passes because the file name matches 'sample.txt'.
BTest fails because <code>selectFile</code> requires an object, not a string path.
CTest fails due to a syntax error in the <code>then</code> callback.
DTest passes but the file name will be empty.
Attempts:
2 left
💡 Hint

Check how cy.selectFile accepts a string path to a fixture file.

assertion
intermediate
1:30remaining
Which assertion correctly verifies the uploaded file size?

You want to check that the uploaded file size is exactly 1024 bytes after using cy.selectFile. Which assertion is correct?

Aassert.equal(input.files.size, 1024)
Bexpect(input.files[0].size).toBe(1024)
Cexpect(input[0].files[0].size).to.equal(1024)
Dexpect(input[0].files.size).to.equal(1024)
Attempts:
2 left
💡 Hint

Remember the file list is an array-like object accessed by index.

locator
advanced
1:30remaining
Which locator is best to select a file input for uploading?

You want to select the file input element on a form for uploading a file using cy.selectFile. Which locator is best practice?

Acy.get('#fileInput')
Bcy.get('input')
Ccy.get('input[type=file]')
Dcy.get('.upload-button')
Attempts:
2 left
💡 Hint

Use the most specific and stable selector possible.

🔧 Debug
advanced
2:00remaining
Why does this file upload test fail with 'No file selected' error?

Given this test code, why does the upload fail with 'No file selected'?

cy.get('input[type=file]').selectFile({ contents: 'Hello' })
AThe <code>selectFile</code> command requires a file path string, not an object.
BThe <code>contents</code> property must be a Buffer or Uint8Array, not a string.
CThe input element is not visible, so <code>selectFile</code> fails silently.
DThe <code>selectFile</code> command needs the <code>force: true</code> option to work.
Attempts:
2 left
💡 Hint

Check the type expected for the contents property in selectFile.

framework
expert
2:30remaining
How to test drag-and-drop file upload with Cypress cy.selectFile?

You want to simulate a drag-and-drop file upload in Cypress using cy.selectFile. Which option correctly triggers the drag-and-drop event?

Acy.get('#dropzone').attachFile('file.png')
Bcy.get('#dropzone').selectFile('cypress/fixtures/file.png')
Ccy.get('#dropzone').trigger('drop', { dataTransfer: { files: ['file.png'] } })
Dcy.get('#dropzone').selectFile('cypress/fixtures/file.png', { action: 'drag-drop' })
Attempts:
2 left
💡 Hint

Look for the action option in selectFile to simulate drag-drop.