Bird
Raised Fist0
Cypresstesting~20 mins

Drag and drop file upload in Cypress - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Drag and Drop File Upload Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Cypress drag and drop file upload test
What will be the test result when running this Cypress test code for drag and drop file upload?
Cypress
describe('Drag and Drop File Upload', () => {
  it('uploads a file via drag and drop', () => {
    cy.visit('/upload')
    cy.get('#dropzone').attachFile('example.pdf', { subjectType: 'drag-n-drop' })
    cy.get('#uploaded-file-name').should('contain.text', 'example.pdf')
  })
})
ATest fails because the selector '#dropzone' does not exist
BTest fails with error: attachFile is not a function
CTest passes because the file is correctly attached and verified
DTest passes but the assertion fails due to wrong file name
Attempts:
2 left
💡 Hint
Check if the attachFile command is used correctly with drag-n-drop option.
❓ locator
intermediate
1:30remaining
Best locator for drag and drop file upload area
Which locator is the best practice to select the drag and drop upload area for testing accessibility and reliability?
Acy.get('[aria-label="File Upload Dropzone"]')
Bcy.get('div').contains('Drop files here')
Ccy.get('#upload-area')
Dcy.get('.dropzone')
Attempts:
2 left
💡 Hint
Consider accessibility attributes and stable selectors.
❓ assertion
advanced
1:30remaining
Correct assertion for verifying file upload success message
Which assertion correctly verifies that the success message 'Upload complete' is visible after drag and drop file upload?
Acy.get('#message').should('have.text', 'Upload complete')
Bcy.get('#message').should('contain.text', 'Upload complete')
Ccy.get('#message').should('include.text', 'Upload complete')
Dcy.get('#message').should('be.visible').and('contain', 'Upload complete')
Attempts:
2 left
💡 Hint
Check for visibility and partial text match.
🔧 Debug
advanced
2:00remaining
Identify the error in this drag and drop file upload test code
What error will this Cypress test code produce when running drag and drop file upload?
Cypress
cy.get('#dropzone').attachFile('test.png')
cy.get('#uploaded-file').should('contain', 'test.png')
ATest fails because attachFile requires subjectType: 'drag-n-drop' for dropzones
BError: attachFile missing drag-n-drop option for dropzone
CTest passes without errors
DError: Cannot find file 'test.png'
Attempts:
2 left
💡 Hint
Consider how attachFile works with drag and drop elements.
❓ framework
expert
3:00remaining
Best practice for testing drag and drop file upload in Cypress with accessibility
Which approach best combines accessibility, reliability, and maintainability when testing drag and drop file upload in Cypress?
AUse cy.get('#upload').attachFile('file.pdf') without drag-n-drop option and assert with cy.get('#message').should('have.text', 'Upload complete')
BUse cy.get('[aria-label="Upload Area"]').attachFile('file.pdf', { subjectType: 'drag-n-drop' }) and assert visibility and text with should('be.visible').and('contain.text', 'Upload complete')
CUse cy.get('.dropzone').attachFile('file.pdf', { subjectType: 'drag-n-drop' }) and assert with cy.contains('Upload complete')
DUse cy.get('input[type=file]').attachFile('file.pdf') and assert with cy.get('.success').should('exist')
Attempts:
2 left
💡 Hint
Think about accessibility attributes and proper assertions.

Practice

(1/5)
1. What is the main purpose of using subjectType: 'drag-n-drop' in Cypress file upload tests?
easy
A. To simulate dragging and dropping a file into an upload area
B. To select a file from the file system dialog
C. To upload files using a URL link
D. To test keyboard navigation on file inputs

Solution

  1. Step 1: Understand the drag-n-drop simulation

    The subjectType: 'drag-n-drop' option in Cypress simulates the user dragging a file and dropping it into a target area, mimicking real user behavior.
  2. Step 2: Differentiate from other upload methods

    This differs from selecting a file via a dialog or uploading by URL, which are not drag-and-drop actions.
  3. Final Answer:

    To simulate dragging and dropping a file into an upload area -> Option A
  4. Quick Check:

    Drag-n-drop simulation = To simulate dragging and dropping a file into an upload area [OK]
Hint: Drag-n-drop means simulating file drag into upload box [OK]
Common Mistakes:
  • Confusing drag-n-drop with file dialog selection
  • Thinking it uploads files from URLs
  • Assuming it tests keyboard navigation
2. Which of the following is the correct Cypress command syntax to drag and drop a file named example.png into an element with selector #dropzone?
easy
A. cy.get('#dropzone').attachFile('example.png', { subjectType: 'drag-n-drop' })
B. cy.get('#dropzone').uploadFile('example.png', { dragDrop: true })
C. cy.get('#dropzone').dragFile('example.png')
D. cy.get('#dropzone').attachFile('example.png')

Solution

  1. Step 1: Identify the correct command and options

    The Cypress plugin for file upload uses attachFile with the option { subjectType: 'drag-n-drop' } to simulate drag and drop.
  2. Step 2: Verify selector and file name usage

    The command targets #dropzone and attaches example.png correctly with the required option.
  3. Final Answer:

    cy.get('#dropzone').attachFile('example.png', { subjectType: 'drag-n-drop' }) -> Option A
  4. Quick Check:

    attachFile + subjectType drag-n-drop = cy.get('#dropzone').attachFile('example.png', { subjectType: 'drag-n-drop' }) [OK]
Hint: Use attachFile with subjectType 'drag-n-drop' for drag-drop [OK]
Common Mistakes:
  • Using non-existent commands like uploadFile or dragFile
  • Omitting the subjectType option
  • Using attachFile without drag-n-drop option
3. Given the following Cypress test code snippet, what will be the result if the file testfile.pdf is successfully dropped?
cy.get('#upload-area').attachFile('testfile.pdf', { subjectType: 'drag-n-drop' })
cy.get('#upload-message').should('contain.text', 'Upload successful')
medium
A. The test fails because the selector #upload-message is incorrect
B. The test fails because attachFile does not support drag-n-drop
C. The test passes only if the file is smaller than 1MB
D. The test passes if the upload message contains 'Upload successful'

Solution

  1. Step 1: Understand the attachFile usage

    The code uses attachFile with subjectType: 'drag-n-drop' correctly to simulate dropping testfile.pdf.
  2. Step 2: Check the assertion on upload message

    The test asserts that the element with #upload-message contains the text 'Upload successful', which indicates a successful upload.
  3. Final Answer:

    The test passes if the upload message contains 'Upload successful' -> Option D
  4. Quick Check:

    Correct attachFile + assertion = The test passes if the upload message contains 'Upload successful' [OK]
Hint: Check assertion text after attachFile drag-n-drop [OK]
Common Mistakes:
  • Assuming attachFile doesn't support drag-n-drop
  • Ignoring the assertion on upload message
  • Confusing selector correctness without context
4. You wrote this Cypress test to drag and drop a file but it fails with an error: attachFile is not a function. What is the most likely cause?
cy.get('#dropzone').attachFile('file.txt', { subjectType: 'drag-n-drop' })
medium
A. The selector '#dropzone' is incorrect
B. The Cypress file upload plugin is not installed or imported
C. The file 'file.txt' does not exist in the fixtures folder
D. The subjectType option is misspelled

Solution

  1. Step 1: Analyze the error message

    The error attachFile is not a function means Cypress does not recognize the attachFile command.
  2. Step 2: Identify missing plugin or import

    This usually happens if the Cypress file upload plugin is not installed or the command is not imported in commands.js.
  3. Final Answer:

    The Cypress file upload plugin is not installed or imported -> Option B
  4. Quick Check:

    attachFile error = missing plugin/import [OK]
Hint: attachFile error means missing plugin or import [OK]
Common Mistakes:
  • Blaming selector without checking plugin
  • Assuming file missing causes attachFile error
  • Ignoring plugin installation step
5. You want to test drag and drop file upload for multiple files at once using Cypress. Which approach correctly simulates dropping two files named img1.png and img2.png into the element .upload-area?
hard
A. cy.get('.upload-area').attachFile('img1.png').attachFile('img2.png', { subjectType: 'drag-n-drop' })
B. cy.get('.upload-area').attachFile('img1.png,img2.png', { subjectType: 'drag-n-drop' })
C. cy.get('.upload-area').attachFile(['img1.png', 'img2.png'], { subjectType: 'drag-n-drop' })
D. cy.get('.upload-area').attachFile(['img1.png', 'img2.png'])

Solution

  1. Step 1: Understand multiple file upload syntax

    The Cypress attachFile command accepts an array of file names to upload multiple files at once.
  2. Step 2: Include drag-n-drop option for simulation

    To simulate drag and drop, the option { subjectType: 'drag-n-drop' } must be included.
  3. Step 3: Verify correct usage of array and options

    cy.get('.upload-area').attachFile(['img1.png', 'img2.png'], { subjectType: 'drag-n-drop' }) correctly passes an array of files and the drag-n-drop option together.
  4. Final Answer:

    cy.get('.upload-area').attachFile(['img1.png', 'img2.png'], { subjectType: 'drag-n-drop' }) -> Option C
  5. Quick Check:

    Multiple files + drag-n-drop = cy.get('.upload-area').attachFile(['img1.png', 'img2.png'], { subjectType: 'drag-n-drop' }) [OK]
Hint: Use array of files with drag-n-drop option for multiple uploads [OK]
Common Mistakes:
  • Passing files as comma-separated string instead of array
  • Calling attachFile twice instead of once with array
  • Omitting drag-n-drop option for simulation
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard